Redux

Redux API Class Simple API for Redux Framework.

Defined (1)

The class is defined in the following location(s).

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