class-gutenberg.php
279 lines
| 1 | <?php |
| 2 | // phpcs:ignoreFile |
| 3 | /** |
| 4 | * Gutenberg block registration |
| 5 | * |
| 6 | * @package Advanced_Ads |
| 7 | */ |
| 8 | |
| 9 | /** |
| 10 | * Class Advanced_Ads_Gutenberg |
| 11 | */ |
| 12 | class Advanced_Ads_Gutenberg { |
| 13 | |
| 14 | /** |
| 15 | * The singleton |
| 16 | * |
| 17 | * @var Advanced_Ads_Gutenberg |
| 18 | */ |
| 19 | private static $instance; |
| 20 | |
| 21 | /** |
| 22 | * CSS classes to use on the frontend |
| 23 | * |
| 24 | * @var string |
| 25 | */ |
| 26 | private static $css_class; |
| 27 | |
| 28 | /** |
| 29 | * Constructor |
| 30 | */ |
| 31 | private function __construct() { |
| 32 | add_action( 'init', [ $this, 'init' ] ); |
| 33 | add_action( 'enqueue_block_editor_assets', [ $this, 'register_scripts' ] ); |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Register blocks |
| 38 | */ |
| 39 | public function init() { |
| 40 | if ( ! function_exists( 'register_block_type' ) ) { |
| 41 | // no Gutenberg, Abort. |
| 42 | return; |
| 43 | } |
| 44 | |
| 45 | register_block_type( |
| 46 | 'advads/gblock', |
| 47 | [ |
| 48 | 'editor_script_handles' => [ ADVADS_PLUGIN_BASENAME . '/gutenberg-ad' ], |
| 49 | 'editor_style_handles' => [ ADVADS_PLUGIN_BASENAME . '/gutenberg-ad' ], |
| 50 | 'render_callback' => [ $this, 'render_ad_selector' ], |
| 51 | ] |
| 52 | ); |
| 53 | |
| 54 | /** |
| 55 | * Removes legacy widget from legacy widget block. |
| 56 | * |
| 57 | * @param string[] $widget_types An array of excluded widget-type IDs. |
| 58 | * |
| 59 | * @return array |
| 60 | */ |
| 61 | add_filter( |
| 62 | 'widget_types_to_hide_from_legacy_widget_block', |
| 63 | function( $widget_types ) { |
| 64 | $widget_types[] = 'advads_ad_widget'; |
| 65 | |
| 66 | return $widget_types; |
| 67 | } |
| 68 | ); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Register back end scripts |
| 73 | * |
| 74 | * @return void |
| 75 | */ |
| 76 | public function register_scripts() { |
| 77 | if ( ! function_exists( 'register_block_type' ) ) { |
| 78 | // no Gutenberg, Abort. |
| 79 | return; |
| 80 | } |
| 81 | |
| 82 | wp_register_script( |
| 83 | ADVADS_PLUGIN_BASENAME . '/gutenberg-ad', |
| 84 | ADVADS_BASE_URL . 'modules/gutenberg/assets/advanced-ads.block.js', |
| 85 | [ 'wp-dom-ready', 'wp-blocks', 'wp-element' ], |
| 86 | ADVADS_VERSION, |
| 87 | false |
| 88 | ); |
| 89 | |
| 90 | $ads = []; |
| 91 | $groups = []; |
| 92 | $placements = []; |
| 93 | |
| 94 | foreach ( wp_advads_get_ad_summaries() as $id => $summary ) { |
| 95 | if ( ! in_array( $summary['status'], [ 'publish', 'future' ], true ) ) { |
| 96 | continue; |
| 97 | } |
| 98 | |
| 99 | $ads[] = [ |
| 100 | 'id' => $id, |
| 101 | 'title' => $summary['title'], |
| 102 | ]; |
| 103 | } |
| 104 | |
| 105 | foreach ( wp_advads_get_groups_dropdown() as $id => $title ) { |
| 106 | $groups[] = [ |
| 107 | 'id' => $id, |
| 108 | 'name' => $title, |
| 109 | ]; |
| 110 | } |
| 111 | |
| 112 | foreach ( wp_advads_get_placement_summaries() as $id => $summary ) { |
| 113 | if ( ! in_array( $summary['type'], [ 'sidebar_widget', 'default' ], true ) ) { |
| 114 | continue; |
| 115 | } |
| 116 | |
| 117 | $placements[] = [ |
| 118 | 'id' => $id, |
| 119 | 'name' => $summary['title'], |
| 120 | ]; |
| 121 | } |
| 122 | |
| 123 | ksort( $placements ); |
| 124 | |
| 125 | if ( empty( $placements ) ) { |
| 126 | $placements = false; |
| 127 | } |
| 128 | |
| 129 | $i18n = [ |
| 130 | '--empty--' => __( '--empty--', 'advanced-ads' ), |
| 131 | 'advads' => __( 'Advanced Ads', 'advanced-ads' ), |
| 132 | 'ads' => __( 'Ads', 'advanced-ads' ), |
| 133 | 'adGroups' => __( 'Ad Groups', 'advanced-ads' ), |
| 134 | 'placements' => __( 'Placements', 'advanced-ads' ), |
| 135 | 'width' => __( 'Width', 'advanced-ads' ), |
| 136 | 'height' => __( 'Height', 'advanced-ads' ), |
| 137 | 'size' => __( 'Size', 'advanced-ads' ), |
| 138 | 'alignment' => __( 'Alignment', 'advanced-ads' ), |
| 139 | ]; |
| 140 | |
| 141 | $inline_script = wp_json_encode( |
| 142 | [ |
| 143 | 'wpVersion' => get_bloginfo( 'version' ), |
| 144 | 'ads' => $ads, |
| 145 | 'groups' => $groups, |
| 146 | 'placements' => $placements, |
| 147 | 'editLinks' => [ |
| 148 | 'group' => admin_url( 'admin.php?page=advanced-ads-groups' ), |
| 149 | 'placement' => admin_url( 'admin.php?page=advanced-ads-placements' ), |
| 150 | 'ad' => admin_url( 'post.php?post=%ID%&action=edit' ), |
| 151 | ], |
| 152 | 'imagesUrl' => ADVADS_BASE_URL . 'modules/gutenberg/assets/img/', |
| 153 | 'i18n' => $i18n, |
| 154 | 'textFlow' => [ |
| 155 | 'default' => [ |
| 156 | 'label' => __( "Theme's default", 'advanced-ads' ), |
| 157 | 'description' => __( 'The ad will behave as predefined by the theme.', 'advanced-ads' ), |
| 158 | ], |
| 159 | 'float-left' => [ |
| 160 | 'label' => __( "Float left", 'advanced-ads' ), |
| 161 | 'description' => __( 'Text will wrap around the ad and its margin.', 'advanced-ads' ), |
| 162 | ], |
| 163 | 'float-right' => [ |
| 164 | 'label' => __( "Float right", 'advanced-ads' ), |
| 165 | 'description' => __( 'Text will wrap around the ad and its margin.', 'advanced-ads' ), |
| 166 | ], |
| 167 | 'block-left' => [ |
| 168 | 'label' => __( "Block left", 'advanced-ads' ), |
| 169 | 'description' => __( 'Text will continue after the ad and its margin.', 'advanced-ads' ), |
| 170 | ], |
| 171 | 'block-right' => [ |
| 172 | 'label' => __( "Block right", 'advanced-ads' ), |
| 173 | 'description' => __( 'Text will continue after the ad and its margin.', 'advanced-ads' ), |
| 174 | ], |
| 175 | 'center' => [ |
| 176 | 'label' => __( "Centered", 'advanced-ads' ), |
| 177 | 'description' => __( 'Text will continue after the ad and its margin.', 'advanced-ads' ), |
| 178 | ], |
| 179 | ], |
| 180 | ] |
| 181 | ); |
| 182 | |
| 183 | // put the inline code with the global variable right before the block's JS file. |
| 184 | wp_add_inline_script( ADVADS_PLUGIN_BASENAME . '/gutenberg-ad', 'var advadsGutenberg = ' . $inline_script, 'before' ); |
| 185 | wp_enqueue_script( ADVADS_PLUGIN_BASENAME . '/gutenberg-ad' ); |
| 186 | |
| 187 | wp_enqueue_style( |
| 188 | ADVADS_PLUGIN_BASENAME . '/gutenberg-ad', |
| 189 | ADVADS_BASE_URL . 'modules/gutenberg/assets/block.css', |
| 190 | [], |
| 191 | ADVADS_VERSION |
| 192 | ); |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Server side rendering for single ad block |
| 197 | * |
| 198 | * @param array $attr Block's attributes. |
| 199 | */ |
| 200 | public static function render_ad_selector( $attr ) { |
| 201 | ob_start(); |
| 202 | |
| 203 | if ( ! isset( $attr['itemID'] ) ) { |
| 204 | ob_end_clean(); |
| 205 | |
| 206 | return ''; |
| 207 | } |
| 208 | |
| 209 | $output = [ |
| 210 | 'output' => [ |
| 211 | 'class' => ! empty( $attr['className'] ) ? array_filter( explode( ' ', $attr['className'] ) ) : [], |
| 212 | ], |
| 213 | ]; |
| 214 | |
| 215 | if ( isset( $attr['fixed_widget'] ) ) { |
| 216 | $output['wrapper_attrs']['data-fixed_widget'] = esc_attr( $attr['fixed_widget'] ); |
| 217 | } |
| 218 | |
| 219 | if ( ! empty( $attr['width'] ) ) { |
| 220 | $output['output']['wrapper_attrs']['style']['width'] = absint( $attr['width'] ) . 'px'; |
| 221 | } |
| 222 | |
| 223 | if ( ! empty( $attr['height'] ) ) { |
| 224 | $output['output']['wrapper_attrs']['style']['height'] = absint( $attr['height'] ) . 'px'; |
| 225 | } |
| 226 | |
| 227 | $align = esc_attr( $attr['align'] ?? 'default' ); |
| 228 | $after_ad_filter = function( $output ) { |
| 229 | return $output . '<br style="clear: both; display: block; float: none;">'; |
| 230 | }; |
| 231 | |
| 232 | if ( 0 === strpos( $align, 'block' ) ) { |
| 233 | add_filter( 'advanced-ads-ad-output', $after_ad_filter ); |
| 234 | } |
| 235 | |
| 236 | switch ( $align ) { |
| 237 | case 'float-left': |
| 238 | case 'block-left': |
| 239 | $output['output']['wrapper_attrs']['style']['float'] = 'left'; |
| 240 | break; |
| 241 | case 'float-right': |
| 242 | case 'block-right': |
| 243 | $output['output']['wrapper_attrs']['style']['float'] = 'right'; |
| 244 | break; |
| 245 | case 'center': |
| 246 | $output['output']['wrapper_attrs']['style']['margin-left'] = 'auto'; |
| 247 | $output['output']['wrapper_attrs']['style']['margin-right'] = 'auto'; |
| 248 | $output['output']['wrapper_attrs']['style']['text-align'] = 'center'; |
| 249 | break; |
| 250 | default: |
| 251 | } |
| 252 | |
| 253 | // phpcs:disable WordPress.Security.EscapeOutput.OutputNotEscaped -- we can't escape ad output without potentially breaking ads |
| 254 | if ( 0 === strpos( $attr['itemID'], 'ad_' ) ) { |
| 255 | echo get_the_ad( absint( substr( $attr['itemID'], 3 ) ), '', $output ); |
| 256 | } elseif ( 0 === strpos( $attr['itemID'], 'group_' ) ) { |
| 257 | echo get_the_group( substr( $attr['itemID'], 6 ), '', $output ); |
| 258 | } elseif ( 0 === strpos( $attr['itemID'], 'place_' ) ) { |
| 259 | $id = substr( $attr['itemID'], 6 ); |
| 260 | echo get_the_placement( is_numeric( $id ) ? (int) $id : $id, '', $output ); |
| 261 | } |
| 262 | |
| 263 | // phpcs:enable |
| 264 | |
| 265 | return ob_get_clean(); |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * Return the unique instance |
| 270 | */ |
| 271 | public static function get_instance() { |
| 272 | if ( null === self::$instance ) { |
| 273 | self::$instance = new self(); |
| 274 | } |
| 275 | |
| 276 | return self::$instance; |
| 277 | } |
| 278 | } |
| 279 |