/wp-content/themes/usaitv/lib/ReduxCore/inc/class.redux_api.php

https://gitlab.com/thisishayat/itv-2016 · PHP · 638 lines · 526 code · 86 blank · 26 comment · 166 complexity · eb69a9c26445d2b5ec7ac1d28ce0a5ce MD5 · raw file

  1. <?php
  2. /**
  3. * Redux Framework API Class
  4. * Makes instantiating a Redux object an absolute piece of cake.
  5. *
  6. * @package Redux_Framework
  7. * @author Dovy Paukstys
  8. * @subpackage Core
  9. */
  10. // Exit if accessed directly
  11. if ( ! defined( 'ABSPATH' ) ) {
  12. exit;
  13. }
  14. // Don't duplicate me!
  15. if ( ! class_exists( 'Redux' ) ) {
  16. /**
  17. * Redux API Class
  18. * Simple API for Redux Framework
  19. *
  20. * @since 1.0.0
  21. */
  22. class Redux {
  23. public static $fields = array();
  24. public static $sections = array();
  25. public static $help = array();
  26. public static $args = array();
  27. public static $priority = array();
  28. public static $errors = array();
  29. public static $init = array();
  30. public static $extensions = array();
  31. public static $uses_extensions = array();
  32. public function __call( $closure, $args ) {
  33. return call_user_func_array( $this->{$closure}->bindTo( $this ), $args );
  34. }
  35. public function __toString() {
  36. return call_user_func( $this->{"__toString"}->bindTo( $this ) );
  37. }
  38. public static function load() {
  39. add_action( 'after_setup_theme', array( 'Redux', 'createRedux' ) );
  40. add_action( 'init', array( 'Redux', 'createRedux' ) );
  41. add_action( 'switch_theme', array( 'Redux', 'createRedux' ) );
  42. }
  43. public static function init( $opt_name = "" ) {
  44. if ( ! empty( $opt_name ) ) {
  45. self::loadRedux( $opt_name );
  46. remove_action( 'setup_theme', array( 'Redux', 'createRedux' ) );
  47. }
  48. }
  49. public static function loadExtensions( $ReduxFramework ) {
  50. if ( $instanceExtensions = self::getExtensions( $ReduxFramework->args['opt_name'], "" ) ) {
  51. foreach ( $instanceExtensions as $name => $extension ) {
  52. if ( ! class_exists( $extension['class'] ) ) {
  53. // In case you wanted override your override, hah.
  54. $extension['path'] = apply_filters( 'redux/extension/' . $ReduxFramework->args['opt_name'] . '/' . $name, $extension['path'] );
  55. if ( file_exists( $extension['path'] ) ) {
  56. require_once $extension['path'];
  57. }
  58. }
  59. if ( ! isset( $ReduxFramework->extensions[ $name ] ) ) {
  60. if ( class_exists( $extension['class'] ) ) {
  61. $ReduxFramework->extensions[ $name ] = new $extension['class']( $ReduxFramework );
  62. } else {
  63. echo '<div id="message" class="error"><p>No class named <strong>' . $extension['class'] . '</strong> exists. Please verify your extension path.</p></div>';
  64. }
  65. }
  66. }
  67. }
  68. }
  69. public static function extensionPath( $extension, $folder = true ) {
  70. if ( ! isset( Redux::$extensions[ $extension ] ) ) {
  71. return;
  72. }
  73. $path = end( Redux::$extensions[ $extension ] );
  74. if ( ! $folder ) {
  75. return $path;
  76. }
  77. return str_replace( 'extension_' . $extension . '.php', '', $path );
  78. }
  79. public static function loadRedux( $opt_name = "" ) {
  80. if ( empty( $opt_name ) ) {
  81. return;
  82. }
  83. $check = ReduxFrameworkInstances::get_instance( $opt_name );
  84. if ( isset( $check->apiHasRun ) ) {
  85. return;
  86. }
  87. $args = self::constructArgs( $opt_name );
  88. $sections = self::constructSections( $opt_name );
  89. if ( ! class_exists( 'ReduxFramework' ) ) {
  90. echo '<div id="message" class="error"><p>Redux Framework is <strong>not installed</strong>. Please install it.</p></div>';
  91. return;
  92. }
  93. if ( isset( self::$uses_extensions[ $opt_name ] ) && ! empty( self::$uses_extensions[ $opt_name ] ) ) {
  94. add_action( "redux/extensions/{$opt_name}/before", array( 'Redux', 'loadExtensions' ), 0 );
  95. }
  96. $redux = new ReduxFramework( $sections, $args );
  97. $redux->apiHasRun = 1;
  98. self::$init[ $opt_name ] = 1;
  99. if ( isset( $redux->args['opt_name'] ) && $redux->args['opt_name'] != $opt_name ) {
  100. self::$init[ $redux->args['opt_name'] ] = 1;
  101. }
  102. }
  103. public static function createRedux() {
  104. foreach ( self::$sections as $opt_name => $theSections ) {
  105. if ( ! self::$init[ $opt_name ] ) {
  106. self::loadRedux( $opt_name );
  107. }
  108. }
  109. }
  110. public static function constructArgs( $opt_name ) {
  111. $args = isset( self::$args[ $opt_name ] ) ? self::$args[ $opt_name ] : array();
  112. $args['opt_name'] = $opt_name;
  113. if ( ! isset( $args['menu_title'] ) ) {
  114. $args['menu_title'] = ucfirst( $opt_name ) . ' Options';
  115. }
  116. if ( ! isset( $args['page_title'] ) ) {
  117. $args['page_title'] = ucfirst( $opt_name ) . ' Options';
  118. }
  119. if ( ! isset( $args['page_slug'] ) ) {
  120. $args['page_slug'] = $opt_name . '_options';
  121. }
  122. return $args;
  123. }
  124. public static function constructSections( $opt_name ) {
  125. $sections = array();
  126. if ( ! isset( self::$sections[ $opt_name ] ) ) {
  127. return $sections;
  128. }
  129. foreach ( self::$sections[ $opt_name ] as $section_id => $section ) {
  130. $section['fields'] = self::constructFields( $opt_name, $section_id );
  131. $p = $section['priority'];
  132. while ( isset( $sections[ $p ] ) ) {
  133. $p++;
  134. }
  135. $sections[ $p ] = $section;
  136. }
  137. ksort( $sections );
  138. return $sections;
  139. }
  140. public static function constructFields( $opt_name = "", $section_id = "" ) {
  141. $fields = array();
  142. if ( ! empty( self::$fields[ $opt_name ] ) ) {
  143. foreach ( self::$fields[ $opt_name ] as $key => $field ) {
  144. if ( $field['section_id'] == $section_id ) {
  145. $p = $field['priority'];
  146. while ( isset( $fields[ $p ] ) ) {
  147. echo $p ++;
  148. }
  149. $fields[ $p ] = $field;
  150. }
  151. }
  152. }
  153. ksort( $fields );
  154. return $fields;
  155. }
  156. public static function getSection( $opt_name = '', $id = '' ) {
  157. self::check_opt_name( $opt_name );
  158. if ( ! empty( $opt_name ) && ! empty( $id ) ) {
  159. if ( ! isset( self::$sections[ $opt_name ][ $id ] ) ) {
  160. $id = strtolower( sanitize_html_class( $id ) );
  161. }
  162. return isset( self::$sections[ $opt_name ][ $id ] ) ? self::$sections[ $opt_name ][ $id ] : false;
  163. }
  164. return false;
  165. }
  166. public static function setSections( $opt_name = '', $sections = array() ) {
  167. self::check_opt_name( $opt_name );
  168. if ( ! empty( $sections ) ) {
  169. foreach ( $sections as $section ) {
  170. Redux::setSection( $opt_name, $section );
  171. }
  172. }
  173. }
  174. public static function getSections( $opt_name = '' ) {
  175. self::check_opt_name( $opt_name );
  176. if ( ! empty( self::$sections[ $opt_name ] ) ) {
  177. return self::$sections[ $opt_name ];
  178. }
  179. return array();
  180. }
  181. public static function removeSection( $opt_name = '', $id = "", $fields = false ) {
  182. if ( ! empty( $opt_name ) && ! empty( $id ) ) {
  183. if ( isset( self::$sections[ $opt_name ][ $id ] ) ) {
  184. $priority = '';
  185. foreach ( self::$sections[ $opt_name ] as $key => $section ) {
  186. if ( $key == $id ) {
  187. $priority = $section['priority'];
  188. self::$priority[ $opt_name ]['sections'] --;
  189. unset( self::$sections[ $opt_name ][ $id ] );
  190. continue;
  191. }
  192. if ( $priority != "" ) {
  193. $newPriority = $section['priority'];
  194. $section['priority'] = $priority;
  195. self::$sections[ $opt_name ][ $key ] = $section;
  196. $priority = $newPriority;
  197. }
  198. }
  199. if ( isset( self::$fields[ $opt_name ] ) && ! empty( self::$fields[ $opt_name ] ) && $fields == true ) {
  200. foreach ( self::$fields[ $opt_name ] as $key => $field ) {
  201. if ( $field['section_id'] == $id ) {
  202. unset( self::$fields[ $opt_name ][ $key ] );
  203. }
  204. }
  205. }
  206. }
  207. }
  208. }
  209. public static function setSection( $opt_name = '', $section = array() ) {
  210. self::check_opt_name( $opt_name );
  211. if ( empty( $section ) ) {
  212. return;
  213. }
  214. if ( ! isset( $section['id'] ) ) {
  215. if ( isset( $section['type'] ) && $section['type'] == "divide" ) {
  216. $section['id'] = time();
  217. } else {
  218. if ( isset( $section['title'] ) ) {
  219. $section['id'] = strtolower( sanitize_title( $section['title'] ) );
  220. } else {
  221. $section['id'] = time();
  222. }
  223. }
  224. if ( ! isset( $section['id'] ) ) {
  225. print_r( $section );
  226. echo "DOVY";
  227. }
  228. if ( isset( self::$sections[ $opt_name ][ $section['id'] ] ) ) {
  229. $orig = $section['id'];
  230. $i = 0;
  231. while ( isset( self::$sections[ $opt_name ][ $section['id'] ] ) ) {
  232. $section['id'] = $orig . '_' . $i;
  233. }
  234. }
  235. }
  236. if ( ! empty( $opt_name ) && is_array( $section ) && ! empty( $section ) ) {
  237. if ( ! isset( $section['id'] ) && ! isset( $section['title'] ) ) {
  238. self::$errors[ $opt_name ]['section']['missing_title'] = "Unable to create a section due to missing id and title.";
  239. return;
  240. }
  241. if ( ! isset( $section['priority'] ) ) {
  242. $section['priority'] = self::getPriority( $opt_name, 'sections' );
  243. }
  244. if ( isset( $section['fields'] ) ) {
  245. if ( ! empty( $section['fields'] ) && is_array( $section['fields'] ) ) {
  246. self::processFieldsArray( $opt_name, $section['id'], $section['fields'] );
  247. }
  248. unset( $section['fields'] );
  249. }
  250. self::$sections[ $opt_name ][ $section['id'] ] = $section;
  251. } else {
  252. self::$errors[ $opt_name ]['section']['empty'] = "Unable to create a section due an empty section array or the section variable passed was not an array.";
  253. return;
  254. }
  255. }
  256. public static function hideSection( $opt_name = '', $id = '', $hide = true ) {
  257. self::check_opt_name( $opt_name );
  258. if ( ! empty( $opt_name ) && ! empty( $id ) ) {
  259. if ( isset ( self::$sections[ $opt_name ][ $id ] ) ) {
  260. self::$sections[ $opt_name ][ $id ]['hidden'] = $hide;
  261. }
  262. }
  263. }
  264. public static function processFieldsArray( $opt_name = "", $section_id = "", $fields = array() ) {
  265. if ( ! empty( $opt_name ) && ! empty( $section_id ) && is_array( $fields ) && ! empty( $fields ) ) {
  266. foreach ( $fields as $field ) {
  267. if ( ! is_array( $field ) ) {
  268. continue;
  269. }
  270. $field['section_id'] = $section_id;
  271. self::setField( $opt_name, $field );
  272. }
  273. }
  274. }
  275. public static function getField( $opt_name = '', $id = '' ) {
  276. self::check_opt_name( $opt_name );
  277. if ( ! empty( $opt_name ) && ! empty( $id ) ) {
  278. return isset( self::$fields[ $opt_name ][ $id ] ) ? self::$fields[ $opt_name ][ $id ] : false;
  279. }
  280. return false;
  281. }
  282. public static function hideField( $opt_name = '', $id = '', $hide = true ) {
  283. self::check_opt_name( $opt_name );
  284. if ( ! empty( $opt_name ) && ! empty( $id ) ) {
  285. if ( isset ( self::$fields[ $opt_name ][ $id ] ) ) {
  286. if ( ! $hide ) {
  287. self::$fields[ $opt_name ][ $id ]['class'] = str_replace( 'hidden', '', self::$fields[ $opt_name ][ $id ]['class'] );
  288. } else {
  289. self::$fields[ $opt_name ][ $id ]['class'] .= 'hidden';
  290. }
  291. }
  292. }
  293. }
  294. public static function setField( $opt_name = '', $field = array() ) {
  295. self::check_opt_name( $opt_name );
  296. if ( ! empty( $opt_name ) && is_array( $field ) && ! empty( $field ) ) {
  297. if ( ! isset( $field['priority'] ) ) {
  298. $field['priority'] = self::getPriority( $opt_name, 'fields' );
  299. }
  300. if ( isset( $field['id'] ) ) {
  301. self::$fields[ $opt_name ][ $field['id'] ] = $field;
  302. }
  303. }
  304. }
  305. public static function removeField( $opt_name = '', $id = '' ) {
  306. self::check_opt_name( $opt_name );
  307. if ( ! empty( $opt_name ) && ! empty( $id ) ) {
  308. if ( isset( self::$fields[ $opt_name ][ $id ] ) ) {
  309. foreach ( self::$fields[ $opt_name ] as $key => $field ) {
  310. if ( $key == $id ) {
  311. $priority = $field['priority'];
  312. self::$priority[ $opt_name ]['fields'] --;
  313. unset( self::$fields[ $opt_name ][ $id ] );
  314. continue;
  315. }
  316. if ( isset( $priority ) && $priority != "" ) {
  317. $newPriority = $field['priority'];
  318. $field['priority'] = $priority;
  319. self::$fields[ $opt_name ][ $key ] = $field;
  320. $priority = $newPriority;
  321. }
  322. }
  323. }
  324. }
  325. return false;
  326. }
  327. public static function setHelpTab( $opt_name = "", $tab = array() ) {
  328. self::check_opt_name( $opt_name );
  329. if ( ! empty( $opt_name ) && ! empty( $tab ) ) {
  330. if ( ! isset( self::$args[ $opt_name ]['help_tabs'] ) ) {
  331. self::$args[ $opt_name ]['help_tabs'] = array();
  332. }
  333. if ( isset( $tab['id'] ) ) {
  334. self::$args[ $opt_name ]['help_tabs'][] = $tab;
  335. } else if ( is_array( end( $tab ) ) ) {
  336. foreach ( $tab as $tab_item ) {
  337. self::$args[ $opt_name ]['help_tabs'][] = $tab_item;
  338. }
  339. }
  340. }
  341. }
  342. public static function setHelpSidebar( $opt_name = "", $content = "" ) {
  343. self::check_opt_name( $opt_name );
  344. if ( ! empty( $opt_name ) && ! empty( $content ) ) {
  345. self::$args[ $opt_name ]['help_sidebar'] = $content;
  346. }
  347. }
  348. public static function setArgs( $opt_name = "", $args = array() ) {
  349. self::check_opt_name( $opt_name );
  350. if ( ! empty( $opt_name ) && ! empty( $args ) && is_array( $args ) ) {
  351. if ( isset( self::$args[ $opt_name ] ) && isset( self::$args[ $opt_name ]['clearArgs'] ) ) {
  352. self::$args[ $opt_name ] = array();
  353. }
  354. self::$args[ $opt_name ] = wp_parse_args( $args, self::$args[ $opt_name ] );
  355. }
  356. }
  357. public static function getArgs( $opt_name = "" ) {
  358. self::check_opt_name( $opt_name );
  359. if ( ! empty( $opt_name ) && ! empty( self::$args[ $opt_name ] ) ) {
  360. return self::$args[ $opt_name ];
  361. }
  362. }
  363. public static function getArg( $opt_name = "", $key = "" ) {
  364. self::check_opt_name( $opt_name );
  365. if ( ! empty( $opt_name ) && ! empty( $key ) && ! empty( self::$args[ $opt_name ] ) ) {
  366. return self::$args[ $opt_name ][ $key ];
  367. } else {
  368. return;
  369. }
  370. }
  371. public static function getOption ($opt_name = "", $key = "") {
  372. self::check_opt_name( $opt_name );
  373. if (!empty($opt_name) && !empty($key)) {
  374. $redux = get_option($opt_name);
  375. if (isset($redux[$key])) {
  376. return $redux[$key];
  377. } else {
  378. return;
  379. }
  380. } else {
  381. return;
  382. }
  383. }
  384. public static function setOption ($opt_name = "", $key = "", $option = "") {
  385. self::check_opt_name( $opt_name );
  386. if (!empty($opt_name) && !empty($key)) {
  387. $redux = get_option($opt_name);
  388. $redux[$key] = $option;
  389. return update_option($opt_name, $redux);
  390. } else {
  391. return false;
  392. }
  393. }
  394. public static function getPriority( $opt_name, $type ) {
  395. $priority = self::$priority[ $opt_name ][ $type ];
  396. self::$priority[ $opt_name ][ $type ] += 1;
  397. return $priority;
  398. }
  399. public static function check_opt_name( $opt_name = "" ) {
  400. if ( empty( $opt_name ) || is_array( $opt_name ) ) {
  401. return;
  402. }
  403. if ( ! isset( self::$sections[ $opt_name ] ) ) {
  404. self::$sections[ $opt_name ] = array();
  405. self::$priority[ $opt_name ]['sections'] = 1;
  406. }
  407. if ( ! isset( self::$args[ $opt_name ] ) ) {
  408. self::$args[ $opt_name ] = array();
  409. self::$priority[ $opt_name ]['args'] = 1;
  410. }
  411. if ( ! isset( self::$fields[ $opt_name ] ) ) {
  412. self::$fields[ $opt_name ] = array();
  413. self::$priority[ $opt_name ]['fields'] = 1;
  414. }
  415. if ( ! isset( self::$help[ $opt_name ] ) ) {
  416. self::$help[ $opt_name ] = array();
  417. self::$priority[ $opt_name ]['help'] = 1;
  418. }
  419. if ( ! isset( self::$errors[ $opt_name ] ) ) {
  420. self::$errors[ $opt_name ] = array();
  421. }
  422. if ( ! isset( self::$init[ $opt_name ] ) ) {
  423. self::$init[ $opt_name ] = false;
  424. }
  425. }
  426. /**
  427. * Retrieve metadata from a file. Based on WP Core's get_file_data function
  428. *
  429. * @since 2.1.1
  430. *
  431. * @param string $file Path to the file
  432. *
  433. * @return string
  434. */
  435. public static function getFileVersion( $file ) {
  436. $data = get_file_data( $file, array( 'version' ), 'plugin' );
  437. return $data[0];
  438. }
  439. public static function checkExtensionClassFile( $opt_name, $name = "", $class_file = "", $instance = "" ) {
  440. if ( file_exists( $class_file ) ) {
  441. self::$uses_extensions[ $opt_name ] = isset( self::$uses_extensions[ $opt_name ] ) ? self::$uses_extensions[ $opt_name ] : array();
  442. if ( ! in_array( $name, self::$uses_extensions[ $opt_name ] ) ) {
  443. self::$uses_extensions[ $opt_name ][] = $name;
  444. }
  445. self::$extensions[ $name ] = isset( self::$extensions[ $name ] ) ? self::$extensions[ $name ] : array();
  446. $version = Redux_Helpers::get_template_version( $class_file );
  447. if ( empty( $version ) && ! empty( $instance ) ) {
  448. if ( isset( $instance->version ) ) {
  449. $version = $instance->version;
  450. }
  451. }
  452. self::$extensions[ $name ][ $version ] = isset( self::$extensions[ $name ][ $version ] ) ? self::$extensions[ $name ][ $version ] : $class_file;
  453. }
  454. }
  455. public static function setExtensions( $opt_name, $path ) {
  456. if ( is_dir( $path ) ) {
  457. $path = trailingslashit( $path );
  458. $folder = str_replace( '.php', '', basename( $path ) );
  459. if ( file_exists( $path . 'extension_' . $folder . '.php' ) ) {
  460. self::checkExtensionClassFile( $opt_name, $folder, $path . 'extension_' . $folder . '.php' );
  461. } else {
  462. $folders = scandir( $path, 1 );
  463. foreach ( $folders as $folder ) {
  464. if ( $folder === '.' or $folder === '..' ) {
  465. continue;
  466. }
  467. if ( file_exists( $path . $folder . '/extension_' . $folder . '.php' ) ) {
  468. self::checkExtensionClassFile( $opt_name, $folder, $path . $folder . '/extension_' . $folder . '.php' );
  469. } else if ( is_dir( $path . $folder ) ) {
  470. self::setExtensions( $opt_name, $path . $folder );
  471. continue;
  472. }
  473. }
  474. }
  475. } else if ( file_exists( $path ) ) {
  476. $name = explode( 'extension_', basename( $path ) );
  477. if ( isset( $name[1] ) && ! empty( $name[1] ) ) {
  478. $name = str_replace( '.php', '', $name[1] );
  479. self::checkExtensionClassFile( $opt_name, $name, $path );
  480. }
  481. }
  482. }
  483. public static function getAllExtensions() {
  484. $redux = ReduxFrameworkInstances::get_all_instances();
  485. foreach ( $redux as $instance ) {
  486. if ( ! empty( self::$uses_extensions[ $instance['args']['opt_name'] ] ) ) {
  487. continue;
  488. }
  489. if ( ! empty( $instance['extensions'] ) ) {
  490. Redux::getInstanceExtensions( $instance['args']['opt_name'], $instance );
  491. }
  492. }
  493. }
  494. public static function getInstanceExtensions( $opt_name, $instance = array() ) {
  495. if ( ! empty( self::$uses_extensions[ $opt_name ] ) ) {
  496. return;
  497. }
  498. if ( empty( $instance ) ) {
  499. $instance = ReduxFrameworkInstances::get_instance( $opt_name );
  500. }
  501. if ( empty( $instance ) || empty( $instance->extensions ) ) {
  502. return;
  503. }
  504. foreach ( $instance->extensions as $name => $extension ) {
  505. if ( $name == "widget_areas" ) {
  506. $new = new Redux_Widget_Areas( $instance );
  507. }
  508. if ( isset( self::$uses_extensions[ $opt_name ][ $name ] ) ) {
  509. continue;
  510. }
  511. if ( isset( $extension->extension_dir ) ) {
  512. Redux::setExtensions( $opt_name, str_replace( $name, '', $extension->extension_dir ) );
  513. } else if ( isset( $extension->_extension_dir ) ) {
  514. Redux::setExtensions( $opt_name, str_replace( $name, '', $extension->_extension_dir ) );
  515. }
  516. }
  517. }
  518. public static function getExtensions( $opt_name = "", $key = "" ) {
  519. if ( empty( $opt_name ) ) {
  520. Redux::getAllExtensions();
  521. if ( empty( $key ) ) {
  522. return self::$extension_paths;
  523. } else {
  524. if ( isset( self::$extension_paths[ $key ] ) ) {
  525. return self::$extension_paths[ $key ];
  526. }
  527. }
  528. } else {
  529. if ( empty( self::$uses_extensions[ $opt_name ] ) ) {
  530. Redux::getInstanceExtensions( $opt_name );
  531. }
  532. if ( empty( self::$uses_extensions[ $opt_name ] ) ) {
  533. return false;
  534. }
  535. $instanceExtensions = array();
  536. foreach ( self::$uses_extensions[ $opt_name ] as $extension ) {
  537. $class_file = end( self::$extensions[ $extension ] );
  538. $name = str_replace( '.php', '', basename( $extension ) );
  539. $extension_class = 'ReduxFramework_Extension_' . $name;
  540. $instanceExtensions[ $extension ] = array(
  541. 'path' => $class_file,
  542. 'class' => $extension_class,
  543. 'version' => Redux_Helpers::get_template_version( $class_file )
  544. );
  545. }
  546. return $instanceExtensions;
  547. }
  548. return false;
  549. }
  550. }
  551. Redux::load();
  552. }