plugin_dir_path( __DIR__ ), 'pluginDirUrl' => plugin_dir_url( __DIR__ ), 'adsensei_get_ads' => adsensei_get_ads(), // Add more data here that you want to access from `adsensei_get_ads ` object. ) ); register_block_type( 'adsensei/adds', array( // Enqueue blocks.style.build.css on both frontend & backend. 'style' => 'adsensei-style-css', // Enqueue blocks.build.js in the editor only. 'editor_script' => 'adsensei-js', // Enqueue blocks.editor.build.css in the editor only. 'editor_style' => 'adsensei-editor-css', ) ); } // Hook: Block assets. add_action( 'init', 'adsensei_block_assets' ); $adsensei_settings = get_option( 'adsensei_settings' ); if(isset($adsensei_settings['adsforwp_adsensei_gutenberg']) && $adsensei_settings['adsforwp_adsensei_gutenberg'] && class_exists('Adsforwp_Ads_Gutenberg') ){ remove_action( 'init', array( Adsforwp_Ads_Gutenberg::get_instance(), 'adsforwp_ads_block' )); add_action( 'init', 'adsforwp_to_adsensei_ads_block',999 ); } if(isset($adsensei_settings['adsforwp_adsensei_gutenberg']) && $adsensei_settings['adsforwp_adsensei_gutenberg'] && !class_exists('Adsforwp_Ads_Gutenberg') ){ add_action( 'init', 'adsforwp_to_adsensei_ads_block',999 ); if(! defined('ADSFORWP_PLUGIN_DIR_URI')){ add_action( 'enqueue_block_editor_assets', 'adsforwp_to_adsensei_register_admin_scripts' ); } } function adsforwp_to_adsensei_ads_block(){ if ( !function_exists( 'register_block_type' ) ) { // no Gutenberg, Abort return; } register_block_type( 'adsforwp/adsblock', array( 'editor_style' => 'adsforwp-gb-css-editor', 'editor_script' => 'adsforwp-gb-ad-js', 'render_callback' => 'adsforwp_to_adsensei_ads_render_blocks', ) ); } function adsforwp_to_adsensei_ads_render_blocks($attributes ){ ob_start(); if ( !isset( $attributes ) ) { ob_end_clean(); return ''; } // the item is an ad if ( 0 === strpos( $attributes['itemID'], 'ad_' ) ) { $id = substr( $attributes['itemID'], 3 ); echo '[adsforwp id="'.$id.'"]'; } elseif ( 0 === strpos( $attributes['itemID'], 'group_' ) ) { $group_id = substr( $attributes['itemID'], 6 ); $output_function_obj = new adsforwp_output_functions(); $group_code = $output_function_obj->adsforwp_group_ads($atts=null, $group_id, ''); echo $group_code; } return ob_get_clean(); } function adsforwp_to_adsensei_register_admin_scripts() { if ( !function_exists( 'register_block_type' ) ) { // no Gutenberg, Abort return; } wp_register_script( 'adsforwp-gb-ad-js', ADSENSEI_PLUGIN_URL . '/includes/gutenberg/src/adsforwp-blocks.js', array( 'wp-i18n', 'wp-element', 'wp-blocks', 'wp-components', 'wp-editor' ) ); $all_ads = adsforwp_to_adsensei_get_ad_ids(); $all_group_ads = array(); $ads = array(); $groups = array(); if (is_array($all_ads) && !empty($all_ads)){ foreach ( $all_ads as $ad_id ) { $ads[] = array( 'id' => $ad_id, 'title' => get_the_title( $ad_id ) ); } } if (is_array($all_group_ads) && !empty($all_group_ads) ){ foreach ( $all_group_ads as $gr_ad_id ) { $groups[] = array( 'id' => $gr_ad_id, 'name' => get_the_title($gr_ad_id) ); } } $default = array( '--empty--' => esc_html__( '--empty--', 'ads-for-wp' ), 'adsforwp' => esc_html__( 'Adsforwp Ads', 'ads-for-wp' ), 'ads' => esc_html__( 'Ads', 'ads-for-wp' ), 'adGroups' => esc_html__( 'Ad Groups', 'ads-for-wp' ), ); $inline_script = wp_json_encode( array( 'ads' => $ads, 'groups' => $groups, 'editLinks' => array( 'group' => admin_url( 'edit.php?post_type=adsforwp-groups' ), 'ad' => admin_url( 'post.php?post=%ID%&action=edit' ), ), 'default' => $default ) ); wp_add_inline_script( 'adsforwp-gb-ad-js', 'var adsforwpGutenberg = '.$inline_script,'before'); wp_enqueue_script( 'adsforwp-gb-ad-js' ); } function adsforwp_to_adsensei_get_ad_ids(){ $all_ads_id = json_decode(get_transient('adsforwp_transient_ads_ids'), true); if(!$all_ads_id){ $all_ads_post = get_posts( array( 'post_type' => 'adsforwp', 'posts_per_page' => -1, 'post_status' => 'publish', ) ); $ads_post_ids = array(); if($all_ads_post){ foreach($all_ads_post as $ads){ $ads_post_ids[] = $ads->ID; } } if(!empty($ads_post_ids) ){ return $ads_post_ids; }else{ return false; } } return $all_ads_id; }