Brak opisu

Pages.php 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. /*******************************************************************************
  3. * Copyright (c) 2019, Code Atlantic LLC
  4. ******************************************************************************/
  5. if ( ! defined( 'ABSPATH' ) ) {
  6. exit;
  7. }
  8. /**
  9. * Class PUM_Admin_Pages
  10. *
  11. * @since 1.7.0
  12. */
  13. class PUM_Admin_Pages {
  14. /**
  15. * @var array
  16. */
  17. public static $pages = array();
  18. /**
  19. *
  20. */
  21. public static function init() {
  22. add_action( 'admin_menu', array( __CLASS__, 'register_pages' ) );
  23. add_action( 'admin_head', array( __CLASS__, 'reorder_admin_submenu' ) );
  24. }
  25. /**
  26. * Returns the requested pages handle.
  27. *
  28. * @param $key
  29. *
  30. * @return bool|mixed
  31. */
  32. public static function get_page( $key ) {
  33. return isset( self::$pages[ $key ] ) ? self::$pages[ $key ] : false;
  34. }
  35. /**
  36. * Creates the admin submenu pages under the Popup Maker menu and assigns their
  37. * links to global variables
  38. */
  39. public static function register_pages() {
  40. $admin_pages = apply_filters( 'pum_admin_pages', array(
  41. 'subscribers' => array(
  42. 'page_title' => __( 'Subscribers', 'popup-maker' ),
  43. 'capability' => 'manage_options',
  44. 'callback' => array( 'PUM_Admin_Subscribers', 'page' ),
  45. ),
  46. 'settings' => array(
  47. 'page_title' => __( 'Settings', 'popup-maker' ),
  48. 'capability' => 'manage_options',
  49. 'callback' => array( 'PUM_Admin_Settings', 'page' ),
  50. ),
  51. 'extensions' => array(
  52. 'page_title' => __( 'Upgrade', 'popup-maker' ),
  53. 'capability' => 'edit_posts',
  54. 'callback' => array( 'PUM_Admin_Extend', 'page' ),
  55. ),
  56. 'support' => array(
  57. 'page_title' => __( 'Help & Support', 'popup-maker' ),
  58. 'capability' => 'edit_posts',
  59. 'callback' => array( 'PUM_Admin_Support', 'page' ),
  60. ),
  61. 'tools' => array(
  62. 'page_title' => __( 'Tools', 'popup-maker' ),
  63. 'capability' => 'manage_options',
  64. 'callback' => array( 'PUM_Admin_Tools', 'page' ),
  65. ),
  66. ) );
  67. foreach ( $admin_pages as $key => $page ) {
  68. $page = wp_parse_args( $page, array(
  69. 'parent_slug' => 'edit.php?post_type=popup',
  70. 'page_title' => '',
  71. 'menu_title' => '',
  72. 'capability' => 'manage_options',
  73. 'menu_slug' => '',
  74. 'callback' => '',
  75. ) );
  76. // Backward compatibility.
  77. $page['capability'] = apply_filters( 'popmake_admin_submenu_' . $key . '_capability', $page['capability'] );
  78. if ( empty( $page['menu_slug'] ) ) {
  79. $page['menu_slug'] = 'pum-' . $key;
  80. }
  81. if ( ! empty( $page['page_title'] ) && empty( $page['menu_title'] ) ) {
  82. $page['menu_title'] = $page['page_title'];
  83. } elseif ( ! empty( $page['menu_title'] ) && empty( $page['page_title'] ) ) {
  84. $page['page_title'] = $page['menu_title'];
  85. }
  86. self::$pages[ $key ] = add_submenu_page( $page['parent_slug'], $page['page_title'], $page['menu_title'], $page['capability'], $page['menu_slug'], $page['callback'] );
  87. // For backward compatibility.
  88. $GLOBALS[ "popmake_" . $key . "_page" ] = self::$pages[ $key ];
  89. }
  90. // Add shortcut to theme editor from Appearance menu.
  91. add_theme_page( __( 'Popup Themes', 'popup-maker' ), __( 'Popup Themes', 'popup-maker' ), 'edit_posts', 'edit.php?post_type=popup_theme' );
  92. }
  93. /**
  94. * Submenu filter function. Tested with Wordpress 4.1.1
  95. * Sort and order submenu positions to match our custom order.
  96. *
  97. * @since 1.4
  98. */
  99. public static function reorder_admin_submenu() {
  100. global $submenu;
  101. if ( isset( $submenu['edit.php?post_type=popup'] ) ) {
  102. // Sort the menu according to your preferences
  103. usort( $submenu['edit.php?post_type=popup'], array( __CLASS__, 'reorder_submenu_array' ) );
  104. }
  105. }
  106. /**
  107. * Reorders the submenu by title.
  108. *
  109. * Forces $first_pages to load in order at the beginning of the menu
  110. * and $last_pages to load in order at the end. All remaining menu items will
  111. * go out in generic order.
  112. *
  113. * @since 1.4
  114. *
  115. * @param $a
  116. * @param $b
  117. *
  118. * @return int
  119. */
  120. public static function reorder_submenu_array( $a, $b ) {
  121. $first_pages = apply_filters( 'pum_admin_submenu_first_pages', array(
  122. __( 'All Popups', 'popup-maker' ),
  123. __( 'Add New', 'popup-maker' ),
  124. __( 'All Themes', 'popup-maker' ),
  125. __( 'Categories', 'popup-maker' ),
  126. __( 'Tags', 'popup-maker' ),
  127. ) );
  128. $last_pages = apply_filters( 'pum_admin_submenu_last_pages', array(
  129. __( 'Extend', 'popup-maker' ),
  130. __( 'Settings', 'popup-maker' ),
  131. __( 'Tools', 'popup-maker' ),
  132. __( 'Support Forum', 'popup-maker' ),
  133. __( 'Account', 'popup-maker' ),
  134. __( 'Contact Us', 'popup-maker' ),
  135. __( 'Help & Support', 'popup-maker' ),
  136. ) );
  137. $a_val = strip_tags( $a[0], false );
  138. $b_val = strip_tags( $b[0], false );
  139. // Sort First Page Keys.
  140. if ( in_array( $a_val, $first_pages ) && ! in_array( $b_val, $first_pages ) ) {
  141. return - 1;
  142. } elseif ( ! in_array( $a_val, $first_pages ) && in_array( $b_val, $first_pages ) ) {
  143. return 1;
  144. } elseif ( in_array( $a_val, $first_pages ) && in_array( $b_val, $first_pages ) ) {
  145. $a_key = array_search( $a_val, $first_pages );
  146. $b_key = array_search( $b_val, $first_pages );
  147. return ( $a_key < $b_key ) ? - 1 : 1;
  148. }
  149. // Sort Last Page Keys.
  150. if ( in_array( $a_val, $last_pages ) && ! in_array( $b_val, $last_pages ) ) {
  151. return 1;
  152. } elseif ( ! in_array( $a_val, $last_pages ) && in_array( $b_val, $last_pages ) ) {
  153. return - 1;
  154. } elseif ( in_array( $a_val, $last_pages ) && in_array( $b_val, $last_pages ) ) {
  155. $a_key = array_search( $a_val, $last_pages );
  156. $b_key = array_search( $b_val, $last_pages );
  157. return ( $a_key < $b_key ) ? - 1 : 1;
  158. }
  159. // Sort remaining keys
  160. return $a > $b ? 1 : - 1;
  161. }
  162. }