PageRenderTime 27ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/src/engine/api/options.php

https://github.com/PressCrew/infinity
PHP | 403 lines | 174 code | 50 blank | 179 comment | 17 complexity | 11a0e3a78d6ea16da19fbe803868f7e1 MD5 | raw file
Possible License(s): AGPL-1.0
  1. <?php
  2. /**
  3. * Infinity Theme: options classes file
  4. *
  5. * @author Marshall Sorenson <marshall@presscrew.com>
  6. * @link http://infinity.presscrew.com/
  7. * @copyright Copyright (C) 2010-2011 Marshall Sorenson
  8. * @license http://www.gnu.org/licenses/gpl.html GPLv2 or later
  9. * @package Infinity-api
  10. * @subpackage options
  11. * @since 1.0
  12. */
  13. ICE_Loader::load(
  14. 'utils/ajax',
  15. 'components/options/component',
  16. 'components/options/policy',
  17. 'components/options/registry',
  18. 'components/options/renderer'
  19. );
  20. //
  21. // Helpers
  22. //
  23. /**
  24. * Render an option (field only)
  25. *
  26. * @package Infinity-api
  27. * @subpackage options
  28. * @param string $option_name
  29. * @param boolean $output
  30. * @return mixed
  31. */
  32. function infinity_option( $option_name, $output = true )
  33. {
  34. return infinity_option_fetch( $option_name )->render( $output );
  35. }
  36. /**
  37. * Get an option image src array
  38. *
  39. * @package Infinity-api
  40. * @subpackage options
  41. * @param string $option_name
  42. * @param string $size Either a string (`thumbnail`, `medium`, `large` or `full`), or a two item array representing width and height in pixels, e.g. array(32,32). The size of media icons are never affected.
  43. * @return array
  44. */
  45. function infinity_option_image_src( $option_name, $size = 'thumbnail' )
  46. {
  47. return ICE_Policy::options()->registry()->get( $option_name )->get_image_src( $size );
  48. }
  49. /**
  50. * Get an option image url
  51. *
  52. * @package Infinity-api
  53. * @subpackage options
  54. * @param string $option_name
  55. * @param string $size Either a string (`thumbnail`, `medium`, `large` or `full`), or a two item array representing width and height in pixels, e.g. array(32,32). The size of media icons are never affected.
  56. * @return string
  57. */
  58. function infinity_option_image_url( $option_name, $size = 'thumbnail' )
  59. {
  60. return ICE_Policy::options()->registry()->get( $option_name )->get_image_url( $size );
  61. }
  62. /**
  63. * Fetch and option object from the registry
  64. *
  65. * @package Infinity-api
  66. * @subpackage options
  67. * @param string $option_name
  68. * @return ICE_Option
  69. */
  70. function infinity_option_fetch( $option_name )
  71. {
  72. return ICE_Policy::options()->registry()->get( $option_name );
  73. }
  74. /**
  75. * Get the value of an option
  76. *
  77. * @package Infinity-api
  78. * @subpackage options
  79. * @param string $option_name
  80. * @return ICE_Option
  81. */
  82. function infinity_option_get( $option_name )
  83. {
  84. return infinity_option_fetch($option_name)->get();
  85. }
  86. /**
  87. * Begin rendering an option
  88. *
  89. * @package Infinity-api
  90. * @subpackage options
  91. * @param string $option_name
  92. */
  93. function infinity_option_render_begin( $option_name )
  94. {
  95. global $infinity_246f86b591;
  96. if ( $infinity_246f86b591 instanceof ICE_Option ) {
  97. throw new Exception(
  98. 'Cannot begin rendering option "%s" because rendering of option "%s" has not ended!',
  99. $option_name, $infinity_246f86b591->get_name()
  100. );
  101. }
  102. // fetch it
  103. $infinity_246f86b591 = infinity_option_fetch($option_name)->render_bypass();
  104. // start rendering
  105. return $infinity_246f86b591->render_begin();
  106. }
  107. /**
  108. * Render the escaped title text for the option
  109. *
  110. * @package Infinity-api
  111. * @subpackage options
  112. */
  113. function infinity_option_render_title()
  114. {
  115. global $infinity_246f86b591;
  116. return $infinity_246f86b591->render_title();
  117. }
  118. /**
  119. * Render the escaped description text for the option
  120. *
  121. * @package Infinity-api
  122. * @subpackage options
  123. */
  124. function infinity_option_render_description()
  125. {
  126. global $infinity_246f86b591;
  127. return $infinity_246f86b591->render_description();
  128. }
  129. /**
  130. * Render the label element for the option
  131. *
  132. * @package Infinity-api
  133. * @subpackage options
  134. */
  135. function infinity_option_render_label()
  136. {
  137. global $infinity_246f86b591;
  138. return $infinity_246f86b591->render_label();
  139. }
  140. /**
  141. * Render the field element for the option
  142. *
  143. * @package Infinity-api
  144. * @subpackage options
  145. */
  146. function infinity_option_render_field()
  147. {
  148. global $infinity_246f86b591;
  149. return $infinity_246f86b591->load_template();
  150. }
  151. /**
  152. * Render one or both button elements for the option
  153. *
  154. * @package Infinity-api
  155. * @subpackage options
  156. */
  157. function infinity_option_render_buttons()
  158. {
  159. global $infinity_246f86b591;
  160. return $infinity_246f86b591->render_buttons();
  161. }
  162. /**
  163. * Render the save one button element for the option
  164. *
  165. * @package Infinity-api
  166. * @subpackage options
  167. */
  168. function infinity_option_render_save_one()
  169. {
  170. global $infinity_246f86b591;
  171. return $infinity_246f86b591->render_save_one();
  172. }
  173. /**
  174. * End rendering the option
  175. *
  176. * @package Infinity-api
  177. * @subpackage options
  178. */
  179. function infinity_option_render_end()
  180. {
  181. global $infinity_246f86b591;
  182. // end rendering
  183. $result = $infinity_246f86b591->render_end();
  184. // wipe global
  185. unset( $infinity_246f86b591 );
  186. return $result;
  187. }
  188. /**
  189. * Render a menu composed of all the sections with their options.
  190. *
  191. * @package Infinity-api
  192. * @subpackage options
  193. * @param array $args
  194. */
  195. function infinity_options_render_menu( $args = null )
  196. {
  197. // define default args
  198. $defaults = new stdClass;
  199. $defaults->sections = null;
  200. // parse the args
  201. $options = (object) wp_parse_args( $args, $defaults );
  202. // sections to filter on
  203. $get_sections = array();
  204. // determine what sections to get
  205. if ( !empty( $options->sections ) ) {
  206. // split at comma
  207. $split_sections = explode( ',', $options->sections );
  208. // get each section from registry
  209. foreach ( $split_sections as $split_section ) {
  210. $get_sections[] = trim( $split_section );
  211. }
  212. }
  213. // get section registry for this theme
  214. $sections_registry = ICE_Policy::sections()->registry();
  215. // get only "root" sections
  216. $sections = $sections_registry->get_roots( $get_sections );
  217. // load the menu template
  218. infinity_dashboard_load_template(
  219. 'options/menu.php',
  220. array( 'sections' => $sections )
  221. );
  222. }
  223. /**
  224. * Render a menu section
  225. *
  226. * @package Infinity-api
  227. * @subpackage options
  228. * @param ICE_Section $section
  229. */
  230. function infinity_options_render_menu_section( ICE_Section $section )
  231. {
  232. // get registries for this theme
  233. $sections_registry = ICE_Policy::sections()->registry();
  234. $options_registry = ICE_Policy::options()->registry();
  235. // get children of this section
  236. $children = $sections_registry->get_children( $section );
  237. // get options for section
  238. $options = $options_registry->get_menu_options( $section );
  239. // check results
  240. if ( empty( $children ) && empty( $options ) ) {
  241. // don't render anything
  242. return;
  243. }
  244. // load the menu section template
  245. infinity_dashboard_load_template(
  246. 'options/menu-section.php',
  247. array(
  248. 'section' => $section,
  249. 'children' => $children,
  250. 'options' => $options
  251. )
  252. );
  253. }
  254. /**
  255. * Render options for a menu section
  256. *
  257. * @package Infinity-api
  258. * @subpackage options
  259. * @param array $options
  260. */
  261. function infinity_options_render_menu_options( $options )
  262. {
  263. // load the menu options template
  264. infinity_dashboard_load_template(
  265. 'options/menu-options.php',
  266. array( 'options' => $options )
  267. );
  268. }
  269. /**
  270. * Render options according to the option name POST var
  271. *
  272. * @package Infinity-api
  273. * @subpackage options
  274. */
  275. function infinity_options_render_options_screen()
  276. {
  277. // section
  278. $load_section = null;
  279. if ( !empty($_POST['load_section']) ) {
  280. $load_section = $_POST['load_section'];
  281. } else {
  282. ICE_Ajax::responseStd( false, 'Missing required "load_section" parameter' );
  283. }
  284. // group
  285. $load_group = null;
  286. if ( !empty($_POST['load_group']) ) {
  287. $load_group = $_POST['load_group'];
  288. }
  289. // name
  290. $load_name = null;
  291. if ( !empty($_POST['load_name']) ) {
  292. $load_name = $_POST['load_name'];
  293. }
  294. // options to render
  295. $options = array();
  296. // look up section
  297. $section = ICE_Policy::sections()->registry()->get( $load_section );
  298. // did we get a valid section?
  299. if ( $section instanceof ICE_Section ) {
  300. // load specific option?
  301. if ( $load_name ) {
  302. // look up the single option
  303. $option = ICE_Policy::options()->registry()->get( $load_name, $load_group );
  304. // did we get a valid option?
  305. if ( $option instanceof ICE_Option ) {
  306. // add it to options to array
  307. $options[] = $option;
  308. }
  309. } else {
  310. // get all options for the section
  311. $options = ICE_Policy::options()->registry()->get_menu_options( $section );
  312. }
  313. }
  314. // content to return
  315. $content = wp_nonce_field( 'ice_options_update', '_wpnonce', true, false );
  316. // loop through all options and render each one
  317. foreach ( $options as $option_to_render ) {
  318. // enable post override
  319. $option_to_render->enable_post_override();
  320. // add option to section components to render
  321. $section->add_component( $option_to_render, true );
  322. }
  323. // render the section
  324. $content .= $section->render( false );
  325. // respond
  326. if ( strlen($content) ) {
  327. ICE_Ajax::responseStd( true, null, $content );
  328. } else {
  329. ICE_Ajax::responseStd( false, __( 'Failed to render options', 'infinity-engine') );
  330. }
  331. }
  332. add_action( 'wp_ajax_infinity_options_screen', 'infinity_options_render_options_screen' );
  333. /**
  334. * Initialize options form handling.
  335. *
  336. * @package Infinity-api
  337. * @subpackage options
  338. */
  339. function infinity_options_init_form_processing()
  340. {
  341. // is admin?
  342. if ( false === ICE_IS_ADMIN ) {
  343. // nope, just return
  344. return;
  345. }
  346. // add form processing
  347. if ( defined( 'DOING_AJAX' ) ) {
  348. add_action( 'wp_ajax_infinity_options_update', array( ICE_Policy::options()->registry(), 'process_form_ajax' ) );
  349. } else {
  350. add_action( 'load-appearance_page_' . INFINITY_ADMIN_PAGE, array( ICE_Policy::options()->registry(), 'process_form' ) );
  351. }
  352. }
  353. add_action( 'wp_loaded', 'infinity_options_init_form_processing' );