arcache_options = get_option('arcache_options'); isset($this->arcache_options['auto_purge']) || $this->arcache_options['auto_purge'] = false; if( empty($this->arcache_options['account']) ){ $this->arcache_options['account'] = ''; update_option('arcache_options', $this->arcache_options); } $this->build_api_calls(); $this->wordpress_upload_dir = wp_upload_dir(); } function build_api_calls(){ $tkn = isset($this->arcache_options['token']) ? $this->arcache_options['token'] : null; $email = isset($this->arcache_options['email']) ? $this->arcache_options['email'] : null; $site = isset($this->arcache_options['site']) ? $this->arcache_options['site'] : null; $z = isset($this->arcache_options['account']) ? $this->arcache_options['account'] : null; $this->api_methods = array( "purge_all" => array( 'cmd' => 'purge-all', 'key' => $tkn, 'user' => $email, 'domain' => $z, 'site' => $site ), "purge_url" => array( 'cmd' => 'purge-files', 'key' => $tkn, 'user' => $email, 'domain' => $z, 'site' => $site ) ); } function return_json_success($data='') { print json_encode( array("success" => 'true', "data" => $data) ); } function return_json_error($error='') { print json_encode( array("success" => 'false', 'error' => array("message" => $error)) ); } function make_api_request($api_method, $extra_post_variables = null){ $headers = ''; if( $this->arcache_options['token'] == '' || $this->arcache_options['email'] == '' || $this->arcache_options['account'] == ''){ arcache_transaction_logging('Purge call failed due to missing config options: email=' . $this->arcache_options['email'] . ' & token=' . substr($this->arcache_options['token'], 0, 10) . '[...]' . ' & domain=' . ( isset($this->arcache_options['account']) ? $this->arcache_options['account'] : '') ); return; } if( is_array($extra_post_variables) ){ $post_variables = array_merge( $this->api_methods[$api_method], $extra_post_variables ); } else{ $post_variables = $this->api_methods[$api_method]; } if( isset($this->arcache_options['console_calls']) && !$this->arcache_suppress_debug ){ arcache_transaction_logging("\n" . "api url: " . $this->arcache_endpoint . "\n" . "api post args: " . print_r($post_variables, true) . "\n", 'print_debug'); } $results = wp_remote_post($this->arcache_endpoint, array( 'headers' => $headers, 'body' => $post_variables) ); if( isset($this->arcache_options['console_details']) && !$this->arcache_suppress_debug ){ print_r($results); } if( is_wp_error( $results ) ){ arcache_transaction_logging(print_r($results->get_error_message(), true), 'Wordpress Error'); } if($results['response']['code'] != '200'){ arcache_transaction_logging(print_r($results, true), 'error'); } return json_decode($results['body']); } function purge_entire_cache(){ $results = $this->make_api_request('purge_all'); if( $results->result == 'success' ){ arcache_transaction_logging("Purged Entire Cache for domain: " . $this->arcache_options['account'] . " | purge timestamp: " . $results->response->fpurge_ts . " | results: " . $results->result . " | msg: " . $results->msg . " | attributes: " . print_r($results->attributes, true) ); print($results->result); } else{ arcache_transaction_logging("Purge Entire Cache for domain: " . $this->arcache_options['account'] . " Error: " . $results->msg, 'error'); print($results->msg); } die(); } function purge_url($url){ $this->purge_url_after_post_save($url, true); } function purge_url_after_post_save($url, $ajax=false){ $results = $this->make_api_request('purge_url', array('url' => $url)); if($ajax){$auto="Manual";} else{$auto="Automatic";} if($ajax){die();} } function get_wordpress_domain(){ $domain = preg_replace('/http:\/\//', '', get_home_url() ); $domain = preg_replace('/www./', '', $domain ); return $domain; } } function arcache_admin_scripts_styles(){ wp_register_script( 'arcache-scripts', arcache_PLUGIN_URL . 'lib/arcache.js' ) ; wp_register_style( 'arcache-style', arcache_PLUGIN_URL . 'lib/arcache.css' ); wp_enqueue_script( 'arcache-scripts' ); wp_enqueue_style( 'arcache-style' ); } add_action('admin_init', 'arcache_admin_scripts_styles'); function arcache_add_menu_page(){ function arcache_menu_page(){ $options_page_url = arcache_PLUGIN_PATH . '/lib/arcache_options.php'; if(file_exists($options_page_url)){ include_once($options_page_url); } }; add_submenu_page( 'options-general.php', 'ابــر آروان', 'ابــر آروان', 'switch_themes', 'arcache', 'arcache_menu_page' ); }; add_action( 'admin_menu', 'arcache_add_menu_page' ); function arcache_plugin_settings_link($links) { $settings_link = 'تنظیمات'; array_unshift($links, $settings_link); return $links; } add_filter("plugin_action_links_" . arcache_PLUGIN_BASENAME, 'arcache_plugin_settings_link' ); function arcache_purge_after_save_post_hook( $post_id ){ global $hook_running; remove_action('publish_post', 'arcache_purge_after_save_post_hook'); if($hook_running) return; $hook_running = true; if( defined('DOING_SAVE') && DOING_SAVE || !$post_id ) return; if ( !in_array(get_post_type( $post_id ), array('post', 'page', 'partners')) ) return; $arcache = new arcache_API; if( $arcache->arcache_options['auto_purge'] ){ $permalink = get_permalink( $post_id ); if( is_multisite() ) : if( function_exists('domain_mapping_post_content') ) : global $wpdb; $orig_url = str_replace( "https", "http", get_original_url( 'siteurl' ) ); $url = str_replace( "https", "http", domain_mapping_siteurl( 'NA' ) ); if ( $url == 'NA' ) return $permalink; $permalink = str_replace( $orig_url, $url, $permalin ); endif; endif; $arcache->arcache_suppress_debug = true; $arcache->purge_url_after_post_save( $permalink ); if( in_array(get_post_type( $post_id ), array('post')) ){ $siteurl = site_url(); if( is_multisite() ) : if( function_exists('domain_mapping_siteurl') ) : $siteurl = domain_mapping_siteurl( get_current_blog_id() ); endif; endif; $arcache->purge_url_after_post_save( $siteurl ); } } add_action('publish_post', 'arcache_purge_after_save_post_hook'); } add_action('publish_post', 'arcache_purge_after_save_post_hook'); function arcache_entire_cache(){ $arcache = new arcache_API; $arcache->purge_entire_cache(); } add_action( 'wp_ajax_arcache_entire_cache', 'arcache_entire_cache' ); function arcache_purge_url(){ $arcache = new arcache_API; $arcache->purge_url($_REQUEST['url']); } add_action( 'wp_ajax_arcache_purge_url', 'arcache_purge_url' );