cls_file_reset(); } } } /** * Resets cls files and echoes the results */ public function cls_file_reset() { header( 'Content-Type: application/json' ); try { $output = array(); $error = array(); $output['status'] = 'success'; $output['message'] = 'Saved all files'; $output['version'] = ADTHRIVE_ADS_VERSION; $output['result'] = $this->sync_cls_data(); if ( empty( $output['result'] ) ) { $error[] = 'Failed to retrieve data from deployment json endpoint.'; } $output['errors'] = $error; 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(); } } /** * Get deployment json from remote location */ public function get_deployment_json() { $remote = 'https://ads.adthrive.com/api/v1/core/cms/experiments?ts=' . strval( time() ); return $this->get_remote_file( $remote ); } /** * Update Deployment json and cls files */ public function sync_cls_data() { $cls_optimization = \AdThrive_Ads\Options::get( 'cls_optimization' ); if ( 'on' === $cls_optimization ) { $status = array(); $deployment_str = $this->get_deployment_json(); if ( $deployment_str && strlen( $deployment_str ) > 1 ) { $deployment = $this->parse_deployment_json( $deployment_str ); if ( isset( $deployment['stable'] ) ) { $this->delete_cls_files( false ); if ( isset( $deployment['test'] ) ) { $status[ $deployment['test'] ] = $this->get_cls_files( $deployment['test'], false ); } $status[ $deployment['stable'] ] = $this->get_cls_files( $deployment['stable'], true ); $status['deployment'] = $deployment; $this->save_to_file( 'cls-deployments.js', wp_json_encode( $deployment ) ); } $status['updated_at'] = time(); } $this->save_to_file( 'cls-updated.js', wp_json_encode( $status ) ); return $status; } } /** * Parse deployment json and return object */ public function parse_deployment_json( $deployment_str ) { $site_id = \AdThrive_Ads\Options::get( 'site_id' ); $deployment = array(); $feature_hashes = array(); $deploy_json = json_decode( $deployment_str ); if ( isset( $deploy_json->values ) ) { foreach ( $deploy_json->values as $feature_branch ) { $feature_hashes[] = $feature_branch->test; foreach ( $feature_branch->sites as $site ) { if ( $site === $site_id ) { $deployment['test'] = $feature_branch->test; break; } } } $deployment['stable'] = $deploy_json->stable; } $deployment['feature_hashes'] = $feature_hashes; return $deployment; } /** * Get CLS insertion files for given hash */ public function get_cls_files( $hash, $is_stable ) { $base_url = 'https://ads.adthrive.com/builds/core/' . $hash . '/js/cls/'; $status = array(); $status['insertion'] = $this->save_to_file( 'js/insertion/min/cls-insertion.' . ( $is_stable ? 'stable' : $hash ) . '.js', $this->get_remote_file( $base_url . 'cls-insertion.min.js' ) ); $status['header-insertion'] = $this->save_to_file( 'js/insertion/min/cls-header-insertion.' . ( $is_stable ? 'stable' : $hash ) . '.js', $this->get_remote_file( $base_url . 'cls-header-insertion.min.js' ) ); $status['disable-ads'] = $this->save_to_file( 'js/insertion/min/cls-disable-ads.' . ( $is_stable ? 'stable' : $hash ) . '.js', $this->get_remote_file( $base_url . 'cls-disable-ads.min.js' ) ); return $status; } /** * Make request to remote location and return content */ public function get_remote_file( $remote ) { $response = wp_remote_get( $remote ); if ( is_wp_error( $response ) ) { return ''; } return wp_remote_retrieve_body( $response ); } /** * Save string content to wp file system */ public function save_to_file( $filename, $content ) { if ( $content && strlen( $content ) > 1 ) { require_once ABSPATH . 'wp-admin/includes/file.php'; if ( \WP_Filesystem() ) { global $wp_filesystem; return $wp_filesystem->put_contents( ADTHRIVE_ADS_PATH . $filename, $content, FS_CHMOD_FILE ); } } return false; } /** * Delete existing cls files, if delete_all is true, it will delete all fines under insertion/min, otherwise keep stable files */ public function delete_cls_files( $delete_all ) { require_once ABSPATH . 'wp-admin/includes/file.php'; if ( \WP_Filesystem() ) { global $wp_filesystem; if ( $wp_filesystem->is_file( 'cls-deployments.js' ) ) { $wp_filesystem->delete( 'cls-deployments.js' ); } $cls_files = list_files( ADTHRIVE_ADS_PATH . 'js/insertion/min' ); foreach ( $cls_files as $cls_file ) { if ( $wp_filesystem->is_file( $cls_file ) && ( ! preg_match( '/stable.js$/', $cls_file ) || $delete_all ) ) { $wp_filesystem->delete( $cls_file ); } } } } /** * 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->sync_cls_data(); } else { $this->delete_cls_files( true ); } } /** * Deployment json and cls files need to be updated when plugin is upgraded */ 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->sync_cls_data(); } } } } } }