ad-health-notices.php
5 years ago
class-ad-groups-list.php
5 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
5 years ago
class-admin-upgrades.php
5 years ago
class-licenses.php
5 years ago
class-list-filters.php
5 years ago
class-menu.php
5 years ago
class-meta-box.php
5 years ago
class-notices.php
5 years ago
class-options.php
6 years ago
class-overview-widgets.php
5 years ago
class-settings.php
5 years ago
class-shortcode-creator.php
5 years ago
notices.php
5 years ago
shortcode-creator-l10n.php
5 years ago
class-menu.php
393 lines
| 1 | <?php |
| 2 | defined( 'ABSPATH' ) || exit; |
| 3 | |
| 4 | /** |
| 5 | * Class Advanced_Ads_Admin_Menu |
| 6 | */ |
| 7 | class Advanced_Ads_Admin_Menu { |
| 8 | /** |
| 9 | * Instance of this class. |
| 10 | * |
| 11 | * @var object |
| 12 | */ |
| 13 | protected static $instance = null; |
| 14 | |
| 15 | /** |
| 16 | * Slug of the ad group page |
| 17 | * |
| 18 | * @var string |
| 19 | */ |
| 20 | protected $ad_group_hook_suffix = null; |
| 21 | |
| 22 | /** |
| 23 | * Advanced_Ads_Admin_Menu constructor. |
| 24 | */ |
| 25 | private function __construct() { |
| 26 | // add menu items. |
| 27 | add_action( 'admin_menu', array( $this, 'add_plugin_admin_menu' ) ); |
| 28 | add_action( 'admin_head', array( $this, 'highlight_menu_item' ) ); |
| 29 | |
| 30 | $this->plugin_slug = Advanced_Ads::get_instance()->get_plugin_slug(); |
| 31 | $this->post_type = constant( 'Advanced_Ads::POST_TYPE_SLUG' ); |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Return an instance of this class. |
| 36 | * |
| 37 | * @return object A single instance of this class. |
| 38 | */ |
| 39 | public static function get_instance() { |
| 40 | // If the single instance hasn't been set, set it now. |
| 41 | if ( null === self::$instance ) { |
| 42 | self::$instance = new self(); |
| 43 | } |
| 44 | |
| 45 | return self::$instance; |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Register the administration menu for this plugin into the WordPress Dashboard menu. |
| 50 | * |
| 51 | * @since 1.0.0 |
| 52 | */ |
| 53 | public function add_plugin_admin_menu() { |
| 54 | |
| 55 | // get number of ads including those in trash. |
| 56 | $has_ads = Advanced_Ads::get_number_of_ads( array( 'any', 'trash' ) ); |
| 57 | |
| 58 | // get number of Ad Health notices. |
| 59 | $notices = Advanced_Ads_Ad_Health_Notices::get_number_of_notices(); |
| 60 | // string for Ad Health notice number. |
| 61 | $notice_alert = ' <span class="update-plugins count-' . $notices . '"><span class="update-count">' . $notices . '</span></span>'; |
| 62 | |
| 63 | // use the overview page only when there is an ad already. |
| 64 | if ( $has_ads ) { |
| 65 | add_menu_page( |
| 66 | __( 'Overview', 'advanced-ads' ), |
| 67 | 'Advanced Ads', |
| 68 | Advanced_Ads_Plugin::user_cap( 'advanced_ads_see_interface' ), |
| 69 | $this->plugin_slug, |
| 70 | array( $this, 'display_overview_page' ), |
| 71 | Advanced_Ads_Plugin::get_icon_svg(), |
| 72 | '58.74' |
| 73 | ); |
| 74 | } |
| 75 | // forward Ads link to new-ad page when there is no ad existing yet. |
| 76 | // 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. |
| 77 | $target = ( ! $has_ads ) ? 'post-new.php?post_type=' . Advanced_Ads::POST_TYPE_SLUG . '&new=new' : 'edit.php?post_type=' . Advanced_Ads::POST_TYPE_SLUG; |
| 78 | add_submenu_page( |
| 79 | $this->plugin_slug, |
| 80 | __( 'Ads', 'advanced-ads' ), |
| 81 | __( 'Ads', 'advanced-ads' ), |
| 82 | Advanced_Ads_Plugin::user_cap( 'advanced_ads_edit_ads' ), |
| 83 | $target |
| 84 | ); |
| 85 | |
| 86 | // display the main overview page as second item when we don’t have ads yet. |
| 87 | if ( ! $has_ads ) { |
| 88 | add_menu_page( |
| 89 | __( 'Overview', 'advanced-ads' ), |
| 90 | 'Advanced Ads', |
| 91 | Advanced_Ads_Plugin::user_cap( 'advanced_ads_see_interface' ), |
| 92 | $this->plugin_slug, |
| 93 | array( $this, 'display_overview_page' ), |
| 94 | Advanced_Ads_Plugin::get_icon_svg(), |
| 95 | '58.74' |
| 96 | ); |
| 97 | |
| 98 | add_submenu_page( |
| 99 | $this->plugin_slug, |
| 100 | __( 'Overview', 'advanced-ads' ), |
| 101 | __( 'Overview', 'advanced-ads' ), |
| 102 | Advanced_Ads_Plugin::user_cap( 'advanced_ads_see_interface' ), |
| 103 | $this->plugin_slug, |
| 104 | array( $this, 'display_overview_page' ) |
| 105 | ); |
| 106 | } |
| 107 | |
| 108 | // hidden by css; not placed in 'options.php' in order to highlight the correct item, see the 'highlight_menu_item()'. |
| 109 | if ( ! current_user_can( 'edit_posts' ) ) { |
| 110 | add_submenu_page( |
| 111 | $this->plugin_slug, |
| 112 | __( 'Add New Ad', 'advanced-ads' ), |
| 113 | __( 'New Ad', 'advanced-ads' ), |
| 114 | Advanced_Ads_Plugin::user_cap( 'advanced_ads_edit_ads' ), |
| 115 | 'post-new.php?post_type=' . Advanced_Ads::POST_TYPE_SLUG |
| 116 | ); |
| 117 | } |
| 118 | |
| 119 | $this->ad_group_hook_suffix = add_submenu_page( |
| 120 | $this->plugin_slug, |
| 121 | __( 'Ad Groups & Rotations', 'advanced-ads' ), |
| 122 | __( 'Groups & Rotation', 'advanced-ads' ), |
| 123 | Advanced_Ads_Plugin::user_cap( 'advanced_ads_edit_ads' ), |
| 124 | $this->plugin_slug . '-groups', |
| 125 | array( $this, 'ad_group_admin_page' ) |
| 126 | ); |
| 127 | |
| 128 | // add placements page. |
| 129 | add_submenu_page( |
| 130 | $this->plugin_slug, |
| 131 | __( 'Ad Placements', 'advanced-ads' ), |
| 132 | __( 'Placements', 'advanced-ads' ), |
| 133 | Advanced_Ads_Plugin::user_cap( 'advanced_ads_manage_placements' ), |
| 134 | $this->plugin_slug . '-placements', |
| 135 | array( $this, 'display_placements_page' ) |
| 136 | ); |
| 137 | // add settings page. |
| 138 | Advanced_Ads_Admin::get_instance()->plugin_screen_hook_suffix = add_submenu_page( |
| 139 | $this->plugin_slug, |
| 140 | __( 'Advanced Ads Settings', 'advanced-ads' ), |
| 141 | __( 'Settings', 'advanced-ads' ), |
| 142 | Advanced_Ads_Plugin::user_cap( 'advanced_ads_manage_options' ), |
| 143 | $this->plugin_slug . '-settings', |
| 144 | array( $this, 'display_plugin_settings_page' ) |
| 145 | ); |
| 146 | |
| 147 | /** |
| 148 | * Since we forward the support link to the settings page, we need to add the menu item manually |
| 149 | * could break if WordPress changes the API at one point, but it didn’t do that for many years |
| 150 | */ |
| 151 | global $submenu; |
| 152 | if ( current_user_can( Advanced_Ads_Plugin::user_cap( 'advanced_ads_manage_options' ) ) ) { |
| 153 | // we have to mute the phpcs warning about overriding superglobals since WordPress does not offer a better way to manipulate menu links. |
| 154 | // phpcs:ignore |
| 155 | $submenu['advanced-ads'][] = array( |
| 156 | __( 'Support', 'advanced-ads' ), // title. |
| 157 | Advanced_Ads_Plugin::user_cap( 'advanced_ads_manage_options' ), // capability. |
| 158 | admin_url( 'admin.php?page=advanced-ads-settings#top#support' ), |
| 159 | __( 'Support', 'advanced-ads' ), // not sure what this is, but it is in the API. |
| 160 | ); |
| 161 | // manipulate the title of the overview submenu page and add error count. |
| 162 | if ( $has_ads ) { |
| 163 | // we have to mute the phpcs warning about overriding superglobals since WordPress does not offer a better way to manipulate menu links. |
| 164 | // phpcs:ignore |
| 165 | $submenu['advanced-ads'][0][0] .= $notice_alert; |
| 166 | } else { |
| 167 | // we have to mute the phpcs warning about overriding superglobals since WordPress does not offer a better way to manipulate menu links. |
| 168 | // phpcs:ignore |
| 169 | $submenu['advanced-ads'][1][0] .= $notice_alert; |
| 170 | } |
| 171 | // link to license tab if they are invalid. |
| 172 | if ( Advanced_Ads_Checks::licenses_invalid() ) { |
| 173 | // we have to mute the phpcs warning about overriding superglobals since WordPress does not offer a better way to manipulate menu links. |
| 174 | // phpcs:ignore |
| 175 | $submenu['advanced-ads'][] = array( |
| 176 | __( 'Licenses', 'advanced-ads' ) // title.. |
| 177 | . ' <span class="update-plugins count-1"><span class="update-count">!</span></span>', |
| 178 | Advanced_Ads_Plugin::user_cap( 'advanced_ads_manage_options' ), // capability. |
| 179 | admin_url( 'admin.php?page=advanced-ads-settings#top#licenses' ), |
| 180 | __( 'Licenses', 'advanced-ads' ), // not sure what this is, but it is in the API. |
| 181 | ); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | add_filter( |
| 186 | 'option_page_capability_' . ADVADS_SLUG, |
| 187 | function () { |
| 188 | return Advanced_Ads_Plugin::user_cap( 'advanced_ads_manage_options' ); |
| 189 | } |
| 190 | ); |
| 191 | |
| 192 | /** |
| 193 | * Allows extensions to insert sub menu pages. |
| 194 | * |
| 195 | * @since untagged Added the `$hidden_page_slug` parameter. |
| 196 | * |
| 197 | * @param string $plugin_slug The slug slug used to add a visible page. |
| 198 | * @param string $hidden_page_slug The slug slug used to add a hidden page. |
| 199 | */ |
| 200 | do_action( 'advanced-ads-submenu-pages', $this->plugin_slug, 'advanced_ads_hidden_page_slug' ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Highlights the 'Advanced Ads->Ads' item in the menu when an ad edit page is open |
| 205 | * |
| 206 | * @see the 'parent_file' and the 'submenu_file' filters for reference |
| 207 | */ |
| 208 | public function highlight_menu_item() { |
| 209 | global $parent_file, $submenu_file, $post_type; |
| 210 | if ( $post_type === $this->post_type ) { |
| 211 | // we have to mute the phpcs warning about overriding superglobals since WordPress does not offer a better way to manipulate menu links. |
| 212 | // phpcs:ignore |
| 213 | $parent_file = $this->plugin_slug; |
| 214 | // phpcs:ignore |
| 215 | $submenu_file = 'edit.php?post_type=' . $this->post_type; |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * Render the overview page |
| 221 | * |
| 222 | * @since 1.2.2 |
| 223 | */ |
| 224 | public function display_overview_page() { |
| 225 | |
| 226 | include ADVADS_BASE_PATH . 'admin/views/overview.php'; |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * Render the settings page |
| 231 | * |
| 232 | * @since 1.0.0 |
| 233 | */ |
| 234 | public function display_plugin_settings_page() { |
| 235 | include ADVADS_BASE_PATH . 'admin/views/settings.php'; |
| 236 | } |
| 237 | |
| 238 | /** |
| 239 | * Render the placements page |
| 240 | * |
| 241 | * @since 1.1.0 |
| 242 | */ |
| 243 | public function display_placements_page() { |
| 244 | $placement_types = Advanced_Ads_Placements::get_placement_types(); |
| 245 | $placements = Advanced_Ads::get_ad_placements_array(); // -TODO use model |
| 246 | // load ads and groups for select field. |
| 247 | $items = Advanced_Ads_Placements::items_for_select(); |
| 248 | $orderby = $this->get_field_to_order_placement(); |
| 249 | |
| 250 | // display view. |
| 251 | include ADVADS_BASE_PATH . 'admin/views/placements.php'; |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * Render the support page |
| 256 | * |
| 257 | * @since 1.6.8.1 |
| 258 | */ |
| 259 | public function display_support_page() { |
| 260 | |
| 261 | include ADVADS_BASE_PATH . 'admin/views/support.php'; |
| 262 | } |
| 263 | |
| 264 | /** |
| 265 | * Render the ad group page |
| 266 | * |
| 267 | * @since 1.0.0 |
| 268 | */ |
| 269 | public function ad_group_admin_page() { |
| 270 | |
| 271 | $taxonomy = Advanced_Ads::AD_GROUP_TAXONOMY; |
| 272 | $post_type = Advanced_Ads::POST_TYPE_SLUG; |
| 273 | $tax = get_taxonomy( $taxonomy ); |
| 274 | |
| 275 | $action = Advanced_Ads_Admin::get_instance()->current_action(); |
| 276 | |
| 277 | // handle new and updated groups. |
| 278 | if ( 'editedgroup' === $action ) { |
| 279 | $group_id = (int) $_POST['group_id']; |
| 280 | check_admin_referer( 'update-group_' . $group_id ); |
| 281 | |
| 282 | if ( ! current_user_can( $tax->cap->edit_terms ) ) { |
| 283 | wp_die( esc_html__( 'Sorry, you are not allowed to access this feature.', 'advanced-ads' ) ); } |
| 284 | |
| 285 | // handle new groups. |
| 286 | if ( 0 === $group_id ) { |
| 287 | $ret = wp_insert_term( $_POST['name'], $taxonomy, $_POST ); |
| 288 | if ( $ret && ! is_wp_error( $ret ) ) { |
| 289 | $forced_message = 1; } else { |
| 290 | $forced_message = 4; } |
| 291 | // handle group updates. |
| 292 | } else { |
| 293 | $tag = get_term( $group_id, $taxonomy ); |
| 294 | if ( ! $tag ) { |
| 295 | wp_die( esc_html__( 'You attempted to edit an ad group that doesn’t exist. Perhaps it was deleted?', 'advanced-ads' ) ); } |
| 296 | |
| 297 | $ret = wp_update_term( $group_id, $taxonomy, $_POST ); |
| 298 | if ( $ret && ! is_wp_error( $ret ) ) { |
| 299 | $forced_message = 3; } else { |
| 300 | $forced_message = 5; } |
| 301 | } |
| 302 | // deleting items. |
| 303 | } elseif ( 'delete' === $action ) { |
| 304 | $group_id = (int) $_REQUEST['group_id']; |
| 305 | check_admin_referer( 'delete-tag_' . $group_id ); |
| 306 | |
| 307 | if ( ! current_user_can( $tax->cap->delete_terms ) ) { |
| 308 | wp_die( esc_html__( 'Sorry, you are not allowed to access this feature.', 'advanced-ads' ) ); } |
| 309 | |
| 310 | wp_delete_term( $group_id, $taxonomy ); |
| 311 | // delete the weights. |
| 312 | Advanced_Ads_Group::delete_ad_weights( $group_id ); |
| 313 | |
| 314 | $forced_message = 2; |
| 315 | } |
| 316 | |
| 317 | // handle views. |
| 318 | switch ( $action ) { |
| 319 | case 'edit': |
| 320 | $title = $tax->labels->edit_item; |
| 321 | if ( isset( $_REQUEST['group_id'] ) ) { |
| 322 | $group_id = absint( $_REQUEST['group_id'] ); |
| 323 | $tag = get_term( $group_id, $taxonomy, OBJECT, 'edit' ); |
| 324 | } else { |
| 325 | $group_id = 0; |
| 326 | $tag = false; |
| 327 | } |
| 328 | |
| 329 | include ADVADS_BASE_PATH . 'admin/views/ad-group-edit.php'; |
| 330 | break; |
| 331 | |
| 332 | default: |
| 333 | $title = $tax->labels->name; |
| 334 | $wp_list_table = _get_list_table( 'WP_Terms_List_Table' ); |
| 335 | |
| 336 | // load template. |
| 337 | include ADVADS_BASE_PATH . 'admin/views/ad-group.php'; |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | /** |
| 342 | * Get the field to order placements. |
| 343 | * |
| 344 | * @return string |
| 345 | */ |
| 346 | private function get_field_to_order_placement() { |
| 347 | $admin_settings = Advanced_Ads_Admin::get_admin_settings(); |
| 348 | |
| 349 | $default = isset( $admin_settings['placement-orderby'] ) ? $admin_settings['placement-orderby'] : 'type'; |
| 350 | $current = isset( $_GET['orderby'] ) ? $_GET['orderby'] : $default; // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 351 | |
| 352 | if ( ! in_array( $current, array( 'name', 'type' ) ) ) { |
| 353 | $current = 'type'; |
| 354 | } |
| 355 | |
| 356 | // Save default field, if it was changed. |
| 357 | $admin_settings['placement-orderby'] = $current; |
| 358 | Advanced_Ads_Admin::update_admin_setttings( $admin_settings ); |
| 359 | |
| 360 | return $current; |
| 361 | } |
| 362 | |
| 363 | /** |
| 364 | * Get the URL where the user is redirected after activating the frontend picker for a "Content" placement. |
| 365 | * |
| 366 | * @since string URL. |
| 367 | */ |
| 368 | private function get_url_for_content_placement_picker() { |
| 369 | $location = false; |
| 370 | |
| 371 | if ( 'posts' === get_option( 'show_on_front' ) ) { |
| 372 | $recent_posts = wp_get_recent_posts( |
| 373 | array( |
| 374 | 'numberposts' => 1, |
| 375 | 'post_type' => 'post', |
| 376 | 'post_status' => 'publish', |
| 377 | ), |
| 378 | 'OBJECT' |
| 379 | ); |
| 380 | |
| 381 | if ( $recent_posts ) { |
| 382 | $location = get_permalink( $recent_posts[0] ); |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | if ( ! $location ) { |
| 387 | $location = home_url(); |
| 388 | } |
| 389 | return $location; |
| 390 | } |
| 391 | |
| 392 | } |
| 393 |