class-ad-groups-list.php
8 years ago
class-ad-type.php
7 years ago
class-licenses.php
7 years ago
class-list-filters.php
7 years ago
class-menu.php
8 years ago
class-meta-box.php
7 years ago
class-notices.php
7 years ago
class-options.php
9 years ago
class-overview-widgets.php
7 years ago
class-settings.php
8 years ago
class-shortcode-creator.php
9 years ago
notices.php
7 years ago
shortcode-creator-l10n.php
10 years ago
class-menu.php
251 lines
| 1 | <?php |
| 2 | defined( 'ABSPATH' ) || exit; |
| 3 | |
| 4 | class Advanced_Ads_Admin_Menu { |
| 5 | /** |
| 6 | * Instance of this class. |
| 7 | * |
| 8 | * @var object |
| 9 | */ |
| 10 | protected static $instance = null; |
| 11 | |
| 12 | /** |
| 13 | * Slug of the ad group page |
| 14 | * |
| 15 | * @since 1.0.0 |
| 16 | * @var string |
| 17 | */ |
| 18 | protected $ad_group_hook_suffix = null; |
| 19 | |
| 20 | private function __construct() { |
| 21 | // Add menu items |
| 22 | add_action( 'admin_menu', array($this, 'add_plugin_admin_menu') ); |
| 23 | add_action( 'admin_head', array( $this, 'highlight_menu_item' ) ); |
| 24 | |
| 25 | $this->plugin_slug = Advanced_Ads::get_instance()->get_plugin_slug(); |
| 26 | $this->post_type = constant( 'Advanced_Ads::POST_TYPE_SLUG' ); |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Return an instance of this class. |
| 31 | * |
| 32 | * @return object A single instance of this class. |
| 33 | */ |
| 34 | public static function get_instance() { |
| 35 | // If the single instance hasn't been set, set it now. |
| 36 | if ( null == self::$instance ) { |
| 37 | self::$instance = new self; |
| 38 | } |
| 39 | |
| 40 | return self::$instance; |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Register the administration menu for this plugin into the WordPress Dashboard menu. |
| 45 | * |
| 46 | * @since 1.0.0 |
| 47 | */ |
| 48 | public function add_plugin_admin_menu() { |
| 49 | |
| 50 | $has_ads = Advanced_Ads::get_number_of_ads(); |
| 51 | |
| 52 | // use the overview page only when there is an ad already |
| 53 | if( $has_ads ){ |
| 54 | add_menu_page( |
| 55 | __( 'Overview', 'advanced-ads' ), 'Advanced Ads', Advanced_Ads_Plugin::user_cap( 'advanced_ads_see_interface'), $this->plugin_slug, array($this, 'display_overview_page'), 'dashicons-chart-line', '58.74' |
| 56 | ); |
| 57 | } |
| 58 | // forward Ads link to new-ad page when there is no ad existing yet. |
| 59 | // the target to post-new.php needs the extra "new" or any other attribute, since the original add-ad link was removed by CSS using the exact href attribute as a selector |
| 60 | $target = ( ! $has_ads ) ? 'post-new.php?post_type=' . Advanced_Ads::POST_TYPE_SLUG . '&new=new' : 'edit.php?post_type=' . Advanced_Ads::POST_TYPE_SLUG; |
| 61 | add_submenu_page( |
| 62 | $this->plugin_slug, __( 'Ads', 'advanced-ads' ), __( 'Ads', 'advanced-ads' ), Advanced_Ads_Plugin::user_cap( 'advanced_ads_edit_ads'), $target |
| 63 | ); |
| 64 | |
| 65 | // display the main overview page as second item when we don’t have ads yet |
| 66 | if( ! $has_ads ){ |
| 67 | add_menu_page( |
| 68 | __( 'Overview', 'advanced-ads' ), 'Advanced Ads', Advanced_Ads_Plugin::user_cap( 'advanced_ads_see_interface'), $this->plugin_slug, array($this, 'display_overview_page'), 'dashicons-chart-line', '58.74' |
| 69 | ); |
| 70 | |
| 71 | add_submenu_page( |
| 72 | $this->plugin_slug, __( 'Overview', 'advanced-ads' ), __( 'Overview', 'advanced-ads' ), Advanced_Ads_Plugin::user_cap( 'advanced_ads_see_interface'), $this->plugin_slug, array($this, 'display_overview_page') |
| 73 | ); |
| 74 | } |
| 75 | |
| 76 | // hidden by css; not placed in 'options.php' in order to highlight the correct item, see the 'highlight_menu_item()' |
| 77 | if ( ! current_user_can( 'edit_posts' ) ) { |
| 78 | add_submenu_page( |
| 79 | $this->plugin_slug, __( 'Add New Ad', 'advanced-ads' ), __( 'New Ad', 'advanced-ads' ), Advanced_Ads_Plugin::user_cap( 'advanced_ads_edit_ads'), 'post-new.php?post_type=' . Advanced_Ads::POST_TYPE_SLUG |
| 80 | ); |
| 81 | } |
| 82 | |
| 83 | $this->ad_group_hook_suffix = add_submenu_page( |
| 84 | $this->plugin_slug, __( 'Ad Groups & Rotations', 'advanced-ads' ), __( 'Groups & Rotation', 'advanced-ads' ), Advanced_Ads_Plugin::user_cap( 'advanced_ads_edit_ads'), $this->plugin_slug . '-groups', array($this, 'ad_group_admin_page') |
| 85 | ); |
| 86 | |
| 87 | // add placements page |
| 88 | add_submenu_page( |
| 89 | $this->plugin_slug, __( 'Ad Placements', 'advanced-ads' ), __( 'Placements', 'advanced-ads' ), Advanced_Ads_Plugin::user_cap( 'advanced_ads_manage_placements'), $this->plugin_slug . '-placements', array($this, 'display_placements_page') |
| 90 | ); |
| 91 | // add settings page |
| 92 | Advanced_Ads_Admin::get_instance()->plugin_screen_hook_suffix = add_submenu_page( |
| 93 | $this->plugin_slug, __( 'Advanced Ads Settings', 'advanced-ads' ), __( 'Settings', 'advanced-ads' ), Advanced_Ads_Plugin::user_cap( 'advanced_ads_manage_options'), $this->plugin_slug . '-settings', array($this, 'display_plugin_settings_page') |
| 94 | ); |
| 95 | // add support page |
| 96 | /*add_submenu_page( |
| 97 | $this->plugin_slug, __( 'Support', 'advanced-ads' ), __( 'Support', 'advanced-ads' ), Advanced_Ads_Plugin::user_cap( 'advanced_ads_manage_options'), $this->plugin_slug . '-support', array($this, 'display_support_page') |
| 98 | );*/ |
| 99 | |
| 100 | /** |
| 101 | * since we forward the support link to the settings page, we need to add the menu item manually |
| 102 | * could break if WordPress changes the API at one point, but it didn’t do that for many years |
| 103 | */ |
| 104 | global $submenu; |
| 105 | if(current_user_can( Advanced_Ads_Plugin::user_cap( 'advanced_ads_manage_options') ) ){ |
| 106 | $submenu['advanced-ads'][] = array( |
| 107 | __('Support', 'advanced-ads' ), // title |
| 108 | Advanced_Ads_Plugin::user_cap( 'advanced_ads_manage_options'), // capability |
| 109 | admin_url( 'admin.php?page=advanced-ads-settings#top#support' ), |
| 110 | __('Support', 'advanced-ads' ), // not sure what this is, but it is in the API |
| 111 | ); |
| 112 | } |
| 113 | |
| 114 | // allows extensions to insert sub menu pages |
| 115 | do_action( 'advanced-ads-submenu-pages', $this->plugin_slug ); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Highlights the 'Advanced Ads->Ads' item in the menu when an ad edit page is open |
| 120 | * @see the 'parent_file' and the 'submenu_file' filters for reference |
| 121 | */ |
| 122 | public function highlight_menu_item() { |
| 123 | global $parent_file, $submenu_file, $post_type; |
| 124 | if ( $post_type === $this->post_type ) { |
| 125 | $parent_file = $this->plugin_slug; |
| 126 | $submenu_file = 'edit.php?post_type=' . $this->post_type; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Render the overview page |
| 132 | * |
| 133 | * @since 1.2.2 |
| 134 | */ |
| 135 | public function display_overview_page() { |
| 136 | |
| 137 | include ADVADS_BASE_PATH . 'admin/views/overview.php'; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Render the settings page |
| 142 | * |
| 143 | * @since 1.0.0 |
| 144 | */ |
| 145 | public function display_plugin_settings_page() { |
| 146 | include ADVADS_BASE_PATH . 'admin/views/settings.php'; |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Render the placements page |
| 151 | * |
| 152 | * @since 1.1.0 |
| 153 | */ |
| 154 | public function display_placements_page() { |
| 155 | $placement_types = Advanced_Ads_Placements::get_placement_types(); |
| 156 | $placements = Advanced_Ads::get_ad_placements_array(); // -TODO use model |
| 157 | $items = Advanced_Ads_Placements::items_for_select(); |
| 158 | // load ads and groups for select field |
| 159 | |
| 160 | // display view |
| 161 | include ADVADS_BASE_PATH . 'admin/views/placements.php'; |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Render the support page |
| 166 | * |
| 167 | * @since 1.6.8.1 |
| 168 | */ |
| 169 | public function display_support_page() { |
| 170 | |
| 171 | include ADVADS_BASE_PATH . 'admin/views/support.php'; |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Render the ad group page |
| 176 | * |
| 177 | * @since 1.0.0 |
| 178 | */ |
| 179 | public function ad_group_admin_page() { |
| 180 | |
| 181 | $taxonomy = Advanced_Ads::AD_GROUP_TAXONOMY; |
| 182 | $post_type = Advanced_Ads::POST_TYPE_SLUG; |
| 183 | $tax = get_taxonomy( $taxonomy ); |
| 184 | |
| 185 | $action = Advanced_Ads_Admin::get_instance()->current_action(); |
| 186 | |
| 187 | // handle new and updated groups |
| 188 | if ( 'editedgroup' == $action ) { |
| 189 | $group_id = (int) $_POST['group_id']; |
| 190 | check_admin_referer( 'update-group_' . $group_id ); |
| 191 | |
| 192 | if ( ! current_user_can( $tax->cap->edit_terms ) ) { |
| 193 | wp_die( __( 'Sorry, you are not allowed to access this feature.', 'advanced-ads' ) ); } |
| 194 | |
| 195 | // handle new groups |
| 196 | if ( 0 == $group_id ) { |
| 197 | $ret = wp_insert_term( $_POST['name'], $taxonomy, $_POST ); |
| 198 | if ( $ret && ! is_wp_error( $ret ) ) { |
| 199 | $forced_message = 1; } |
| 200 | else { |
| 201 | $forced_message = 4; } |
| 202 | // handle group updates |
| 203 | } else { |
| 204 | $tag = get_term( $group_id, $taxonomy ); |
| 205 | if ( ! $tag ) { |
| 206 | wp_die( __( 'You attempted to edit an ad group that doesn’t exist. Perhaps it was deleted?', 'advanced-ads' ) ); } |
| 207 | |
| 208 | $ret = wp_update_term( $group_id, $taxonomy, $_POST ); |
| 209 | if ( $ret && ! is_wp_error( $ret ) ) { |
| 210 | $forced_message = 3; } |
| 211 | else { |
| 212 | $forced_message = 5; } |
| 213 | } |
| 214 | // deleting items |
| 215 | } elseif ( $action == 'delete' ){ |
| 216 | $group_id = (int) $_REQUEST['group_id']; |
| 217 | check_admin_referer( 'delete-tag_' . $group_id ); |
| 218 | |
| 219 | if ( ! current_user_can( $tax->cap->delete_terms ) ) { |
| 220 | wp_die( __( 'Sorry, you are not allowed to access this feature.', 'advanced-ads' ) ); } |
| 221 | |
| 222 | wp_delete_term( $group_id, $taxonomy ); |
| 223 | |
| 224 | $forced_message = 2; |
| 225 | } |
| 226 | |
| 227 | // handle views |
| 228 | switch ( $action ) { |
| 229 | case 'edit' : |
| 230 | $title = $tax->labels->edit_item; |
| 231 | if ( isset($_REQUEST['group_id']) ) { |
| 232 | $group_id = absint( $_REQUEST['group_id'] ); |
| 233 | $tag = get_term( $group_id, $taxonomy, OBJECT, 'edit' ); |
| 234 | } else { |
| 235 | $group_id = 0; |
| 236 | $tag = false; |
| 237 | } |
| 238 | |
| 239 | include ADVADS_BASE_PATH . 'admin/views/ad-group-edit.php'; |
| 240 | break; |
| 241 | |
| 242 | default : |
| 243 | $title = $tax->labels->name; |
| 244 | $wp_list_table = _get_list_table( 'WP_Terms_List_Table' ); |
| 245 | |
| 246 | // load template |
| 247 | include ADVADS_BASE_PATH . 'admin/views/ad-group.php'; |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | } |