NEXT craftinamerica.org. Base setup for headless wordpress https://www.craftinamerica.org
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

AdminPage.php 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. <?php
  2. /**
  3. * Administration page base class.
  4. */
  5. abstract class scbAdminPage {
  6. /** Page args
  7. * $page_title string (mandatory)
  8. * $parent (string) (default: options-general.php)
  9. * $capability (string) (default: 'manage_options')
  10. * $menu_title (string) (default: $page_title)
  11. * $submenu_title (string) (default: $menu_title)
  12. * $page_slug (string) (default: sanitized $page_title)
  13. * $toplevel (string) If not empty, will create a new top level menu (for expected values see http://codex.wordpress.org/Administration_Menus#Using_add_submenu_page)
  14. * - $icon_url (string) URL to an icon for the top level menu
  15. * - $position (int) Position of the toplevel menu (caution!)
  16. * $screen_icon (string) The icon type to use in the screen header
  17. * $nonce string (default: $page_slug)
  18. * $action_link (string|bool) Text of the action link on the Plugins page (default: 'Settings')
  19. * $admin_action_priority int The priority that the admin_menu action should be executed at (default: 10)
  20. */
  21. protected $args;
  22. // URL to the current plugin directory.
  23. // Useful for adding css and js files
  24. protected $plugin_url;
  25. // Created at page init
  26. protected $pagehook;
  27. // scbOptions object holder
  28. // Normally, it's used for storing formdata
  29. protected $options;
  30. protected $option_name;
  31. // l10n
  32. protected $textdomain;
  33. // ____________REGISTRATION COMPONENT____________
  34. private static $registered = array();
  35. /**
  36. * Registers class of page.
  37. *
  38. * @param string $class
  39. * @param string $file
  40. * @param object $options (optional) A scbOptions object.
  41. *
  42. * @return bool
  43. */
  44. public static function register( $class, $file, $options = null ) {
  45. if ( isset( self::$registered[ $class ] ) ) {
  46. return false;
  47. }
  48. self::$registered[ $class ] = array( $file, $options );
  49. add_action( '_admin_menu', array( __CLASS__, '_pages_init' ) );
  50. return true;
  51. }
  52. /**
  53. * Replaces class of page.
  54. *
  55. * @param string $old_class
  56. * @param string $new_class
  57. *
  58. * @return bool
  59. */
  60. public static function replace( $old_class, $new_class ) {
  61. if ( ! isset( self::$registered[ $old_class ] ) ) {
  62. return false;
  63. }
  64. self::$registered[ $new_class ] = self::$registered[ $old_class ];
  65. unset( self::$registered[ $old_class ] );
  66. return true;
  67. }
  68. /**
  69. * Removes class of page.
  70. *
  71. * @param string $class
  72. *
  73. * @return bool
  74. */
  75. public static function remove( $class ) {
  76. if ( ! isset( self::$registered[ $class ] ) ) {
  77. return false;
  78. }
  79. unset( self::$registered[ $class ] );
  80. return true;
  81. }
  82. /**
  83. * Instantiates classes of pages.
  84. *
  85. * @return void
  86. */
  87. public static function _pages_init() {
  88. foreach ( self::$registered as $class => $args ) {
  89. new $class( $args[0], $args[1] );
  90. }
  91. }
  92. // ____________MAIN METHODS____________
  93. /**
  94. * Constructor.
  95. *
  96. * @param string|bool $file (optional)
  97. * @param object $options (optional) A scbOptions object.
  98. *
  99. * @return void
  100. */
  101. public function __construct( $file = false, $options = null ) {
  102. if ( is_a( $options, 'scbOptions' ) ) {
  103. $this->options = $options;
  104. }
  105. $this->setup();
  106. $this->check_args();
  107. if ( isset( $this->option_name ) ) {
  108. add_action( 'admin_init', array( $this, 'option_init' ) );
  109. add_action( 'admin_notices', 'settings_errors' );
  110. }
  111. add_action( 'admin_menu', array( $this, 'page_init' ), $this->args['admin_action_priority'] );
  112. if ( $file ) {
  113. $this->file = $file;
  114. $this->plugin_url = plugin_dir_url( $file );
  115. if ( $this->args['action_link'] ) {
  116. add_filter( 'plugin_action_links_' . plugin_basename( $file ), array( $this, '_action_link' ) );
  117. }
  118. }
  119. }
  120. /**
  121. * This is where all the page args can be set.
  122. *
  123. * @return void
  124. */
  125. protected function setup() { }
  126. /**
  127. * Called when the page is loaded, but before any rendering.
  128. * Useful for calling $screen->add_help_tab() etc.
  129. *
  130. * @return void
  131. */
  132. public function page_loaded() {
  133. $this->form_handler();
  134. }
  135. /**
  136. * This is where the css and js go.
  137. * Both wp_enqueue_*() and inline code can be added.
  138. *
  139. * @return void
  140. */
  141. public function page_head() { }
  142. /**
  143. * This is where the contextual help goes.
  144. *
  145. * @return string
  146. */
  147. protected function page_help() { }
  148. /**
  149. * A generic page header.
  150. *
  151. * @return void
  152. */
  153. protected function page_header() {
  154. echo "<div class='wrap'>\n";
  155. echo html( 'h2', $this->args['page_title'] );
  156. }
  157. /**
  158. * This is where the page content goes.
  159. *
  160. * @return void
  161. */
  162. abstract protected function page_content();
  163. /**
  164. * A generic page footer.
  165. *
  166. * @return void
  167. */
  168. protected function page_footer() {
  169. echo "</div>\n";
  170. }
  171. /**
  172. * This is where the form data should be validated.
  173. *
  174. * @param array $new_data
  175. * @param array $old_data
  176. *
  177. * @return array
  178. */
  179. public function validate( $new_data, $old_data ) {
  180. return $new_data;
  181. }
  182. /**
  183. * Manually handle option saving ( use Settings API instead ).
  184. *
  185. * @return bool
  186. */
  187. protected function form_handler() {
  188. if ( empty( $_POST['submit'] ) && empty( $_POST['action'] ) ) {
  189. return false;
  190. }
  191. check_admin_referer( $this->nonce );
  192. if ( ! isset( $this->options ) ) {
  193. trigger_error( 'options handler not set', E_USER_WARNING );
  194. return false;
  195. }
  196. $new_data = wp_array_slice_assoc( $_POST, array_keys( $this->options->get_defaults() ) );
  197. $new_data = stripslashes_deep( $new_data );
  198. $new_data = $this->validate( $new_data, $this->options->get() );
  199. $this->options->set( $new_data );
  200. add_action( 'admin_notices', array( $this, 'admin_msg' ) );
  201. return true;
  202. }
  203. /**
  204. * Manually generate a standard admin notice ( use Settings API instead ).
  205. *
  206. * @param string $msg (optional)
  207. * @param string $class (optional)
  208. *
  209. * @return void
  210. */
  211. public function admin_msg( $msg = '', $class = 'updated' ) {
  212. if ( empty( $msg ) ) {
  213. $msg = __( 'Settings <strong>saved</strong>.', $this->textdomain );
  214. }
  215. echo scb_admin_notice( $msg, $class );
  216. }
  217. // ____________UTILITIES____________
  218. /**
  219. * Generates a form submit button.
  220. *
  221. * @param string|array $value (optional) Button text or array of arguments.
  222. * @param string $action (optional)
  223. * @param string $class (optional)
  224. *
  225. * @return string
  226. */
  227. public function submit_button( $value = '', $action = 'submit', $class = 'button' ) {
  228. $args = is_array( $value ) ? $value : compact( 'value', 'action', 'class' );
  229. $args = wp_parse_args( $args, array(
  230. 'value' => null,
  231. 'action' => $action,
  232. 'class' => $class,
  233. ) );
  234. return get_submit_button( $args['value'], $args['class'], $args['action'] );
  235. }
  236. /**
  237. * Mimics scbForms::form_wrap()
  238. *
  239. * $this->form_wrap( $content ); // generates a form with a default submit button
  240. *
  241. * $this->form_wrap( $content, false ); // generates a form with no submit button
  242. *
  243. * // the second argument is sent to submit_button()
  244. * $this->form_wrap( $content, array(
  245. * 'text' => 'Save changes',
  246. * 'name' => 'action',
  247. * ) );
  248. *
  249. * @see scbForms::form_wrap()
  250. *
  251. * @param string $content
  252. * @param boolean|string|array $submit_button (optional)
  253. *
  254. * @return string
  255. */
  256. public function form_wrap( $content, $submit_button = true ) {
  257. if ( is_array( $submit_button ) ) {
  258. $content .= $this->submit_button( $submit_button );
  259. } else if ( true === $submit_button ) {
  260. $content .= $this->submit_button();
  261. } else if ( false !== strpos( $submit_button, '<input' ) ) {
  262. $content .= $submit_button;
  263. } else if ( false !== strpos( $submit_button, '<button' ) ) {
  264. $content .= $submit_button;
  265. } else if ( false !== $submit_button ) {
  266. $button_args = array_slice( func_get_args(), 1 );
  267. $content .= call_user_func_array( array( $this, 'submit_button' ), $button_args );
  268. }
  269. return scbForms::form_wrap( $content, $this->nonce );
  270. }
  271. /**
  272. * Generates a table wrapped in a form.
  273. *
  274. * @param array $rows
  275. * @param array|boolean $formdata (optional)
  276. *
  277. * @return string
  278. */
  279. public function form_table( $rows, $formdata = false ) {
  280. $output = '';
  281. foreach ( $rows as $row ) {
  282. $output .= $this->table_row( $row, $formdata );
  283. }
  284. $output = $this->form_table_wrap( $output );
  285. return $output;
  286. }
  287. /**
  288. * Wraps the given content in a <form><table>
  289. *
  290. * @param string $content
  291. *
  292. * @return string
  293. */
  294. public function form_table_wrap( $content ) {
  295. $output = $this->table_wrap( $content );
  296. $output = $this->form_wrap( $output );
  297. return $output;
  298. }
  299. /**
  300. * Generates a form table.
  301. *
  302. * @param array $rows
  303. * @param array|boolean $formdata (optional)
  304. *
  305. * @return string
  306. */
  307. public function table( $rows, $formdata = false ) {
  308. $output = '';
  309. foreach ( $rows as $row ) {
  310. $output .= $this->table_row( $row, $formdata );
  311. }
  312. $output = $this->table_wrap( $output );
  313. return $output;
  314. }
  315. /**
  316. * Generates a table row.
  317. *
  318. * @param array $args
  319. * @param array|boolean $formdata (optional)
  320. *
  321. * @return string
  322. */
  323. public function table_row( $args, $formdata = false ) {
  324. return $this->row_wrap( $args['title'], $this->input( $args, $formdata ) );
  325. }
  326. /**
  327. * Mimic scbForms inheritance.
  328. *
  329. * @see scbForms
  330. *
  331. * @param string $method
  332. * @param array $args
  333. *
  334. * @return mixed
  335. */
  336. public function __call( $method, $args ) {
  337. if ( in_array( $method, array( 'input', 'form' ) ) ) {
  338. if ( empty( $args[1] ) && isset( $this->options ) ) {
  339. $args[1] = $this->options->get();
  340. }
  341. if ( 'form' == $method ) {
  342. $args[2] = $this->nonce;
  343. }
  344. }
  345. return call_user_func_array( array( 'scbForms', $method ), $args );
  346. }
  347. /**
  348. * Wraps a string in a <script> tag.
  349. *
  350. * @param string $string
  351. *
  352. * @return string
  353. */
  354. public function js_wrap( $string ) {
  355. return html( "script type='text/javascript'", $string );
  356. }
  357. /**
  358. * Wraps a string in a <style> tag.
  359. *
  360. * @param string $string
  361. *
  362. * @return string
  363. */
  364. public function css_wrap( $string ) {
  365. return html( "style type='text/css'", $string );
  366. }
  367. // ____________INTERNAL METHODS____________
  368. /**
  369. * Registers a page.
  370. *
  371. * @return void
  372. */
  373. public function page_init() {
  374. if ( ! $this->args['toplevel'] ) {
  375. $this->pagehook = add_submenu_page(
  376. $this->args['parent'],
  377. $this->args['page_title'],
  378. $this->args['menu_title'],
  379. $this->args['capability'],
  380. $this->args['page_slug'],
  381. array( $this, '_page_content_hook' )
  382. );
  383. } else {
  384. $func = 'add_' . $this->args['toplevel'] . '_page';
  385. $this->pagehook = $func(
  386. $this->args['page_title'],
  387. $this->args['menu_title'],
  388. $this->args['capability'],
  389. $this->args['page_slug'],
  390. null,
  391. $this->args['icon_url'],
  392. $this->args['position']
  393. );
  394. add_submenu_page(
  395. $this->args['page_slug'],
  396. $this->args['page_title'],
  397. $this->args['submenu_title'],
  398. $this->args['capability'],
  399. $this->args['page_slug'],
  400. array( $this, '_page_content_hook' )
  401. );
  402. }
  403. if ( ! $this->pagehook ) {
  404. return;
  405. }
  406. add_action( 'load-' . $this->pagehook, array( $this, 'page_loaded' ) );
  407. add_action( 'admin_print_styles-' . $this->pagehook, array( $this, 'page_head' ) );
  408. }
  409. /**
  410. * Registers a option.
  411. *
  412. * @return void
  413. */
  414. public function option_init() {
  415. register_setting( $this->option_name, $this->option_name, array( $this, 'validate' ) );
  416. }
  417. /**
  418. * Checks page args.
  419. *
  420. * @return void
  421. */
  422. private function check_args() {
  423. if ( empty( $this->args['page_title'] ) ) {
  424. trigger_error( 'Page title cannot be empty', E_USER_WARNING );
  425. }
  426. $this->args = wp_parse_args( $this->args, array(
  427. 'toplevel' => '',
  428. 'position' => null,
  429. 'icon_url' => '',
  430. 'screen_icon' => '',
  431. 'parent' => 'options-general.php',
  432. 'capability' => 'manage_options',
  433. 'menu_title' => $this->args['page_title'],
  434. 'page_slug' => '',
  435. 'nonce' => '',
  436. 'action_link' => __( 'Settings', $this->textdomain ),
  437. 'admin_action_priority' => 10,
  438. ) );
  439. if ( empty( $this->args['submenu_title'] ) ) {
  440. $this->args['submenu_title'] = $this->args['menu_title'];
  441. }
  442. if ( empty( $this->args['page_slug'] ) ) {
  443. $this->args['page_slug'] = sanitize_title_with_dashes( $this->args['menu_title'] );
  444. }
  445. if ( empty( $this->args['nonce'] ) ) {
  446. $this->nonce = $this->args['page_slug'];
  447. }
  448. }
  449. /**
  450. * Adds contextual help.
  451. *
  452. * @param string $help
  453. * @param string|object $screen
  454. *
  455. * @return string
  456. * @deprecated 1.6.6 Not used
  457. */
  458. public function _contextual_help( $help, $screen ) {
  459. if ( is_object( $screen ) ) {
  460. $screen = $screen->id;
  461. }
  462. $actual_help = $this->page_help();
  463. if ( $screen == $this->pagehook && $actual_help ) {
  464. return $actual_help;
  465. }
  466. return $help;
  467. }
  468. /**
  469. * Displays page content.
  470. *
  471. * @return void
  472. */
  473. public function _page_content_hook() {
  474. $this->page_header();
  475. $this->page_content();
  476. $this->page_footer();
  477. }
  478. /**
  479. * Adds an action link.
  480. *
  481. * @param array $links
  482. *
  483. * @return array
  484. */
  485. public function _action_link( $links ) {
  486. $url = add_query_arg( 'page', $this->args['page_slug'], admin_url( $this->args['parent'] ) );
  487. $links[] = html_link( $url, $this->args['action_link'] );
  488. return $links;
  489. }
  490. }