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

/wp-content/plugins/advanced-custom-fields-pro/admin/field-groups.php

https://gitlab.com/sihabudinahmad/asppi
PHP | 671 lines | 233 code | 218 blank | 220 comment | 32 complexity | f649ec4e90645bfdd1fc4cd5fd2a941c MD5 | raw file
  1. <?php
  2. /*
  3. * ACF Admin Field Groups Class
  4. *
  5. * All the logic for editing a list of field groups
  6. *
  7. * @class acf_admin_field_groups
  8. * @package ACF
  9. * @subpackage Admin
  10. */
  11. if( ! class_exists('acf_admin_field_groups') ) :
  12. class acf_admin_field_groups {
  13. // vars
  14. var $url = 'edit.php?post_type=acf-field-group',
  15. $sync = array();
  16. /*
  17. * __construct
  18. *
  19. * This function will setup the class functionality
  20. *
  21. * @type function
  22. * @date 5/03/2014
  23. * @since 5.0.0
  24. *
  25. * @param n/a
  26. * @return n/a
  27. */
  28. function __construct() {
  29. // actions
  30. add_action('current_screen', array($this, 'current_screen'));
  31. add_action('trashed_post', array($this, 'trashed_post'));
  32. add_action('untrashed_post', array($this, 'untrashed_post'));
  33. add_action('deleted_post', array($this, 'deleted_post'));
  34. }
  35. /*
  36. * current_screen
  37. *
  38. * This function is fired when loading the admin page before HTML has been rendered.
  39. *
  40. * @type action (current_screen)
  41. * @date 21/07/2014
  42. * @since 5.0.0
  43. *
  44. * @param n/a
  45. * @return n/a
  46. */
  47. function current_screen() {
  48. // validate screen
  49. if( !acf_is_screen('edit-acf-field-group') ) {
  50. return;
  51. }
  52. // check stuff
  53. $this->check_duplicate();
  54. $this->check_sync();
  55. // actions
  56. add_action('admin_footer', array($this, 'admin_footer'));
  57. // columns
  58. add_filter('manage_edit-acf-field-group_columns', array($this, 'field_group_columns'), 10, 1);
  59. add_action('manage_acf-field-group_posts_custom_column', array($this, 'field_group_columns_html'), 10, 2);
  60. }
  61. /*
  62. * check_duplicate
  63. *
  64. * This function will check for any $_GET data to duplicate
  65. *
  66. * @type function
  67. * @date 17/10/13
  68. * @since 5.0.0
  69. *
  70. * @param n/a
  71. * @return n/a
  72. */
  73. function check_duplicate() {
  74. // message
  75. if( $ids = acf_maybe_get($_GET, 'acfduplicatecomplete') ) {
  76. // explode
  77. $ids = explode(',', $ids);
  78. $total = count($ids);
  79. if( $total == 1 ) {
  80. acf_add_admin_notice( sprintf(__('Field group duplicated. %s', 'acf'), '<a href="' . get_edit_post_link($ids[0]) . '">' . get_the_title($ids[0]) . '</a>') );
  81. } else {
  82. acf_add_admin_notice( sprintf(_n( '%s field group duplicated.', '%s field groups duplicated.', $total, 'acf' ), $total) );
  83. }
  84. }
  85. // import field group
  86. if( $id = acf_maybe_get($_GET, 'acfduplicate') ) {
  87. // validate
  88. check_admin_referer('bulk-posts');
  89. // duplicate
  90. $field_group = acf_duplicate_field_group( $id );
  91. // redirect
  92. wp_redirect( admin_url( $this->url . '&acfduplicatecomplete=' . $field_group['ID'] ) );
  93. exit;
  94. } elseif( acf_maybe_get($_GET, 'action2') === 'acfduplicate' ) {
  95. // validate
  96. check_admin_referer('bulk-posts');
  97. // get ids
  98. $ids = acf_maybe_get($_GET, 'post');
  99. if( !empty($ids) ) {
  100. // vars
  101. $new_ids = array();
  102. foreach( $ids as $id ) {
  103. // duplicate
  104. $field_group = acf_duplicate_field_group( $id );
  105. // increase counter
  106. $new_ids[] = $field_group['ID'];
  107. }
  108. // redirect
  109. wp_redirect( admin_url( $this->url . '&acfduplicatecomplete=' . implode(',', $new_ids)) );
  110. exit;
  111. }
  112. }
  113. }
  114. /*
  115. * check_sync
  116. *
  117. * This function will check for any $_GET data to sync
  118. *
  119. * @type function
  120. * @date 9/12/2014
  121. * @since 5.1.5
  122. *
  123. * @param n/a
  124. * @return n/a
  125. */
  126. function check_sync() {
  127. // message
  128. if( $ids = acf_maybe_get($_GET, 'acfsynccomplete') ) {
  129. // explode
  130. $ids = explode(',', $ids);
  131. $total = count($ids);
  132. if( $total == 1 ) {
  133. acf_add_admin_notice( sprintf(__('Field group synchronised. %s', 'acf'), '<a href="' . get_edit_post_link($ids[0]) . '">' . get_the_title($ids[0]) . '</a>') );
  134. } else {
  135. acf_add_admin_notice( sprintf(_n( '%s field group synchronised.', '%s field groups synchronised.', $total, 'acf' ), $total) );
  136. }
  137. }
  138. // vars
  139. $groups = acf_get_field_groups();
  140. // bail early if no field groups
  141. if( empty($groups) ) {
  142. return;
  143. }
  144. // find JSON field groups which have not yet been imported
  145. foreach( $groups as $group ) {
  146. // vars
  147. $local = acf_maybe_get($group, 'local', false);
  148. $modified = acf_maybe_get($group, 'modified', 0);
  149. $private = acf_maybe_get($group, 'private', false);
  150. // ignore DB / PHP / private field groups
  151. if( $local !== 'json' || $private ) {
  152. // do nothing
  153. } elseif( !$group['ID'] ) {
  154. $this->sync[ $group['key'] ] = $group;
  155. } elseif( $modified && $modified > get_post_modified_time('U', true, $group['ID'], true) ) {
  156. $this->sync[ $group['key'] ] = $group;
  157. }
  158. }
  159. // bail if no sync needed
  160. if( empty($this->sync) ) {
  161. return;
  162. }
  163. // import field group
  164. if( $key = acf_maybe_get($_GET, 'acfsync') ) {
  165. // validate
  166. check_admin_referer('bulk-posts');
  167. // append fields
  168. if( acf_have_local_fields( $key ) ) {
  169. $this->sync[ $key ]['fields'] = acf_get_local_fields( $key );
  170. }
  171. // import
  172. $field_group = acf_import_field_group( $this->sync[ $key ] );
  173. // redirect
  174. wp_redirect( admin_url( $this->url . '&acfsynccomplete=' . $field_group['ID'] ) );
  175. exit;
  176. } elseif( acf_maybe_get($_GET, 'action2') === 'acfsync' ) {
  177. // validate
  178. check_admin_referer('bulk-posts');
  179. // get ids
  180. $keys = acf_maybe_get($_GET, 'post');
  181. if( !empty($keys) ) {
  182. // vars
  183. $new_ids = array();
  184. foreach( $keys as $key ) {
  185. // append fields
  186. if( acf_have_local_fields( $key ) ) {
  187. $this->sync[ $key ]['fields'] = acf_get_local_fields( $key );
  188. }
  189. // import
  190. $field_group = acf_import_field_group( $this->sync[ $key ] );
  191. // append
  192. $new_ids[] = $field_group['ID'];
  193. }
  194. // redirect
  195. wp_redirect( admin_url( $this->url . '&acfsynccomplete=' . implode(',', $new_ids)) );
  196. exit;
  197. }
  198. }
  199. // filters
  200. add_filter('views_edit-acf-field-group', array($this, 'list_table_views'));
  201. }
  202. /*
  203. * list_table_views
  204. *
  205. * This function will add an extra link for JSON in the field group list table
  206. *
  207. * @type function
  208. * @date 3/12/2014
  209. * @since 5.1.5
  210. *
  211. * @param $views (array)
  212. * @return $views
  213. */
  214. function list_table_views( $views ) {
  215. // vars
  216. $class = '';
  217. $total = count($this->sync);
  218. // active
  219. if( acf_maybe_get($_GET, 'post_status') === 'sync' ) {
  220. // actions
  221. add_action('admin_footer', array($this, 'sync_admin_footer'));
  222. // set active class
  223. $class = ' class="current"';
  224. // global
  225. global $wp_list_table;
  226. // update pagination
  227. $wp_list_table->set_pagination_args( array(
  228. 'total_items' => $total,
  229. 'total_pages' => 1,
  230. 'per_page' => $total
  231. ));
  232. }
  233. // add view
  234. $views['json'] = '<a' . $class . ' href="' . admin_url($this->url . '&post_status=sync') . '">' . __('Sync available', 'acf') . ' <span class="count">(' . $total . ')</span></a>';
  235. // return
  236. return $views;
  237. }
  238. /*
  239. * trashed_post
  240. *
  241. * This function is run when a post object is sent to the trash
  242. *
  243. * @type action (trashed_post)
  244. * @date 8/01/2014
  245. * @since 5.0.0
  246. *
  247. * @param $post_id (int)
  248. * @return n/a
  249. */
  250. function trashed_post( $post_id ) {
  251. // validate post type
  252. if( get_post_type($post_id) != 'acf-field-group' ) {
  253. return;
  254. }
  255. // trash field group
  256. acf_trash_field_group( $post_id );
  257. }
  258. /*
  259. * untrashed_post
  260. *
  261. * This function is run when a post object is restored from the trash
  262. *
  263. * @type action (untrashed_post)
  264. * @date 8/01/2014
  265. * @since 5.0.0
  266. *
  267. * @param $post_id (int)
  268. * @return n/a
  269. */
  270. function untrashed_post( $post_id ) {
  271. // validate post type
  272. if( get_post_type($post_id) != 'acf-field-group' ) {
  273. return;
  274. }
  275. // trash field group
  276. acf_untrash_field_group( $post_id );
  277. }
  278. /*
  279. * deleted_post
  280. *
  281. * This function is run when a post object is deleted from the trash
  282. *
  283. * @type action (deleted_post)
  284. * @date 8/01/2014
  285. * @since 5.0.0
  286. *
  287. * @param $post_id (int)
  288. * @return n/a
  289. */
  290. function deleted_post( $post_id ) {
  291. // validate post type
  292. if( get_post_type($post_id) != 'acf-field-group' ) {
  293. return;
  294. }
  295. // trash field group
  296. acf_delete_field_group( $post_id );
  297. }
  298. /*
  299. * field_group_columns
  300. *
  301. * This function will customize the columns for the field group table
  302. *
  303. * @type filter (manage_edit-acf-field-group_columns)
  304. * @date 28/09/13
  305. * @since 5.0.0
  306. *
  307. * @param $columns (array)
  308. * @return $columns (array)
  309. */
  310. function field_group_columns( $columns ) {
  311. $columns = array(
  312. 'cb' => '<input type="checkbox" />',
  313. 'title' => __('Title', 'acf'),
  314. 'fields' => __('Fields', 'acf'),
  315. );
  316. return $columns;
  317. }
  318. /*
  319. * field_group_columns_html
  320. *
  321. * This function will render the HTML for each table cell
  322. *
  323. * @type action (manage_acf-field-group_posts_custom_column)
  324. * @date 28/09/13
  325. * @since 5.0.0
  326. *
  327. * @param $column (string)
  328. * @param $post_id (int)
  329. * @return n/a
  330. */
  331. function field_group_columns_html( $column, $post_id ) {
  332. // vars
  333. if( $column == 'fields' ) {
  334. echo acf_get_field_count( $post_id );
  335. }
  336. }
  337. /*
  338. * admin_footer
  339. *
  340. * This function will render extra HTML onto the page
  341. *
  342. * @type action (admin_footer)
  343. * @date 23/06/12
  344. * @since 3.1.8
  345. *
  346. * @param n/a
  347. * @return n/a
  348. */
  349. function admin_footer() {
  350. // vars
  351. $www = 'http://www.advancedcustomfields.com/resources/';
  352. ?><script type="text/html" id="tmpl-acf-col-side">
  353. <div id="acf-col-side">
  354. <div class="acf-box">
  355. <div class="inner">
  356. <h2><?php echo acf_get_setting('name'); ?> <?php echo acf_get_setting('version'); ?></h2>
  357. <h3><?php _e("Changelog",'acf'); ?></h3>
  358. <p><?php _e("See what's new in",'acf'); ?> <a href="<?php echo admin_url('edit.php?post_type=acf-field-group&page=acf-settings-info&tab=changelog'); ?>"><?php _e("version",'acf'); ?> <?php echo acf_get_setting('version'); ?></a>
  359. <h3><?php _e("Resources",'acf'); ?></h3>
  360. <ul>
  361. <li><a href="<?php echo $www; ?>#getting-started" target="_blank"><?php _e("Getting Started",'acf'); ?></a></li>
  362. <li><a href="<?php echo $www; ?>#updates" target="_blank"><?php _e("Updates",'acf'); ?></a></li>
  363. <li><a href="<?php echo $www; ?>#field-types" target="_blank"><?php _e("Field Types",'acf'); ?></a></li>
  364. <li><a href="<?php echo $www; ?>#functions" target="_blank"><?php _e("Functions",'acf'); ?></a></li>
  365. <li><a href="<?php echo $www; ?>#actions" target="_blank"><?php _e("Actions",'acf'); ?></a></li>
  366. <li><a href="<?php echo $www; ?>#filters" target="_blank"><?php _e("Filters",'acf'); ?></a></li>
  367. <li><a href="<?php echo $www; ?>#how-to" target="_blank"><?php _e("'How to' guides",'acf'); ?></a></li>
  368. <li><a href="<?php echo $www; ?>#tutorials" target="_blank"><?php _e("Tutorials",'acf'); ?></a></li>
  369. </ul>
  370. </div>
  371. <div class="footer footer-blue">
  372. <ul class="acf-hl">
  373. <li><?php _e("Created by",'acf'); ?> Elliot Condon</li>
  374. </ul>
  375. </div>
  376. </div>
  377. </div>
  378. </script>
  379. <script type="text/javascript">
  380. (function($){
  381. // wrap
  382. $('#wpbody .wrap').attr('id', 'acf-field-group-list');
  383. // wrap column main
  384. $('#acf-field-group-list').wrapInner('<div id="acf-col-main" />');
  385. // add column side
  386. $('#acf-field-group-list').prepend( $('#tmpl-acf-col-side').html() );
  387. // wrap columns
  388. $('#acf-field-group-list').wrapInner('<div id="acf-col-wrap" class="acf-clearfix" />');
  389. // take out h2 + icon
  390. $('#acf-col-main > .icon32').insertBefore('#acf-col-wrap');
  391. $('#acf-col-main > h2').insertBefore('#acf-col-wrap');
  392. // modify row actions
  393. $('#acf-field-group-list .row-actions').each(function(){
  394. // vars
  395. var id = $(this).closest('tr').attr('id').replace('post-', ''),
  396. $span = $('<span class="acf-duplicate-field-group"><a title="<?php _e('Duplicate this item', 'acf'); ?>" href="<?php echo admin_url($this->url . '&acfduplicate='); ?>' + id + '&_wpnonce=<?php echo wp_create_nonce('bulk-posts'); ?>"><?php _e('Duplicate', 'acf'); ?></a> | </span>');
  397. $(this).find('.inline').replaceWith( $span );
  398. });
  399. // modify bulk actions
  400. $('#bulk-action-selector-bottom option[value="edit"]').attr('value','acfduplicate').text('<?php _e( 'Duplicate', 'acf' ); ?>');
  401. })(jQuery);
  402. </script><?php
  403. }
  404. /*
  405. * sync_admin_footer
  406. *
  407. * This function will render extra HTML onto the page
  408. *
  409. * @type action (admin_footer)
  410. * @date 23/06/12
  411. * @since 3.1.8
  412. *
  413. * @param n/a
  414. * @return n/a
  415. */
  416. function sync_admin_footer() {
  417. // vars
  418. $i = -1;
  419. ?>
  420. <script type="text/html" id="tmpl-acf-json-tbody">
  421. <?php foreach( $this->sync as $group ): $i++; ?>
  422. <tr <?php if($i%2 == 0): ?>class="alternate"<?php endif; ?>>
  423. <th class="check-column" scope="row">
  424. <label for="cb-select-<?php echo $group['key']; ?>" class="screen-reader-text"><?php printf( __( 'Select %s', 'acf' ), $group['title'] ); ?></label>
  425. <input type="checkbox" value="<?php echo $group['key']; ?>" name="post[]" id="cb-select-<?php echo $group['key']; ?>">
  426. </th>
  427. <td class="post-title page-title column-title">
  428. <strong><?php echo $group['title']; ?></strong>
  429. <div class="row-actions">
  430. <span class="import"><a title="<?php echo esc_attr( __('Synchronise field group', 'acf') ); ?>" href="<?php echo admin_url($this->url . '&post_status=sync&acfsync=' . $group['key'] . '&_wpnonce=' . wp_create_nonce('bulk-posts')); ?>"><?php _e( 'Sync', 'acf' ); ?></a></span>
  431. </div>
  432. </td>
  433. <td class="fields column-fields"><?php echo acf_count_local_fields( $group['key'] ); ?></td>
  434. </tr>
  435. <?php endforeach; ?>
  436. </script>
  437. <script type="text/javascript">
  438. (function($){
  439. // update table HTML
  440. $('#the-list').html( $('#tmpl-acf-json-tbody').html() );
  441. // modify bulk actions
  442. $('#bulk-action-selector-bottom option[value="acfduplicate"]').attr('value','acfsync').text('<?php _e( 'Sync', 'acf' ); ?>');
  443. $('#bulk-action-selector-bottom option[value="trash"]').remove();
  444. })(jQuery);
  445. </script>
  446. <?php
  447. }
  448. }
  449. new acf_admin_field_groups();
  450. endif;
  451. ?>