ads
3 months ago
groups
3 months ago
metaboxes
1 year ago
pages
3 months ago
placements
2 months ago
class-action-links.php
1 year ago
class-addon-box.php
1 year ago
class-addon-updater.php
3 months ago
class-admin-menu.php
3 months ago
class-admin-notices.php
1 year ago
class-ajax.php
3 months ago
class-assets.php
3 months ago
class-authors.php
1 year ago
class-compatibility.php
1 year ago
class-edd-updater.php
3 months ago
class-list-filters.php
2 months ago
class-marketing.php
1 year ago
class-metabox-ad-settings.php
1 year ago
class-metabox-ad.php
1 year ago
class-misc.php
1 year ago
class-page-quick-edit.php
1 year ago
class-plugin-installer.php
1 year ago
class-post-list.php
1 year ago
class-post-types.php
3 months ago
class-screen-options.php
3 months ago
class-settings.php
1 year ago
class-shortcode-creator.php
1 year ago
class-system-info.php
1 year ago
class-tinymce.php
2 years ago
class-translation-promo.php
1 year ago
class-upgrades.php
1 year ago
class-version-control.php
3 months ago
class-welcome.php
1 year ago
class-wordpress-dashboard.php
1 year ago
index.php
2 years ago
class-screen-options.php
191 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Admin Screen Options. |
| 4 | * |
| 5 | * @package AdvancedAds |
| 6 | * @author Advanced Ads <info@wpadvancedads.com> |
| 7 | * @since 1.48.2 |
| 8 | */ |
| 9 | |
| 10 | namespace AdvancedAds\Admin; |
| 11 | |
| 12 | use WP_Screen; |
| 13 | use AdvancedAds\Options; |
| 14 | use AdvancedAds\Utilities\Conditional; |
| 15 | use AdvancedAds\Framework\Utilities\Params; |
| 16 | use AdvancedAds\Framework\Interfaces\Integration_Interface; |
| 17 | |
| 18 | defined( 'ABSPATH' ) || exit; |
| 19 | |
| 20 | /** |
| 21 | * Admin Screen Options. |
| 22 | */ |
| 23 | class Screen_Options implements Integration_Interface { |
| 24 | |
| 25 | const USER_META_KEY = 'advanced-ads-screen-options'; |
| 26 | |
| 27 | /** |
| 28 | * Array key for screen options. |
| 29 | * |
| 30 | * @var string |
| 31 | */ |
| 32 | private $screen_key = ''; |
| 33 | |
| 34 | /** |
| 35 | * Hook into WordPress. |
| 36 | * |
| 37 | * @return void |
| 38 | */ |
| 39 | public function hooks(): void { |
| 40 | add_filter( 'screen_settings', [ $this, 'add_screen_options' ], 10, 2 ); |
| 41 | add_action( 'wp_loaded', [ $this, 'save_screen_options' ] ); |
| 42 | add_action( 'load-edit.php', [ $this, 'set_screen_options' ] ); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Return true if the current screen is the ad or placement list. |
| 47 | * |
| 48 | * @return bool |
| 49 | */ |
| 50 | private function is_screen(): bool { |
| 51 | return Conditional::is_screen( [ 'edit-advanced_ads', 'edit-advanced_ads_plcmnt' ] ); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Register custom screen options on the ad overview page. |
| 56 | * |
| 57 | * @param string $options Screen options HTML. |
| 58 | * @param WP_Screen $screen Screen object. |
| 59 | * |
| 60 | * @return string |
| 61 | */ |
| 62 | public function add_screen_options( $options, WP_Screen $screen ) { |
| 63 | if ( ! $this->is_screen() ) { |
| 64 | return $options; |
| 65 | } |
| 66 | |
| 67 | $selected_filters = $screen->get_option( 'filters_to_show' ) ?? []; |
| 68 | $optional_filters = $this->get_optional_filters(); |
| 69 | |
| 70 | // If the default WordPress screen options don't exist, we have to force the submit button to show. |
| 71 | add_filter( 'screen_options_show_submit', '__return_true' ); |
| 72 | |
| 73 | ob_start(); |
| 74 | require ADVADS_ABSPATH . 'views/admin/screen-options.php'; |
| 75 | |
| 76 | return $options . ob_get_clean(); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Add the screen options to the WP_Screen options |
| 81 | * |
| 82 | * @return void |
| 83 | */ |
| 84 | public function set_screen_options(): void { |
| 85 | $screen_options = $this->get_screen_options(); |
| 86 | |
| 87 | // Early bail!! |
| 88 | if ( ! $this->is_screen() || empty( $screen_options ) ) { |
| 89 | return; |
| 90 | } |
| 91 | |
| 92 | $screen_key = $this->get_screen_key( get_current_screen()->id ); |
| 93 | $screen_options = $screen_options[ $screen_key ] ?? []; |
| 94 | |
| 95 | foreach ( $screen_options as $option_name => $value ) { |
| 96 | add_screen_option( $option_name, $value ); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Save the screen option setting. |
| 102 | * |
| 103 | * @return void |
| 104 | */ |
| 105 | public function save_screen_options() { |
| 106 | $options = Params::post( self::USER_META_KEY, false, FILTER_DEFAULT, FILTER_REQUIRE_ARRAY ); |
| 107 | $user = wp_get_current_user(); |
| 108 | |
| 109 | // Early bail!! |
| 110 | if ( ! $options || ! $user ) { |
| 111 | return; |
| 112 | } |
| 113 | |
| 114 | check_admin_referer( 'screen-options-nonce', 'screenoptionnonce' ); |
| 115 | |
| 116 | $screen_options = $this->get_screen_options(); |
| 117 | $screen_key = $this->get_screen_key( $options['screen-id'] ); |
| 118 | |
| 119 | // no need to save it. |
| 120 | unset( $options['screen-id'] ); |
| 121 | |
| 122 | $screen_options[ $screen_key ] = $options; |
| 123 | |
| 124 | update_user_meta( $user->ID, self::USER_META_KEY, $screen_options ); |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Get the current user screen options from DB. |
| 129 | * |
| 130 | * @return array |
| 131 | */ |
| 132 | private function get_screen_options() { |
| 133 | $screen_options = get_user_meta( get_current_user_id(), self::USER_META_KEY, true ); |
| 134 | if ( ! is_array( $screen_options ) ) { |
| 135 | return []; |
| 136 | } |
| 137 | |
| 138 | return $screen_options; |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Get the screen key for DB use. |
| 143 | * |
| 144 | * @param string $screen_id Screen ID. |
| 145 | * |
| 146 | * @return string |
| 147 | */ |
| 148 | private function get_screen_key( $screen_id = false ) { |
| 149 | if ( ! $screen_id && ! empty( $this->screen_key ) ) { |
| 150 | return $this->screen_key; |
| 151 | } |
| 152 | |
| 153 | switch ( $screen_id ) { |
| 154 | case 'edit-advanced_ads': |
| 155 | $this->screen_key = 'ad'; |
| 156 | break; |
| 157 | case 'edit-advanced_ads_plcmnt': |
| 158 | $this->screen_key = 'placement'; |
| 159 | break; |
| 160 | default: |
| 161 | $this->screen_key = false; |
| 162 | } |
| 163 | |
| 164 | return $this->screen_key; |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Get optional filters. |
| 169 | * |
| 170 | * @return array The optional filters. |
| 171 | */ |
| 172 | private function get_optional_filters() { |
| 173 | // $optional_filters array order determines display sequence. |
| 174 | $optional_filters = []; |
| 175 | |
| 176 | if ( Conditional::is_screen( [ 'edit-advanced_ads' ] ) ) { |
| 177 | $optional_filters ['all_debug_mode'] = __( 'Debug Mode', 'advanced-ads' ); |
| 178 | $optional_filters['all_authors'] = __( 'Author', 'advanced-ads' ); |
| 179 | |
| 180 | // show only when privacy setting is enabled. |
| 181 | if ( Options::instance()->get( 'privacy.enabled' ) ) { |
| 182 | $optional_filters['all_privacyignore'] = __( 'Privacy Ignore', 'advanced-ads' ); |
| 183 | } |
| 184 | |
| 185 | $optional_filters = apply_filters( 'advanced_ads_optional_filters', $optional_filters ); |
| 186 | } |
| 187 | |
| 188 | return $optional_filters; |
| 189 | } |
| 190 | } |
| 191 |