class-gutenberg.php
172 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Class Advanced_Ads_Gutenberg |
| 5 | */ |
| 6 | class Advanced_Ads_Gutenberg { |
| 7 | |
| 8 | private static $instance; |
| 9 | |
| 10 | private static $css_class; |
| 11 | |
| 12 | private function __construct() { |
| 13 | add_action( 'init', array( $this, 'init' ) ); |
| 14 | add_action( 'enqueue_block_editor_assets', array( $this, 'register_scripts' ) ); |
| 15 | } |
| 16 | |
| 17 | /** |
| 18 | * Register blocks |
| 19 | */ |
| 20 | public function init() { |
| 21 | if ( !function_exists( 'register_block_type' ) ) { |
| 22 | // no Gutenberg, Abort |
| 23 | return; |
| 24 | } |
| 25 | |
| 26 | register_block_type( 'advads/gblock', array( |
| 27 | 'editor_script' => ADVADS_BASE . '/gutenberg-ad', |
| 28 | 'render_callback' => array( $this, 'render_ad_selector' ), |
| 29 | ) ); |
| 30 | |
| 31 | // Removes legacy widget from legacy widget block. |
| 32 | add_filter( 'widget_types_to_hide_from_legacy_widget_block', function() { |
| 33 | $widget_types[] = 'advads_ad_widget'; |
| 34 | |
| 35 | return $widget_types; |
| 36 | } ); |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Register back end scripts |
| 41 | */ |
| 42 | public function register_scripts() { |
| 43 | if ( !function_exists( 'register_block_type' ) ) { |
| 44 | // no Gutenberg, Abort |
| 45 | return; |
| 46 | } |
| 47 | |
| 48 | wp_register_script( |
| 49 | ADVADS_BASE . '/gutenberg-ad', |
| 50 | ADVADS_BASE_URL . 'modules/gutenberg/js/advanced-ads.block.js', |
| 51 | array( 'wp-blocks', 'wp-element' ) |
| 52 | ); |
| 53 | |
| 54 | $model = Advanced_Ads::get_instance()->get_model(); |
| 55 | |
| 56 | $all_ads = Advanced_Ads::get_ads( array( 'post_status' => array( 'publish' ), 'orderby' => 'title', 'order' => 'ASC' ) ); |
| 57 | $all_groups = $model->get_ad_groups(); |
| 58 | $all_placements = Advanced_Ads::get_ad_placements_array(); |
| 59 | |
| 60 | $ads = array(); |
| 61 | $groups = array(); |
| 62 | $placements = array(); |
| 63 | |
| 64 | foreach ( $all_ads as $ad ) { |
| 65 | $ads[] = array( 'id' => $ad->ID, 'title' => $ad->post_title ); |
| 66 | } |
| 67 | |
| 68 | foreach ( $all_groups as $gr ) { |
| 69 | $groups[] = array( 'id' => $gr->term_id, 'name' => $gr->name ); |
| 70 | } |
| 71 | |
| 72 | if ( is_array( $all_placements ) ) { |
| 73 | ksort( $all_placements ); |
| 74 | } |
| 75 | |
| 76 | foreach( $all_placements as $key => $value ) { |
| 77 | if ( 'sidebar_widget' == $value['type'] ) { |
| 78 | $placements[] = array( 'id' => $key, 'name' => $value['name'] ); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | if ( empty( $placements ) ) { |
| 83 | $placements = false; |
| 84 | } |
| 85 | |
| 86 | $i18n = array( |
| 87 | '--empty--' => __( '--empty--', 'advanced-ads' ), |
| 88 | 'advads' => __( 'Advanced Ads', 'advanced-ads' ), |
| 89 | 'ads' => __( 'Ads', 'advanced-ads' ), |
| 90 | 'adGroups' => __( 'Ad Groups', 'advanced-ads' ), |
| 91 | 'placements' => __( 'Placements', 'advanced-ads' ), |
| 92 | ); |
| 93 | |
| 94 | $inline_script = wp_json_encode( |
| 95 | array( |
| 96 | 'ads' => $ads, |
| 97 | 'groups' => $groups, |
| 98 | 'placements' => $placements, |
| 99 | 'editLinks' => array( |
| 100 | 'group' => admin_url( 'admin.php?page=advanced-ads-groups' ), |
| 101 | 'placement' => admin_url( 'admin.php?page=advanced-ads-placements' ), |
| 102 | 'ad' => admin_url( 'post.php?post=%ID%&action=edit' ), |
| 103 | ), |
| 104 | 'i18n' => $i18n |
| 105 | ) |
| 106 | ); |
| 107 | |
| 108 | // put the inline code with the global variable right before the block's JS file |
| 109 | wp_add_inline_script( ADVADS_BASE . '/gutenberg-ad', 'var advadsGutenberg = ' . $inline_script, 'before' ); |
| 110 | wp_enqueue_script( ADVADS_BASE . '/gutenberg-ad' ); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Server side rendering for single ad block |
| 115 | */ |
| 116 | public static function render_ad_selector( $attr ) { |
| 117 | ob_start(); |
| 118 | |
| 119 | if ( !isset( $attr['itemID'] ) ) { |
| 120 | ob_end_clean(); |
| 121 | return ''; |
| 122 | } |
| 123 | |
| 124 | // the item is an ad |
| 125 | if ( 0 === strpos( $attr['itemID'], 'ad_' ) ) { |
| 126 | |
| 127 | $id = substr( $attr['itemID'], 3 ); |
| 128 | |
| 129 | // add CSS classes to the wrapper via filter |
| 130 | if ( isset( $attr['className'] ) ) { |
| 131 | echo get_ad( absint( $id ), array( 'output' => array( 'class' => explode( ' ', $attr['className'] ) ) ) ); |
| 132 | } else { |
| 133 | the_ad( absint( $id ) ); |
| 134 | } |
| 135 | |
| 136 | } elseif ( 0 === strpos( $attr['itemID'], 'group_' ) ) { |
| 137 | |
| 138 | $id = substr( $attr['itemID'], 6 ); |
| 139 | |
| 140 | if ( isset( $attr['className'] ) ) { |
| 141 | echo get_ad_group( $id, array( 'output' => array( 'class' => explode( ' ', $attr['className'] ) ) ) ); |
| 142 | } else { |
| 143 | the_ad_group( $id ); |
| 144 | } |
| 145 | |
| 146 | } elseif ( 0 === strpos( $attr['itemID'], 'place_' ) ) { |
| 147 | |
| 148 | $id = substr( $attr['itemID'], 6 ); |
| 149 | |
| 150 | if ( isset( $attr['className'] ) ) { |
| 151 | echo get_ad_placement( $id, array( 'output' => array( 'class' => explode( ' ', $attr['className'] ) ) ) ); |
| 152 | } else { |
| 153 | the_ad_placement( $id ); |
| 154 | } |
| 155 | |
| 156 | } |
| 157 | |
| 158 | return ob_get_clean(); |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Return the unique instance |
| 163 | */ |
| 164 | public static function get_instance() { |
| 165 | if ( null == self::$instance ) { |
| 166 | self::$instance = new self; |
| 167 | } |
| 168 | return self::$instance; |
| 169 | } |
| 170 | |
| 171 | } |
| 172 |