delete();
$save_result = $this->save();
$output = array();
$output['status'] = 'success';
$output['message'] = 'Saved all files';
$output['version'] = ADTHRIVE_ADS_VERSION;
$output['errors'] = array();
foreach ( $save_result as $k => $k_value ) {
if ( is_wp_error( $k_value ) ) {
$output['status'] = 'error';
$output['message'] = 'Failed to save one or more files';
$output[ $k ] = $k_value->get_error_message();
$output['errors'][] = $k_value;
} elseif ( false === $k_value ) {
$output['status'] = 'error';
$output[ $k ] = 'Failed to save file';
$output['message'] = 'Failed to save one or more files';
$output['errors'][] = array( 'message' => 'Failed to save ' . $k );
} else {
$output[ $k ] = 'Saved file';
}
}
if ( 'error' === $output['status'] ) {
http_response_code( 500 );
}
echo wp_json_encode( $output );
exit();
} catch ( \Exception $e ) {
http_response_code( 500 );
$output = array(
'status' => 'error',
'message' => $e->getMessage(),
'errors' => array( $e ),
);
echo wp_json_encode( $output );
exit();
}
}
}
}
/**
* AJAX method to get terms with the matched search query for the specified taxonomy
*/
public function ajax_terms() {
if ( isset( $_GET['query'] ) ) {
$query = sanitize_text_field( wp_unslash( $_GET['query'] ) );
}
if ( isset( $_GET['taxonomy'] ) ) {
$taxonomy = sanitize_text_field( wp_unslash( $_GET['taxonomy'] ) );
}
wp_send_json( $this->get_term_selectize( $taxonomy, array( 'search' => $query ) ) );
}
/**
* Adds classes to disable ads based on the plugin settings
*
* @param string|array $classes One or more classes to add to the class list.
*/
public function body_class( $classes ) {
if ( is_singular() ) {
global $post;
$disable = get_post_meta( get_the_ID(), 'adthrive_ads_disable' );
$disable_in_image = get_post_meta( get_the_ID(), 'adthrive_ads_disable_in_image' );
$disable_content_ads = get_post_meta( get_the_ID(), 'adthrive_ads_disable_content_ads' );
$disable_auto_insert_videos = get_post_meta( get_the_ID(), 'adthrive_ads_disable_auto_insert_videos' );
$re_enable_ads_on = get_post_meta( get_the_ID(), 'adthrive_ads_re_enable_ads_on' );
$disabled_categories = \AdThrive_Ads\Options::get( 'disabled_categories' );
$disabled_tags = \AdThrive_Ads\Options::get( 'disabled_tags' );
$categories = get_the_category( $post->ID );
$tags = get_the_tags( $post->ID );
$category_names = is_array( $categories ) ? array_map( array( $this, 'pluck_name' ), $categories ) : array();
$tag_names = is_array( $tags ) ? array_map( array( $this, 'pluck_name' ), $tags ) : array();
if ( ! isset( $re_enable_ads_on[0] ) || false === trim( $re_enable_ads_on[0] ) || $re_enable_ads_on[0] > time() ) {
if ( isset( $disable[0] ) ) {
$classes[] = 'adthrive-disable-all';
}
if ( isset( $disable_in_image[0] ) || in_array( 'noads', $tag_names, true ) ) {
$classes[] = 'adthrive-disable-in-image';
}
if ( isset( $disable_content_ads[0] ) || in_array( 'noads', $tag_names, true ) ) {
$classes[] = 'adthrive-disable-content';
}
if ( isset( $disable_auto_insert_videos[0] ) || in_array( 'noads', $tag_names, true ) ) {
$classes[] = 'adthrive-disable-video';
}
}
if ( is_array( $disabled_categories ) && array_intersect( $disabled_categories, $category_names ) ) {
$classes[] = 'adthrive-disable-all';
} elseif ( is_array( $disabled_tags ) && array_intersect( $disabled_tags, $tag_names ) ) {
$classes[] = 'adthrive-disable-all';
}
} elseif ( is_404() ) {
$classes[] = 'adthrive-disable-all';
}
return $classes;
}
/**
* Gets the object name
*
* @param object $obj An object with a name property
*
* @return string The object name
*/
private function pluck_name( $obj ) {
return $obj->name;
}
/**
* Gets just the object name property
*
* @param object $obj An object with a name property
*
* @return string An object with just a name property
*/
private function get_selectize( $obj ) {
return array(
'text' => $obj->name,
'value' => $obj->name,
);
}
/**
* Add fields to the options metabox
*/
public function all_objects() {
$post_meta = new_cmb2_box( array(
'id' => 'adthrive_ads_object_metabox',
'title' => __( 'AdThrive Ads', 'adthrive_ads' ),
'object_types' => array( 'page', 'post' ),
) );
$post_meta->add_field( array(
'name' => __( 'Disable all ads', 'adthrive_ads' ),
'id' => 'adthrive_ads_disable',
'type' => 'checkbox',
) );
$post_meta->add_field( array(
'name' => __( 'Disable in image ads', 'adthrive_ads' ),
'id' => 'adthrive_ads_disable_in_image',
'type' => 'checkbox',
) );
$post_meta->add_field( array(
'name' => __( 'Disable content ads', 'adthrive_ads' ),
'id' => 'adthrive_ads_disable_content_ads',
'type' => 'checkbox',
) );
$post_meta->add_field( array(
'name' => __( 'Disable auto-insert video players', 'adthrive_ads' ),
'id' => 'adthrive_ads_disable_auto_insert_videos',
'type' => 'checkbox',
) );
$post_meta->add_field( array(
'name' => __( 'Re-enable ads on', 'adthrive_ads' ),
'desc' => __( 'All ads on this post will be enabled on the specified date', 'adthrive_ads' ),
'id' => 'adthrive_ads_re_enable_ads_on',
'type' => 'text_date_timestamp',
) );
if ( \AdThrive_Ads\Options::get( 'disable_video_metadata' ) === 'on' ) {
$post_meta->add_field( array(
'name' => __( 'Enable Video Metadata', 'adthrive_ads' ),
'desc' => __( 'Enable adding metadata to video player on this post', 'adthrive_ads' ),
'id' => 'adthrive_ads_enable_metadata',
'type' => 'checkbox',
) );
} else {
$post_meta->add_field( array(
'name' => __( 'Disable Video Metadata', 'adthrive_ads' ),
'desc' => __( 'Disable adding metadata to video player on this post', 'adthrive_ads' ),
'id' => 'adthrive_ads_disable_metadata',
'type' => 'checkbox',
) );
}
}
/**
* Add fields to the options metabox
*
* @param CMB $cmb A CMB metabox instance
*/
public function add_options( $cmb ) {
$cmb->add_field( array(
'name' => __( 'Site Id', 'adthrive_ads' ),
'desc' => __( 'Add your AdThrive Site ID', 'adthrive_ads' ),
'id' => 'site_id',
'type' => 'text',
'attributes' => array(
'required' => 'required',
'pattern' => '[0-9a-f]{24}',
'title' => 'The site id needs to match the one provided by AdThrive exactly',
),
) );
$cmb->add_field( array(
'name' => 'Disabled for Categories',
'desc' => 'Disable ads for the selected categories.',
'id' => 'disabled_categories',
'type' => 'text',
'escape_cb' => array( $this, 'selectize_escape' ),
'sanitization_cb' => array( $this, 'selectize_sanitize' ),
) );
$cmb->add_field( array(
'name' => 'Disabled for Tags',
'desc' => 'Disable ads for the selected tags.',
'id' => 'disabled_tags',
'type' => 'text',
'escape_cb' => array( $this, 'selectize_escape' ),
'sanitization_cb' => array( $this, 'selectize_sanitize' ),
) );
$cmb->add_field( array(
'name' => 'Disable Video Metadata',
'desc' => 'Disable adding metadata to video players. Caution: This is a site-wide change. Only choose if metadata is being loaded another way.',
'id' => 'disable_video_metadata',
'type' => 'checkbox',
) );
$cmb->add_field( array(
'name' => 'CLS Optimization',
'desc' => "Enable solution to reduce ad-related CLS
Clear your site's cache after saving this setting to apply the update across your site. Get more details on CLS optimization here.",
'id' => 'cls_optimization',
'type' => 'checkbox',
) );
return $cmb;
}
/**
* Called when the adthrive_ads option is updated
*/
public function options_updated( $old_value, $value, $option ) {
if ( isset( $value['cls_optimization'] ) && 'on' === $value['cls_optimization'] ) {
$this->delete();
$this->save();
}
}
/**
* Convert a selectize field array value to string
*
* @param mixed $value The actual field value.
* @return String Field value converted to a string
*/
public function selectize_escape( $value ) {
if ( is_string( $value ) ) {
return $value;
}
return ! empty( $value ) ? implode( ',', $value ) : null;
}
/**
* Convert a selectize field value to array
*
* @param mixed $value The actual field value.
* @return array Field value converted to an array
*/
public function selectize_sanitize( $value ) {
if ( is_array( $value ) ) {
return $value;
}
return ! empty( $value ) ? explode( ',', $value ) : null;
}
/**
* Add the AdThrive ads script
*/
public function ad_head() {
$data['site_id'] = \AdThrive_Ads\Options::get( 'site_id' );
$cls_optimization = \AdThrive_Ads\Options::get( 'cls_optimization' );
$thrive_architect_enabled = isset( $_GET['tve'] ) && sanitize_key( $_GET['tve'] ) === 'true';
if ( isset( $data['site_id'] ) && preg_match( '/[0-9a-f]{24}/i', $data['site_id'] ) && ! $thrive_architect_enabled ) {
$body_classes = $this->body_class( [] );
if ( 'on' === $cls_optimization ) {
$data['site_js'] = $this->get_file_content( 'site.js' );
$data['site_css'] = $this->get_file_content( 'site.css' );
$disable_content = in_array( 'adthrive-disable-content', $body_classes, true );
$disable_all = in_array( 'adthrive-disable-all', $body_classes, true );
if ( ! empty( $data['site_js'] ) ) {
require 'partials/insertion-includes.php';
$decoded_data = json_decode( $data['site_js'] );
add_action('wp_head', function() use ( $data ) {
// phpcs:disable
echo "";
// phpcs:enable
}, 100 );
if ( ! $disable_all && null !== $decoded_data && isset( $decoded_data->adUnits ) ) {
$adunits = $decoded_data->adUnits;
foreach ( $adunits as $adunit ) {
if ( 'Header' === $adunit->location && isset( $adunit->dynamic ) ) {
if ( ! isset( $adunit->dynamic->spacing ) ) {
$adunit->dynamic->spacing = 0;
}
if ( ! isset( $adunit->dynamic->max ) ) {
$adunit->dynamic->max = 0;
} else {
$adunit->dynamic->max = (int) floor( $adunit->dynamic->max );
}
if ( ! isset( $adunit->sequence ) ) {
$adunit->sequence = 1;
}
if ( true === $adunit->dynamic->enabled && 1 === $adunit->dynamic->max && 0 === $adunit->dynamic->spacing && 1 === $adunit->sequence ) {
add_action('wp_head', function() use ( $data ) {
// phpcs:disable
echo "";
// phpcs:enable
}, 101 );
}
}
}
}
add_action('wp_footer', function() use ( $data, $disable_all, $disable_content ) {
// phpcs:disable
echo "";
// phpcs:enable
}, 1 );
}
}
require 'partials/ads.php';
}
}
/**
* Gets terms and displays them as options
*
* @param String $taxonomy Taxonomy terms to retrieve. Default is category.
* @param String|array $args Optional. get_terms optional arguments
* @return array An array of options that matches the CMB2 options array
*/
public function get_term_selectize( $taxonomy = 'category', $args = array() ) {
$args['taxonomy'] = $taxonomy;
$args = wp_parse_args( $args, array(
'taxonomy' => 'category',
'number' => 100,
) );
$taxonomy = $args['taxonomy'];
$terms = (array) get_terms( $taxonomy, $args );
return is_array( $terms ) ? array_map( array( $this, 'get_selectize' ), $terms ) : array();
}
/**
* Handles upgrade process
*/
public function plugin_upgraded( $upgrader_object, $options ) {
$current_plugin_path_name = plugin_basename( ADTHRIVE_ADS_FILE );
if ( 'update' === $options['action'] && 'plugin' === $options['type'] ) {
foreach ( $options['plugins'] as $each_plugin ) {
if ( $each_plugin === $current_plugin_path_name ) {
$cls_optimization = \AdThrive_Ads\Options::get( 'cls_optimization' );
if ( 'on' === $cls_optimization ) {
$this->delete();
$this->save();
}
}
}
}
}
/**
* Get the local file content
*/
private function get_file_content( $filename ) {
require_once ABSPATH . 'wp-admin/includes/file.php';
if ( \WP_Filesystem() ) {
global $wp_filesystem;
return $wp_filesystem->get_contents( ADTHRIVE_ADS_PATH . $filename );
}
return false;
}
/**
* Get and save the latest site js and css file
*/
private function save() {
require_once ABSPATH . 'wp-admin/includes/file.php';
global $wp;
$site_id = \AdThrive_Ads\Options::get( 'site_id' );
$remote = "https://ads.adthrive.com/api/v1/siteAds/$site_id?ts=" . strval( time() );
$filename = ADTHRIVE_ADS_PATH . 'site.js';
$css_filename = ADTHRIVE_ADS_PATH . 'site.css';
// use a transient to store the etag?
$response = wp_remote_get( $remote );
$successful_saves = array();
if ( is_wp_error( $response ) ) {
$successful_saves['site_ads_json'] = $response;
}
$response_text = wp_remote_retrieve_body( $response );
if ( is_array( $response ) ) {
if ( \WP_Filesystem() ) {
global $wp_filesystem;
$successful_saves['site_ads_json'] = $wp_filesystem->put_contents( $filename, $response_text, FS_CHMOD_FILE );
$css_content = $this->get_site_css( $site_id );
if ( is_wp_error( $css_content ) ) {
$successful_saves['css_content'] = $css_content;
} else {
$successful_saves['css_content'] = $wp_filesystem->put_contents( $css_filename, $css_content, FS_CHMOD_FILE );
}
} else {
$successful_saves['site_ads_json'] = new WP_Error( 'NO_FILESYSTEM', 'WP_Filesystem not available' );
}
}
return $successful_saves;
}
/**
* Get site specific CSS content
*/
private function get_site_css( $site_id ) {
global $wp;
$url = 'https://ads.adthrive.com/sites/' . $site_id . '/ads.min.css?ts=' . strval( time() );
$response = wp_remote_get( $url );
$response_text = wp_remote_retrieve_body( $response );
if ( is_array( $response ) ) {
return $response_text;
}
return $response;
}
/**
* Delete the site js and css file
*/
private function delete() {
require_once ABSPATH . 'wp-admin/includes/file.php';
if ( \WP_Filesystem() ) {
global $wp_filesystem;
$filename = ADTHRIVE_ADS_PATH . 'site.js';
$css_filename = ADTHRIVE_ADS_PATH . 'site.css';
if ( $wp_filesystem->is_file( $filename ) ) {
$wp_filesystem->delete( $filename );
}
if ( $wp_filesystem->is_file( $css_filename ) ) {
$wp_filesystem->delete( $css_filename );
}
}
}
}