ad-health-notices.php
6 years ago
class-ad-groups-list.php
6 years ago
class-ad-network-ad-importer.php
6 years ago
class-ad-network-ad-unit.php
6 years ago
class-ad-network.php
6 years ago
class-ad-type.php
6 years ago
class-licenses.php
6 years ago
class-list-filters.php
6 years ago
class-menu.php
6 years ago
class-meta-box.php
6 years ago
class-notices.php
6 years ago
class-options.php
9 years ago
class-overview-widgets.php
6 years ago
class-settings.php
6 years ago
class-shortcode-creator.php
9 years ago
notices.php
6 years ago
shortcode-creator-l10n.php
10 years ago
class-menu.php
280 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 | // get number of ad health notices |
| 53 | $notices = Advanced_Ads_Ad_Health_Notices::get_number_of_notices(); |
| 54 | |
| 55 | // use the overview page only when there is an ad already |
| 56 | if( $has_ads ){ |
| 57 | add_menu_page( |
| 58 | __( 'Overview', 'advanced-ads' ), |
| 59 | 'Advanced Ads', |
| 60 | //sprintf( 'Advanced Ads' . '<span class="update-plugins count-' . $notices. '"><span class="update-count">' . $notices . '</span></span>' ), |
| 61 | Advanced_Ads_Plugin::user_cap( 'advanced_ads_see_interface'), $this->plugin_slug, array($this, 'display_overview_page'), 'dashicons-chart-line', '58.74' |
| 62 | ); |
| 63 | } |
| 64 | // forward Ads link to new-ad page when there is no ad existing yet. |
| 65 | // 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 |
| 66 | $target = ( ! $has_ads ) ? 'post-new.php?post_type=' . Advanced_Ads::POST_TYPE_SLUG . '&new=new' : 'edit.php?post_type=' . Advanced_Ads::POST_TYPE_SLUG; |
| 67 | add_submenu_page( |
| 68 | $this->plugin_slug, __( 'Ads', 'advanced-ads' ), __( 'Ads', 'advanced-ads' ), Advanced_Ads_Plugin::user_cap( 'advanced_ads_edit_ads'), $target |
| 69 | ); |
| 70 | |
| 71 | // display the main overview page as second item when we don’t have ads yet |
| 72 | if( ! $has_ads ){ |
| 73 | add_menu_page( |
| 74 | __( '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' |
| 75 | ); |
| 76 | |
| 77 | add_submenu_page( |
| 78 | $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') |
| 79 | ); |
| 80 | } |
| 81 | |
| 82 | // hidden by css; not placed in 'options.php' in order to highlight the correct item, see the 'highlight_menu_item()' |
| 83 | if ( ! current_user_can( 'edit_posts' ) ) { |
| 84 | add_submenu_page( |
| 85 | $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 |
| 86 | ); |
| 87 | } |
| 88 | |
| 89 | $this->ad_group_hook_suffix = add_submenu_page( |
| 90 | $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') |
| 91 | ); |
| 92 | |
| 93 | // add placements page |
| 94 | add_submenu_page( |
| 95 | $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') |
| 96 | ); |
| 97 | // add settings page |
| 98 | Advanced_Ads_Admin::get_instance()->plugin_screen_hook_suffix = add_submenu_page( |
| 99 | $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') |
| 100 | ); |
| 101 | // add support page |
| 102 | /*add_submenu_page( |
| 103 | $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') |
| 104 | );*/ |
| 105 | |
| 106 | /** |
| 107 | * since we forward the support link to the settings page, we need to add the menu item manually |
| 108 | * could break if WordPress changes the API at one point, but it didn’t do that for many years |
| 109 | */ |
| 110 | global $submenu; |
| 111 | if(current_user_can( Advanced_Ads_Plugin::user_cap( 'advanced_ads_manage_options') ) ){ |
| 112 | $submenu['advanced-ads'][] = array( |
| 113 | __('Support', 'advanced-ads' ), // title |
| 114 | Advanced_Ads_Plugin::user_cap( 'advanced_ads_manage_options'), // capability |
| 115 | admin_url( 'admin.php?page=advanced-ads-settings#top#support' ), |
| 116 | __('Support', 'advanced-ads' ), // not sure what this is, but it is in the API |
| 117 | ); |
| 118 | global $menu; |
| 119 | // manipulate the title of the overview page and add error count |
| 120 | if( isset( $menu[ '58.74' ][ 0 ] ) ){ |
| 121 | $menu[ '58.74' ][ 0 ] .= ' <span class="update-plugins count-' . $notices. '"><span class="update-count">' . $notices . '</span></span>'; |
| 122 | } |
| 123 | /*if( $has_ads ){ |
| 124 | $submenu['advanced-ads'][0][0] .= ' <span class="update-plugins count-' . $notices. '"><span class="update-count">' . $notices . '</span></span>'; |
| 125 | } else { |
| 126 | $submenu['advanced-ads'][1][0] .= ' <span class="update-plugins count-' . $notices. '"><span class="update-count">' . $notices . '</span></span>'; |
| 127 | }*/ |
| 128 | // link to license tab if they are invalid |
| 129 | if( Advanced_Ads_Checks::licenses_invalid() ){ |
| 130 | $submenu['advanced-ads'][] = array( |
| 131 | __('Licenses', 'advanced-ads' ) // title. |
| 132 | . ' <span class="update-plugins count-1"><span class="update-count">!</span></span>', |
| 133 | Advanced_Ads_Plugin::user_cap( 'advanced_ads_manage_options'), // capability |
| 134 | admin_url( 'admin.php?page=advanced-ads-settings#top#licenses' ), |
| 135 | __('Licenses', 'advanced-ads' ), // not sure what this is, but it is in the API |
| 136 | ); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | |
| 141 | // allows extensions to insert sub menu pages |
| 142 | do_action( 'advanced-ads-submenu-pages', $this->plugin_slug ); |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Highlights the 'Advanced Ads->Ads' item in the menu when an ad edit page is open |
| 147 | * @see the 'parent_file' and the 'submenu_file' filters for reference |
| 148 | */ |
| 149 | public function highlight_menu_item() { |
| 150 | global $parent_file, $submenu_file, $post_type; |
| 151 | if ( $post_type === $this->post_type ) { |
| 152 | $parent_file = $this->plugin_slug; |
| 153 | $submenu_file = 'edit.php?post_type=' . $this->post_type; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Render the overview page |
| 159 | * |
| 160 | * @since 1.2.2 |
| 161 | */ |
| 162 | public function display_overview_page() { |
| 163 | |
| 164 | include ADVADS_BASE_PATH . 'admin/views/overview.php'; |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Render the settings page |
| 169 | * |
| 170 | * @since 1.0.0 |
| 171 | */ |
| 172 | public function display_plugin_settings_page() { |
| 173 | include ADVADS_BASE_PATH . 'admin/views/settings.php'; |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Render the placements page |
| 178 | * |
| 179 | * @since 1.1.0 |
| 180 | */ |
| 181 | public function display_placements_page() { |
| 182 | $placement_types = Advanced_Ads_Placements::get_placement_types(); |
| 183 | $placements = Advanced_Ads::get_ad_placements_array(); // -TODO use model |
| 184 | $items = Advanced_Ads_Placements::items_for_select(); |
| 185 | // load ads and groups for select field |
| 186 | |
| 187 | // display view |
| 188 | include ADVADS_BASE_PATH . 'admin/views/placements.php'; |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Render the support page |
| 193 | * |
| 194 | * @since 1.6.8.1 |
| 195 | */ |
| 196 | public function display_support_page() { |
| 197 | |
| 198 | include ADVADS_BASE_PATH . 'admin/views/support.php'; |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Render the ad group page |
| 203 | * |
| 204 | * @since 1.0.0 |
| 205 | */ |
| 206 | public function ad_group_admin_page() { |
| 207 | |
| 208 | $taxonomy = Advanced_Ads::AD_GROUP_TAXONOMY; |
| 209 | $post_type = Advanced_Ads::POST_TYPE_SLUG; |
| 210 | $tax = get_taxonomy( $taxonomy ); |
| 211 | |
| 212 | $action = Advanced_Ads_Admin::get_instance()->current_action(); |
| 213 | |
| 214 | // handle new and updated groups |
| 215 | if ( 'editedgroup' == $action ) { |
| 216 | $group_id = (int) $_POST['group_id']; |
| 217 | check_admin_referer( 'update-group_' . $group_id ); |
| 218 | |
| 219 | if ( ! current_user_can( $tax->cap->edit_terms ) ) { |
| 220 | wp_die( __( 'Sorry, you are not allowed to access this feature.', 'advanced-ads' ) ); } |
| 221 | |
| 222 | // handle new groups |
| 223 | if ( 0 == $group_id ) { |
| 224 | $ret = wp_insert_term( $_POST['name'], $taxonomy, $_POST ); |
| 225 | if ( $ret && ! is_wp_error( $ret ) ) { |
| 226 | $forced_message = 1; } |
| 227 | else { |
| 228 | $forced_message = 4; } |
| 229 | // handle group updates |
| 230 | } else { |
| 231 | $tag = get_term( $group_id, $taxonomy ); |
| 232 | if ( ! $tag ) { |
| 233 | wp_die( __( 'You attempted to edit an ad group that doesn’t exist. Perhaps it was deleted?', 'advanced-ads' ) ); } |
| 234 | |
| 235 | $ret = wp_update_term( $group_id, $taxonomy, $_POST ); |
| 236 | if ( $ret && ! is_wp_error( $ret ) ) { |
| 237 | $forced_message = 3; } |
| 238 | else { |
| 239 | $forced_message = 5; } |
| 240 | } |
| 241 | // deleting items |
| 242 | } elseif ( $action == 'delete' ){ |
| 243 | $group_id = (int) $_REQUEST['group_id']; |
| 244 | check_admin_referer( 'delete-tag_' . $group_id ); |
| 245 | |
| 246 | if ( ! current_user_can( $tax->cap->delete_terms ) ) { |
| 247 | wp_die( __( 'Sorry, you are not allowed to access this feature.', 'advanced-ads' ) ); } |
| 248 | |
| 249 | wp_delete_term( $group_id, $taxonomy ); |
| 250 | // delete the weights |
| 251 | Advanced_Ads_Group::delete_ad_weights($group_id); |
| 252 | |
| 253 | $forced_message = 2; |
| 254 | } |
| 255 | |
| 256 | // handle views |
| 257 | switch ( $action ) { |
| 258 | case 'edit' : |
| 259 | $title = $tax->labels->edit_item; |
| 260 | if ( isset($_REQUEST['group_id']) ) { |
| 261 | $group_id = absint( $_REQUEST['group_id'] ); |
| 262 | $tag = get_term( $group_id, $taxonomy, OBJECT, 'edit' ); |
| 263 | } else { |
| 264 | $group_id = 0; |
| 265 | $tag = false; |
| 266 | } |
| 267 | |
| 268 | include ADVADS_BASE_PATH . 'admin/views/ad-group-edit.php'; |
| 269 | break; |
| 270 | |
| 271 | default : |
| 272 | $title = $tax->labels->name; |
| 273 | $wp_list_table = _get_list_table( 'WP_Terms_List_Table' ); |
| 274 | |
| 275 | // load template |
| 276 | include ADVADS_BASE_PATH . 'admin/views/ad-group.php'; |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | } |