type-amp.php
1 year ago
type-content.php
10 months ago
type-dummy.php
1 year ago
type-gam.php
1 year ago
type-group.php
1 year ago
type-image.php
1 year ago
type-plain.php
1 year ago
type-unknown.php
1 year ago
type-content.php
162 lines
| 1 | <?php |
| 2 | /** |
| 3 | * This class represents the "Content" ad type. |
| 4 | * |
| 5 | * @package AdvancedAds |
| 6 | * @author Advanced Ads <info@wpadvancedads.com> |
| 7 | * @since 1.48.0 |
| 8 | */ |
| 9 | |
| 10 | namespace AdvancedAds\Ads\Types; |
| 11 | |
| 12 | use AdvancedAds\Ads\Ad_Content; |
| 13 | use AdvancedAds\Interfaces\Ad_Type; |
| 14 | |
| 15 | defined( 'ABSPATH' ) || exit; |
| 16 | |
| 17 | /** |
| 18 | * Type Content. |
| 19 | */ |
| 20 | class Content implements Ad_Type { |
| 21 | |
| 22 | /** |
| 23 | * Get the unique identifier (ID) of the ad type. |
| 24 | * |
| 25 | * @return string The unique ID of the ad type. |
| 26 | */ |
| 27 | public function get_id(): string { |
| 28 | return 'content'; |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Get the class name of the object as a string. |
| 33 | * |
| 34 | * @return string |
| 35 | */ |
| 36 | public function get_classname(): string { |
| 37 | return Ad_Content::class; |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Get the title or name of the ad type. |
| 42 | * |
| 43 | * @return string The title of the ad type. |
| 44 | */ |
| 45 | public function get_title(): string { |
| 46 | return __( 'Rich Content', 'advanced-ads' ); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Get a description of the ad type. |
| 51 | * |
| 52 | * @return string The description of the ad type. |
| 53 | */ |
| 54 | public function get_description(): string { |
| 55 | return __( 'The full content editor from WordPress with all features like shortcodes, image upload or styling, but also simple text/html mode for scripts and code.', 'advanced-ads' ); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Check if this ad type requires premium. |
| 60 | * |
| 61 | * @return bool True if premium is required; otherwise, false. |
| 62 | */ |
| 63 | public function is_premium(): bool { |
| 64 | return false; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Get the URL for upgrading to this ad type. |
| 69 | * |
| 70 | * @return string The upgrade URL for the ad type. |
| 71 | */ |
| 72 | public function get_upgrade_url(): string { |
| 73 | return ''; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Get the URL for upgrading to this ad type. |
| 78 | * |
| 79 | * @return string The upgrade URL for the ad type. |
| 80 | */ |
| 81 | public function get_image(): string { |
| 82 | return ADVADS_BASE_URL . 'assets/img/ad-types/content.svg'; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Check if this ad type has size parameters. |
| 87 | * |
| 88 | * @return bool True if has size parameters; otherwise, false. |
| 89 | */ |
| 90 | public function has_size(): bool { |
| 91 | return true; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Output for the ad parameters metabox |
| 96 | * |
| 97 | * @param Ad_Content $ad Ad instance. |
| 98 | * |
| 99 | * @return void |
| 100 | */ |
| 101 | public function render_parameters( $ad ): void { |
| 102 | $content = $ad->get_content() ?? ''; |
| 103 | |
| 104 | /** |
| 105 | * Build the tinymc editor |
| 106 | * |
| 107 | * @link http://codex.wordpress.org/Function_Reference/wp_editor |
| 108 | * |
| 109 | * Don't build it when ajax is used; display message and buttons instead |
| 110 | */ |
| 111 | if ( wp_doing_ajax() ) : |
| 112 | // IMPORTANT: Keep textarea on a single line to prevent whitespace from being added to the content. |
| 113 | ?> |
| 114 | <textarea id="advads-ad-content-plain" style="display:none;" cols="40" rows="10" name="advanced_ad[content]"><?php echo esc_textarea( $content ); ?></textarea> |
| 115 | <?php |
| 116 | else : |
| 117 | if ( ! user_can_richedit() ) { |
| 118 | $content = esc_textarea( $content ); |
| 119 | } |
| 120 | add_filter( 'tiny_mce_before_init', [ $this, 'tiny_mce_before_init' ], 10, 2 ); |
| 121 | |
| 122 | $args = [ |
| 123 | 'textarea_name' => 'advanced_ad[content]', |
| 124 | 'textarea_rows' => 10, |
| 125 | 'drag_drop_upload' => true, |
| 126 | ]; |
| 127 | wp_editor( $content, 'advanced-ad-parameters-content', $args ); |
| 128 | endif; |
| 129 | ?> |
| 130 | <br class="clear"/> |
| 131 | <input type="hidden" name="advanced_ad[output][allow_shortcodes]" value="1" /> |
| 132 | <?php |
| 133 | include ADVADS_ABSPATH . 'views/admin/metaboxes/ads/ad-info-after-textarea.php'; |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Add JS into tinyMCE |
| 138 | * |
| 139 | * @param array $init_array TinyMCE arguments. |
| 140 | * @param string $editor_id Editor id. |
| 141 | * |
| 142 | * @return array |
| 143 | */ |
| 144 | public function tiny_mce_before_init( array $init_array, $editor_id ): array { |
| 145 | if ( 'advanced-ad-parameters-content' !== $editor_id ) { |
| 146 | return $init_array; |
| 147 | } |
| 148 | |
| 149 | // Add a JS listener to trigger an `input` event for the rich text textarea. |
| 150 | $init_array['setup'] = <<<'JS' |
| 151 | [editor => { |
| 152 | const textarea = document.getElementById('advanced-ad-parameters-content'); |
| 153 | editor.on('Dirty', event => { |
| 154 | textarea.value = editor.getContent(); |
| 155 | textarea.dispatchEvent(new Event('input')); |
| 156 | }); |
| 157 | }][0] |
| 158 | JS; |
| 159 | return $init_array; |
| 160 | } |
| 161 | } |
| 162 |