ad-health-notices.php
5 years ago
class-ad-groups-list.php
4 years ago
class-ad-network-ad-importer.php
6 years ago
class-ad-network-ad-unit.php
6 years ago
class-ad-network.php
4 years ago
class-ad-type.php
4 years ago
class-admin-upgrades.php
4 years ago
class-licenses.php
4 years ago
class-list-filters.php
4 years ago
class-menu.php
4 years ago
class-meta-box.php
4 years ago
class-notices.php
4 years ago
class-options.php
6 years ago
class-overview-widgets.php
4 years ago
class-settings.php
4 years ago
class-shortcode-creator.php
5 years ago
notices.php
4 years ago
shortcode-creator-l10n.php
5 years ago
class-shortcode-creator.php
245 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Shortcode generator for TinyMCE editor |
| 4 | */ |
| 5 | class Advanced_Ads_Shortcode_Creator { |
| 6 | /** |
| 7 | * Instance of this class. |
| 8 | * |
| 9 | * @var object |
| 10 | */ |
| 11 | protected static $instance = null; |
| 12 | |
| 13 | /** |
| 14 | * Advanced_Ads_Shortcode_Creator constructor. |
| 15 | */ |
| 16 | private function __construct() { |
| 17 | add_action( 'init', array( $this, 'init' ) ); |
| 18 | } |
| 19 | |
| 20 | /** |
| 21 | * Return an instance of this class. |
| 22 | * |
| 23 | * @return object A single instance of this class. |
| 24 | */ |
| 25 | public static function get_instance() { |
| 26 | // If the single instance hasn't been set, set it now. |
| 27 | if ( null === self::$instance ) { |
| 28 | self::$instance = new self(); |
| 29 | } |
| 30 | |
| 31 | return self::$instance; |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Call needed hooks and functions |
| 36 | */ |
| 37 | public function init() { |
| 38 | $options = Advanced_Ads::get_instance()->options(); |
| 39 | |
| 40 | if ( 'true' !== get_user_option( 'rich_editing' ) |
| 41 | || ! current_user_can( Advanced_Ads_Plugin::user_cap( 'advanced_ads_place_ads' ) ) |
| 42 | || defined( 'ADVANCED_ADS_DISABLE_SHORTCODE_BUTTON' ) |
| 43 | || apply_filters( 'advanced-ads-disable-shortcode-button', false ) |
| 44 | ) { |
| 45 | return; |
| 46 | } |
| 47 | |
| 48 | add_action( 'wp_ajax_advads_content_for_shortcode_creator', array( $this, 'get_content_for_shortcode_creator' ) ); |
| 49 | |
| 50 | // @see self::hooks_exist |
| 51 | add_filter( 'mce_buttons', array( $this, 'register_buttons' ) ); |
| 52 | add_filter( 'tiny_mce_plugins', array( $this, 'tiny_mce_plugins' ) ); |
| 53 | add_action( 'wp_tiny_mce_init', array( $this, 'print_shortcode_plugin' ) ); |
| 54 | add_action( 'print_default_editor_scripts', array( $this, 'print_shortcode_plugin' ) ); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Check if needed actions and filters have not been removed by a plugin. |
| 59 | * |
| 60 | * @return array |
| 61 | */ |
| 62 | private function hooks_exist() { |
| 63 | if ( |
| 64 | ( |
| 65 | has_action( 'wp_tiny_mce_init', array( $this, 'print_shortcode_plugin' ) ) |
| 66 | || has_action( 'print_default_editor_scripts', array( $this, 'print_shortcode_plugin' ) ) |
| 67 | ) |
| 68 | && has_filter( 'mce_buttons', array( $this, 'register_buttons' ) ) |
| 69 | && has_filter( 'tiny_mce_plugins', array( $this, 'tiny_mce_plugins' ) ) ) { |
| 70 | return true; |
| 71 | } |
| 72 | return false; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Print shortcode plugin inline. |
| 77 | * |
| 78 | * @param array|null $mce_settings TinyMCE settings array. |
| 79 | */ |
| 80 | public function print_shortcode_plugin( $mce_settings = array() ) { |
| 81 | static $printed = null; |
| 82 | |
| 83 | if ( $printed !== null ) { |
| 84 | return; |
| 85 | } |
| 86 | |
| 87 | $printed = true; |
| 88 | |
| 89 | // The `tinymce` argument of the `wp_editor()` function is set to `false`. |
| 90 | if ( empty( $mce_settings ) && ! ( doing_action( 'print_default_editor_scripts' ) && user_can_richedit() ) ) { |
| 91 | return; |
| 92 | } |
| 93 | |
| 94 | if ( ! $this->hooks_exist() ) { |
| 95 | return; |
| 96 | } |
| 97 | |
| 98 | // phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped |
| 99 | echo "<script>\n" |
| 100 | . $this->get_l10n() . "\n" |
| 101 | . file_get_contents( ADVADS_BASE_PATH . 'admin/assets/js/shortcode.js' ) . "\n" |
| 102 | . "</script>\n"; |
| 103 | // phpcs:enable |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Get localization strings. |
| 108 | * |
| 109 | * @return string |
| 110 | */ |
| 111 | private function get_l10n() { |
| 112 | static $script = null; |
| 113 | |
| 114 | if ( null === $script ) { |
| 115 | include_once ADVADS_BASE_PATH . 'admin/includes/shortcode-creator-l10n.php'; |
| 116 | $script = $strings; |
| 117 | } |
| 118 | |
| 119 | return $script; |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Add the plugin to the array of default TinyMCE plugins. |
| 124 | * We do not use the array of external TinyMCE plugins because we print the plugin file inline. |
| 125 | * |
| 126 | * @see self::admin_enqueue_scripts |
| 127 | * |
| 128 | * @param array $plugins An array of default TinyMCE plugins. |
| 129 | * @return array $plugins An array of default TinyMCE plugins. |
| 130 | */ |
| 131 | public function tiny_mce_plugins( $plugins ) { |
| 132 | if ( ! $this->hooks_exist() ) { |
| 133 | return $plugins; |
| 134 | } |
| 135 | |
| 136 | $plugins[] = 'advads_shortcode'; |
| 137 | return $plugins; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Include the shortcode plugin inline to prevent it from being blocked by ad blockers. |
| 142 | */ |
| 143 | public function admin_enqueue_scripts() { |
| 144 | // Add the localization. |
| 145 | include_once ADVADS_BASE_PATH . 'admin/includes/shortcode-creator-l10n.php'; |
| 146 | $script = $strings . "\n"; |
| 147 | // Add the plugin. |
| 148 | $script .= file_get_contents( ADVADS_BASE_PATH . 'admin/assets/js/shortcode.js' ); |
| 149 | |
| 150 | wp_add_inline_script( 'wp-tinymce', $script ); |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Add button to tinyMCE window |
| 155 | * |
| 156 | * @param array $buttons array with existing buttons. |
| 157 | * |
| 158 | * @return array |
| 159 | */ |
| 160 | public function register_buttons( $buttons ) { |
| 161 | if ( ! $this->hooks_exist() ) { |
| 162 | return $buttons; |
| 163 | } |
| 164 | if ( ! is_array( $buttons ) ) { |
| 165 | $buttons = array(); |
| 166 | } |
| 167 | $buttons[] = 'advads_shortcode_button'; |
| 168 | return $buttons; |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Prints html select field for shortcode creator |
| 173 | */ |
| 174 | public function get_content_for_shortcode_creator() { |
| 175 | if ( ! ( current_user_can( 'edit_posts' ) || current_user_can( 'edit_pages' ) ) ) { |
| 176 | return; |
| 177 | } |
| 178 | |
| 179 | $items = self::items_for_select(); ?> |
| 180 | |
| 181 | <select id="advads-select-for-shortcode"> |
| 182 | <option value=""><?php esc_html_e( '--empty--', 'advanced-ads' ); ?></option> |
| 183 | <?php if ( isset( $items['ads'] ) ) : ?> |
| 184 | <optgroup label="<?php esc_html_e( 'Ads', 'advanced-ads' ); ?>"> |
| 185 | <?php foreach ( $items['ads'] as $_item_id => $_item_title ) : ?> |
| 186 | <option value="<?php echo esc_attr( $_item_id ); ?>"><?php echo esc_html( $_item_title ); ?></option> |
| 187 | <?php endforeach; ?> |
| 188 | </optgroup> |
| 189 | <?php endif; ?> |
| 190 | <?php if ( isset( $items['groups'] ) ) : ?> |
| 191 | <optgroup label="<?php esc_html_e( 'Ad Groups', 'advanced-ads' ); ?>"> |
| 192 | <?php foreach ( $items['groups'] as $_item_id => $_item_title ) : ?> |
| 193 | <option value="<?php echo esc_attr( $_item_id ); ?>"><?php echo esc_html( $_item_title ); ?></option> |
| 194 | <?php endforeach; ?> |
| 195 | </optgroup> |
| 196 | <?php endif; ?> |
| 197 | <?php if ( isset( $items['placements'] ) ) : ?> |
| 198 | <optgroup label="<?php esc_html_e( 'Placements', 'advanced-ads' ); ?>"> |
| 199 | <?php foreach ( $items['placements'] as $_item_id => $_item_title ) : ?> |
| 200 | <option value="<?php echo esc_attr( $_item_id ); ?>"><?php echo esc_html( $_item_title ); ?></option> |
| 201 | <?php endforeach; ?> |
| 202 | </optgroup> |
| 203 | <?php endif; ?> |
| 204 | </select> |
| 205 | <?php |
| 206 | exit(); |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Get items for item select field |
| 211 | * |
| 212 | * @return array $select items for select field. |
| 213 | */ |
| 214 | public static function items_for_select() { |
| 215 | $select = array(); |
| 216 | $model = Advanced_Ads::get_instance()->get_model(); |
| 217 | |
| 218 | // load all ads. |
| 219 | $ads = $model->get_ads( |
| 220 | array( |
| 221 | 'orderby' => 'title', |
| 222 | 'order' => 'ASC', |
| 223 | ) |
| 224 | ); |
| 225 | foreach ( $ads as $_ad ) { |
| 226 | $select['ads'][ 'ad_' . $_ad->ID ] = $_ad->post_title; |
| 227 | } |
| 228 | |
| 229 | // load all ad groups. |
| 230 | $groups = $model->get_ad_groups(); |
| 231 | foreach ( $groups as $_group ) { |
| 232 | $select['groups'][ 'group_' . $_group->term_id ] = $_group->name; |
| 233 | } |
| 234 | |
| 235 | // load all placements. |
| 236 | $placements = $model->get_ad_placements_array(); |
| 237 | ksort( $placements ); |
| 238 | foreach ( $placements as $key => $_placement ) { |
| 239 | $select['placements'][ 'placement_' . $key ] = $_placement['name']; |
| 240 | } |
| 241 | |
| 242 | return $select; |
| 243 | } |
| 244 | } |
| 245 |