admin-tools.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) {
  3. exit; // Exit if accessed directly
  4. }
  5. if ( ! class_exists( 'acf_admin_tools' ) ) :
  6. class acf_admin_tools {
  7. /** @var array Contains an array of admin tool instances */
  8. var $tools = array();
  9. /** @var string The active tool */
  10. var $active = '';
  11. /**
  12. * __construct
  13. *
  14. * This function will setup the class functionality
  15. *
  16. * @date 10/10/17
  17. * @since 5.6.3
  18. *
  19. * @param n/a
  20. * @return n/a
  21. */
  22. function __construct() {
  23. // actions
  24. add_action( 'admin_menu', array( $this, 'admin_menu' ) );
  25. }
  26. /**
  27. * register_tool
  28. *
  29. * This function will store a tool tool class
  30. *
  31. * @date 10/10/17
  32. * @since 5.6.3
  33. *
  34. * @param string $class
  35. * @return n/a
  36. */
  37. function register_tool( $class ) {
  38. $instance = new $class();
  39. $this->tools[ $instance->name ] = $instance;
  40. }
  41. /**
  42. * get_tool
  43. *
  44. * This function will return a tool tool class
  45. *
  46. * @date 10/10/17
  47. * @since 5.6.3
  48. *
  49. * @param string $name
  50. * @return n/a
  51. */
  52. function get_tool( $name ) {
  53. return isset( $this->tools[ $name ] ) ? $this->tools[ $name ] : null;
  54. }
  55. /**
  56. * get_tools
  57. *
  58. * This function will return an array of all tools
  59. *
  60. * @date 10/10/17
  61. * @since 5.6.3
  62. *
  63. * @param n/a
  64. * @return array
  65. */
  66. function get_tools() {
  67. return $this->tools;
  68. }
  69. /*
  70. * admin_menu
  71. *
  72. * This function will add the ACF menu item to the WP admin
  73. *
  74. * @type action (admin_menu)
  75. * @date 28/09/13
  76. * @since 5.0.0
  77. *
  78. * @param n/a
  79. * @return n/a
  80. */
  81. function admin_menu() {
  82. // bail early if no show_admin
  83. if ( ! acf_get_setting( 'show_admin' ) ) {
  84. return;
  85. }
  86. // add page
  87. $page = add_submenu_page( 'edit.php?post_type=acf-field-group', __( 'Tools', 'acf' ), __( 'Tools', 'acf' ), acf_get_setting( 'capability' ), 'acf-tools', array( $this, 'html' ) );
  88. // actions
  89. add_action( 'load-' . $page, array( $this, 'load' ) );
  90. }
  91. /**
  92. * load
  93. *
  94. * description
  95. *
  96. * @date 10/10/17
  97. * @since 5.6.3
  98. *
  99. * @param n/a
  100. * @return n/a
  101. */
  102. function load() {
  103. // disable filters (default to raw data)
  104. acf_disable_filters();
  105. // include tools
  106. $this->include_tools();
  107. // check submit
  108. $this->check_submit();
  109. // load acf scripts
  110. acf_enqueue_scripts();
  111. }
  112. /**
  113. * include_tools
  114. *
  115. * description
  116. *
  117. * @date 10/10/17
  118. * @since 5.6.3
  119. *
  120. * @param n/a
  121. * @return n/a
  122. */
  123. function include_tools() {
  124. // include
  125. acf_include( 'includes/admin/tools/class-acf-admin-tool.php' );
  126. acf_include( 'includes/admin/tools/class-acf-admin-tool-export.php' );
  127. acf_include( 'includes/admin/tools/class-acf-admin-tool-import.php' );
  128. // action
  129. do_action( 'acf/include_admin_tools' );
  130. }
  131. /**
  132. * check_submit
  133. *
  134. * description
  135. *
  136. * @date 10/10/17
  137. * @since 5.6.3
  138. *
  139. * @param n/a
  140. * @return n/a
  141. */
  142. function check_submit() {
  143. // loop
  144. foreach ( $this->get_tools() as $tool ) {
  145. // load
  146. $tool->load();
  147. // submit
  148. if ( acf_verify_nonce( $tool->name ) ) {
  149. $tool->submit();
  150. }
  151. }
  152. }
  153. /**
  154. * html
  155. *
  156. * description
  157. *
  158. * @date 10/10/17
  159. * @since 5.6.3
  160. *
  161. * @param n/a
  162. * @return n/a
  163. */
  164. function html() {
  165. // vars
  166. $screen = get_current_screen();
  167. $active = acf_maybe_get_GET( 'tool' );
  168. // view
  169. $view = array(
  170. 'screen_id' => $screen->id,
  171. 'active' => $active,
  172. );
  173. // register metaboxes
  174. foreach ( $this->get_tools() as $tool ) {
  175. // check active
  176. if ( $active && $active !== $tool->name ) {
  177. continue;
  178. }
  179. // add metabox
  180. add_meta_box( 'acf-admin-tool-' . $tool->name, acf_esc_html( $tool->title ), array( $this, 'metabox_html' ), $screen->id, 'normal', 'default', array( 'tool' => $tool->name ) );
  181. }
  182. // view
  183. acf_get_view( 'html-admin-tools', $view );
  184. }
  185. /**
  186. * meta_box_html
  187. *
  188. * description
  189. *
  190. * @date 10/10/17
  191. * @since 5.6.3
  192. *
  193. * @param n/a
  194. * @return n/a
  195. */
  196. function metabox_html( $post, $metabox ) {
  197. // vars
  198. $tool = $this->get_tool( $metabox['args']['tool'] );
  199. ?>
  200. <form method="post">
  201. <?php $tool->html(); ?>
  202. <?php acf_nonce_input( $tool->name ); ?>
  203. </form>
  204. <?php
  205. }
  206. }
  207. // initialize
  208. acf()->admin_tools = new acf_admin_tools();
  209. endif; // class_exists check
  210. /*
  211. * acf_register_admin_tool
  212. *
  213. * alias of acf()->admin_tools->register_tool()
  214. *
  215. * @type function
  216. * @date 31/5/17
  217. * @since 5.6.0
  218. *
  219. * @param n/a
  220. * @return n/a
  221. */
  222. function acf_register_admin_tool( $class ) {
  223. return acf()->admin_tools->register_tool( $class );
  224. }
  225. /*
  226. * acf_get_admin_tools_url
  227. *
  228. * This function will return the admin URL to the tools page
  229. *
  230. * @type function
  231. * @date 31/5/17
  232. * @since 5.6.0
  233. *
  234. * @param n/a
  235. * @return n/a
  236. */
  237. function acf_get_admin_tools_url() {
  238. return admin_url( 'edit.php?post_type=acf-field-group&page=acf-tools' );
  239. }
  240. /*
  241. * acf_get_admin_tool_url
  242. *
  243. * This function will return the admin URL to the tools page
  244. *
  245. * @type function
  246. * @date 31/5/17
  247. * @since 5.6.0
  248. *
  249. * @param n/a
  250. * @return n/a
  251. */
  252. function acf_get_admin_tool_url( $tool = '' ) {
  253. return acf_get_admin_tools_url() . '&tool=' . $tool;
  254. }
  255. ?>