admin-options-page.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) {
  3. exit; // Exit if accessed directly
  4. }
  5. if ( ! class_exists( 'acf_admin_options_page' ) ) :
  6. class acf_admin_options_page {
  7. /** @var array Contains the current options page */
  8. var $page;
  9. /*
  10. * __construct
  11. *
  12. * Initialize filters, action, variables and includes
  13. *
  14. * @type function
  15. * @date 23/06/12
  16. * @since 5.0.0
  17. *
  18. * @param n/a
  19. * @return n/a
  20. */
  21. function __construct() {
  22. // add menu items
  23. add_action( 'admin_menu', array( $this, 'admin_menu' ), 99, 0 );
  24. }
  25. /*
  26. * admin_menu
  27. *
  28. * description
  29. *
  30. * @type function
  31. * @date 24/02/2014
  32. * @since 5.0.0
  33. *
  34. * @param
  35. * @return
  36. */
  37. function admin_menu() {
  38. // vars
  39. $pages = acf_get_options_pages();
  40. // bail early if no pages
  41. if ( empty( $pages ) ) {
  42. return;
  43. }
  44. // loop
  45. foreach ( $pages as $page ) {
  46. // vars
  47. $slug = '';
  48. // parent
  49. if ( empty( $page['parent_slug'] ) ) {
  50. $slug = add_menu_page( $page['page_title'], $page['menu_title'], $page['capability'], $page['menu_slug'], array( $this, 'html' ), $page['icon_url'], $page['position'] );
  51. // child
  52. } else {
  53. $slug = add_submenu_page( $page['parent_slug'], $page['page_title'], $page['menu_title'], $page['capability'], $page['menu_slug'], array( $this, 'html' ), $page['position'] );
  54. }
  55. // actions
  56. add_action( "load-{$slug}", array( $this, 'admin_load' ) );
  57. }
  58. }
  59. /*
  60. * load
  61. *
  62. * description
  63. *
  64. * @type function
  65. * @date 2/02/13
  66. * @since 3.6
  67. *
  68. * @param $post_id (int)
  69. * @return $post_id (int)
  70. */
  71. function admin_load() {
  72. // globals
  73. global $plugin_page;
  74. // vars
  75. $this->page = acf_get_options_page( $plugin_page );
  76. // get post_id (allow lang modification)
  77. $this->page['post_id'] = acf_get_valid_post_id( $this->page['post_id'] );
  78. // verify and remove nonce
  79. if ( acf_verify_nonce( 'options' ) ) {
  80. // save data
  81. if ( acf_validate_save_post( true ) ) {
  82. // set autoload
  83. acf_update_setting( 'autoload', $this->page['autoload'] );
  84. // save
  85. acf_save_post( $this->page['post_id'] );
  86. // redirect
  87. wp_redirect( add_query_arg( array( 'message' => '1' ) ) );
  88. exit;
  89. }
  90. }
  91. // load acf scripts
  92. acf_enqueue_scripts();
  93. // actions
  94. add_action( 'acf/input/admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) );
  95. add_action( 'acf/input/admin_head', array( $this, 'admin_head' ) );
  96. // add columns support
  97. add_screen_option(
  98. 'layout_columns',
  99. array(
  100. 'max' => 2,
  101. 'default' => 2,
  102. )
  103. );
  104. }
  105. /*
  106. * admin_enqueue_scripts
  107. *
  108. * This function will enqueue the 'post.js' script which adds support for 'Screen Options' column toggle
  109. *
  110. * @type function
  111. * @date 23/03/2016
  112. * @since 5.3.2
  113. *
  114. * @param
  115. * @return
  116. */
  117. function admin_enqueue_scripts() {
  118. wp_enqueue_script( 'post' );
  119. }
  120. /*
  121. * admin_head
  122. *
  123. * This action will find and add field groups to the current edit page
  124. *
  125. * @type action (admin_head)
  126. * @date 23/06/12
  127. * @since 3.1.8
  128. *
  129. * @param n/a
  130. * @return n/a
  131. */
  132. function admin_head() {
  133. // get field groups
  134. $field_groups = acf_get_field_groups(
  135. array(
  136. 'options_page' => $this->page['menu_slug'],
  137. )
  138. );
  139. // notices
  140. if ( ! empty( $_GET['message'] ) && $_GET['message'] == '1' ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Used to display a notice.
  141. acf_add_admin_notice( $this->page['updated_message'], 'success' );
  142. }
  143. // add submit div
  144. add_meta_box( 'submitdiv', __( 'Publish', 'acf' ), array( $this, 'postbox_submitdiv' ), 'acf_options_page', 'side', 'high' );
  145. if ( empty( $field_groups ) ) {
  146. acf_add_admin_notice( sprintf( __( 'No Custom Field Groups found for this options page. <a href="%s">Create a Custom Field Group</a>', 'acf' ), admin_url( 'post-new.php?post_type=acf-field-group' ) ), 'warning' );
  147. } else {
  148. foreach ( $field_groups as $i => $field_group ) {
  149. // vars
  150. $id = "acf-{$field_group['key']}";
  151. $title = $field_group['title'];
  152. $context = $field_group['position'];
  153. $priority = 'high';
  154. $args = array( 'field_group' => $field_group );
  155. // tweaks to vars
  156. if ( $context == 'acf_after_title' ) {
  157. $context = 'normal';
  158. } elseif ( $context == 'side' ) {
  159. $priority = 'core';
  160. }
  161. // filter for 3rd party customization
  162. $priority = apply_filters( 'acf/input/meta_box_priority', $priority, $field_group );
  163. // add meta box
  164. add_meta_box( $id, acf_esc_html( $title ), array( $this, 'postbox_acf' ), 'acf_options_page', $context, $priority, $args );
  165. }
  166. // foreach
  167. }
  168. // if
  169. }
  170. /*
  171. * postbox_submitdiv
  172. *
  173. * This function will render the submitdiv metabox
  174. *
  175. * @type function
  176. * @date 23/03/2016
  177. * @since 5.3.2
  178. *
  179. * @param n/a
  180. * @return n/a
  181. */
  182. function postbox_submitdiv( $post, $args ) {
  183. /**
  184. * Fires before the major-publishing-actions div.
  185. *
  186. * @date 24/9/18
  187. * @since 5.7.7
  188. *
  189. * @param array $page The current options page.
  190. */
  191. do_action( 'acf/options_page/submitbox_before_major_actions', $this->page );
  192. ?>
  193. <div id="major-publishing-actions">
  194. <div id="publishing-action">
  195. <span class="spinner"></span>
  196. <input type="submit" accesskey="p" value="<?php echo $this->page['update_button']; ?>" class="button button-primary button-large" id="publish" name="publish">
  197. </div>
  198. <?php
  199. /**
  200. * Fires before the major-publishing-actions div.
  201. *
  202. * @date 24/9/18
  203. * @since 5.7.7
  204. *
  205. * @param array $page The current options page.
  206. */
  207. do_action( 'acf/options_page/submitbox_major_actions', $this->page );
  208. ?>
  209. <div class="clear"></div>
  210. </div>
  211. <?php
  212. }
  213. /**
  214. * Renders a postbox on an ACF options page.
  215. *
  216. * @date 24/02/2014
  217. * @since 5.0.0
  218. *
  219. * @param object $post
  220. * @param array $args
  221. *
  222. * @return void
  223. */
  224. function postbox_acf( $post, $args ) {
  225. $id = $args['id'];
  226. $field_group = $args['args']['field_group'];
  227. // vars
  228. $o = array(
  229. 'id' => $id,
  230. 'key' => $field_group['key'],
  231. 'style' => $field_group['style'],
  232. 'label' => $field_group['label_placement'],
  233. 'editLink' => '',
  234. 'editTitle' => __( 'Edit field group', 'acf' ),
  235. 'visibility' => true,
  236. );
  237. // edit_url
  238. if ( $field_group['ID'] && acf_current_user_can_admin() ) {
  239. $o['editLink'] = admin_url( 'post.php?post=' . $field_group['ID'] . '&action=edit' );
  240. }
  241. // load fields
  242. $fields = acf_get_fields( $field_group );
  243. // render
  244. acf_render_fields( $fields, $this->page['post_id'], 'div', $field_group['instruction_placement'] );
  245. ?>
  246. <script type="text/javascript">
  247. if( typeof acf !== 'undefined' ) {
  248. acf.newPostbox(<?php echo json_encode( $o ); ?>);
  249. }
  250. </script>
  251. <?php
  252. }
  253. /*
  254. * html
  255. *
  256. * @description:
  257. * @since: 2.0.4
  258. * @created: 5/12/12
  259. */
  260. function html() {
  261. // load view
  262. acf_get_view( dirname( __FILE__ ) . '/views/html-options-page.php', $this->page );
  263. }
  264. }
  265. // initialize
  266. new acf_admin_options_page();
  267. endif;
  268. ?>