_config = Dispatcher::config();
}
/**
* Runs plugin
*/
function run() {
$this->is_w3tc_page = Util_Admin::is_w3tc_admin_page();
add_action( 'admin_init', array(
$this,
'admin_init'
) );
add_action( 'admin_init_w3tc_dashboard', array(
'\W3TC\Generic_WidgetServices',
'admin_init_w3tc_dashboard' ) );
add_action( 'admin_init_w3tc_dashboard', array(
'\W3TC\Generic_WidgetCommunity',
'admin_init_w3tc_dashboard' ) );
add_action( 'admin_init_w3tc_dashboard', array(
'\W3TC\Generic_WidgetBoldGrid',
'admin_init_w3tc_dashboard' ) );
add_action( 'admin_enqueue_scripts', array(
$this,
'admin_enqueue_scripts'
) );
add_action( 'admin_print_styles-toplevel_page_w3tc_dashboard', array(
'\W3TC\Generic_Page_Dashboard',
'admin_print_styles_w3tc_dashboard'
) );
add_action( 'wp_ajax_w3tc_ajax', array(
$this,
'wp_ajax_w3tc_ajax'
) );
add_action( 'wp_ajax_w3tc_monitoring_score', array(
$this,
'wp_ajax_w3tc_monitoring_score'
) );
add_action( 'admin_head', array(
$this,
'admin_head'
) );
if ( is_network_admin() ) {
add_action( 'network_admin_menu', array(
$this,
'network_admin_menu'
) );
add_filter( 'network_admin_plugin_action_links_' . W3TC_FILE, array(
$this,
'plugin_action_links'
) );
} else {
add_action( 'admin_menu', array(
$this,
'admin_menu'
) );
add_filter( 'plugin_action_links_' . W3TC_FILE, array(
$this,
'plugin_action_links'
) );
}
add_filter( 'favorite_actions', array(
$this,
'favorite_actions'
) );
add_action( 'in_plugin_update_message-' . W3TC_FILE, array(
$this,
'in_plugin_update_message'
) );
if ( $this->_config->get_boolean( 'pgcache.enabled' ) || $this->_config->get_boolean( 'minify.enabled' ) ) {
add_filter( 'pre_update_option_active_plugins', array(
$this,
'pre_update_option_active_plugins'
) );
}
$config_labels = new Generic_ConfigLabels();
add_filter( 'w3tc_config_labels', array( $config_labels, 'config_labels' ) );
$admin_notes = new Generic_AdminNotes();
add_filter( 'w3tc_notes', array( $admin_notes, 'w3tc_notes' ) );
add_filter( 'w3tc_errors', array( $admin_notes, 'w3tc_errors' ), 1000 );
add_action( 'w3tc_ajax_faq', array(
$this,
'w3tc_ajax_faq'
) );
// load w3tc_message
$message_id = Util_Request::get_string( 'w3tc_message' );
if ( $message_id ) {
$v = get_transient( 'w3tc_message' );
if ( isset( $v[$message_id] ) ) {
$this->w3tc_message = $v[$message_id];
delete_transient( 'w3tc_message' );
}
}
// should be in Support_PluginAdmin, but saving loading file by being here
add_action( 'admin_print_scripts-performance_page_w3tc_support', array(
'\W3TC\Support_Page',
'admin_print_scripts_w3tc_support'
) );
}
/**
* Load action
*
* @return void
*/
function load() {
$this->add_help_tabs();
$this->_page = Util_Admin::get_current_page();
// run plugin action
$action = false;
foreach ( $_REQUEST as $key => $value ) {
if ( substr( $key, 0, 5 ) == 'w3tc_' ) {
$action = $key;
break;
}
}
$executor = new Root_AdminActions();
if ( $action && $executor->exists( $action ) ) {
if ( !wp_verify_nonce( Util_Request::get_string( '_wpnonce' ), 'w3tc' ) )
wp_nonce_ays( 'w3tc' );
try {
$executor->execute( $action );
} catch ( \Exception $e ) {
$key = 'admin_action_failed_' . $action;
Util_Admin::redirect_with_custom_messages( array(),
array( $key => $e->getMessage() ) );
}
exit();
}
}
public function wp_ajax_w3tc_ajax() {
if ( !wp_verify_nonce( Util_Request::get_string( '_wpnonce' ), 'w3tc' ) )
wp_nonce_ays( 'w3tc' );
try {
$base_capability = apply_filters( 'w3tc_ajax_base_capability_', 'manage_options' );
$capability = apply_filters( 'w3tc_ajax_capability_' . Util_Request::get_string( 'w3tc_action' ),
$base_capability );
if ( !empty( $capability ) && !current_user_can( $capability ) )
throw new \Exception( 'no permissions' );
do_action( 'w3tc_ajax' );
do_action( 'w3tc_ajax_' . Util_Request::get_string( 'w3tc_action' ) );
} catch ( \Exception $e ) {
echo esc_html( $e->getMessage() );
}
exit();
}
public function wp_ajax_w3tc_monitoring_score() {
if ( !$this->_config->get_boolean( 'widget.pagespeed.show_in_admin_bar' ) )
exit();
$score = '';
$modules = Dispatcher::component( 'ModuleStatus' );
$score = apply_filters( 'w3tc_monitoring_score', $score );
header( "Content-Type: application/x-javascript; charset=UTF-8" );
echo 'document.getElementById("w3tc_monitoring_score") && ( document.getElementById("w3tc_monitoring_score").innerHTML = "' .
esc_html( strtr( $score, '"', '.' ) ) . '" );';
exit();
}
/**
* Admin init
*
* @return void
*/
function admin_init() {
// special handling for deactivation link, it's plugins.php file
if ( Util_Request::get_string( 'action' ) == 'w3tc_deactivate_plugin' ) {
Util_Activation::deactivate_plugin();
}
$page_val = Util_Request::get_string( 'page' );
if ( ! empty( $page_val ) ) {
do_action( 'admin_init_' . $page_val );
}
}
/**
* Enqueue admin scripts.
*/
public function admin_enqueue_scripts() {
wp_register_style( 'w3tc-options', plugins_url( 'pub/css/options.css', W3TC_FILE ), array(), W3TC_VERSION );
wp_register_style( 'w3tc-lightbox', plugins_url( 'pub/css/lightbox.css', W3TC_FILE ), array(), W3TC_VERSION );
wp_register_style( 'w3tc-widget', plugins_url( 'pub/css/widget.css', W3TC_FILE ), array(), W3TC_VERSION );
wp_register_script( 'w3tc-metadata', plugins_url( 'pub/js/metadata.js', W3TC_FILE ), array(), W3TC_VERSION );
wp_register_script( 'w3tc-options', plugins_url( 'pub/js/options.js', W3TC_FILE ), array(), W3TC_VERSION );
wp_register_script( 'w3tc-lightbox', plugins_url( 'pub/js/lightbox.js', W3TC_FILE ), array(), W3TC_VERSION );
wp_register_script( 'w3tc-widget', plugins_url( 'pub/js/widget.js', W3TC_FILE ), array(), W3TC_VERSION );
wp_register_script( 'w3tc-jquery-masonry', plugins_url( 'pub/js/jquery.masonry.min.js', W3TC_FILE ), array( 'jquery' ), W3TC_VERSION );
// New feature count for the Feature Showcase.
wp_register_script( 'w3tc-feature-counter', plugins_url( 'pub/js/feature-counter.js', W3TC_FILE ), array(), W3TC_VERSION, true );
wp_localize_script(
'w3tc-feature-counter',
'W3TCFeatureShowcaseData',
array(
'unseenCount' => FeatureShowcase_Plugin_Admin::get_unseen_count(),
)
);
wp_enqueue_script( 'w3tc-feature-counter' );
// Messages.
if ( !is_null( $this->w3tc_message ) &&
isset( $this->w3tc_message['actions'] ) &&
is_array( $this->w3tc_message['actions'] ) ) {
foreach ( $this->w3tc_message['actions'] as $action )
do_action( 'w3tc_message_action_' . $action );
}
// for testing.
$w3tc_message_action_val = Util_Request::get_string( 'w3tc_message_action' );
if ( ! empty( $w3tc_message_action_val ) ) {
do_action( 'w3tc_message_action_' . $w3tc_message_action_val );
}
}
// Define icon styles for the custom post type
function admin_head() {
$page = Util_Request::get_string( 'page', null );
if ( ( ! is_multisite() || is_super_admin() ) && false !== strpos( $page, 'w3tc' ) && 'w3tc_setup_guide' !== $page && ! get_site_option( 'w3tc_setupguide_completed' ) ) {
$config = new Config();
$state_master = Dispatcher::config_state_master();
if ( ! $config->get_boolean( 'pgcache.enabled' ) && $state_master->get_integer( 'common.install' ) > strtotime( 'NOW - 1 WEEK' ) ) {
wp_redirect( esc_url( network_admin_url( 'admin.php?page=w3tc_setup_guide' ) ) );
}
}
if ( 'w3tc_dashboard' === $page ) {
?>
_config->get_boolean( 'common.track_usage' ) && $this->is_w3tc_page ) {
$current_user = wp_get_current_user();
$page = Util_Request::get_string( 'page' );
if ( $page == 'w3tc_extensions' )
$page = 'extensions/' . Util_Request::get_string( 'extension' );
if ( defined( 'W3TC_DEBUG' ) && W3TC_DEBUG )
$profile = 'UA-2264433-7';
else
$profile = 'UA-2264433-8';
$state = Dispatcher::config_state();
?>
_admin_menu( 'manage_network_options' );
}
function admin_menu() {
$this->_admin_menu( 'manage_options' );
}
/**
* Admin menu
*
* @return void
*/
private function _admin_menu( $base_capability ) {
$base_capability = apply_filters( 'w3tc_capability_menu',
$base_capability );
if ( current_user_can( $base_capability ) ) {
$menus = Dispatcher::component( 'Root_AdminMenu' );
$submenu_pages = $menus->generate( $base_capability );
/**
* Only admin can modify W3TC settings
*/
foreach ( $submenu_pages as $submenu_page ) {
add_action( 'load-' . $submenu_page,
array( $this, 'load' ) );
add_action( 'admin_print_styles-' . $submenu_page,
array( $this, 'admin_print_styles' ) );
add_action( 'admin_print_scripts-' . $submenu_page,
array( $this, 'admin_print_scripts' ) );
}
global $pagenow;
if ( $pagenow == 'plugins.php' ) {
add_action( 'admin_print_scripts', array( $this, 'load_plugins_page_js' ) );
add_action( 'admin_print_styles', array( $this, 'print_plugins_page_css' ) );
}
global $pagenow;
if ( 'plugins.php' === $pagenow || $this->is_w3tc_page ||
! empty( Util_Request::get_string( 'w3tc_note' ) ) ||
! empty( Util_Request::get_string( 'w3tc_error' ) ) ||
! empty( Util_Request::get_string( 'w3tc_message' ) ) ) {
/**
* Only admin can see W3TC notices and errors
*/
add_action( 'admin_notices', array(
$this,
'admin_notices'
) );
add_action( 'network_admin_notices', array(
$this,
'admin_notices'
) );
}
}
}
/**
* Print styles
*
* @return void
*/
function admin_print_styles() {
wp_enqueue_style( 'w3tc-options' );
wp_enqueue_style( 'w3tc-lightbox' );
}
/**
* Print scripts.
*/
function admin_print_scripts() {
wp_enqueue_script( 'w3tc-metadata' );
wp_enqueue_script( 'w3tc-options' );
wp_enqueue_script( 'w3tc-lightbox' );
if ( $this->is_w3tc_page ) {
wp_localize_script(
'w3tc-options',
'w3tc_nonce',
array( wp_create_nonce( 'w3tc' ) )
);
wp_localize_script(
'w3tc-options',
'w3tcData',
array(
'cdnEnabled' => $this->_config->get_boolean( 'cdn.enabled' ),
'cdnEngine' => $this->_config->get_string( 'cdn.engine' ),
'cdnFlushManually' => $this->_config->get_boolean( 'cdn.flush_manually' ),
'cfWarning' => wp_kses(
sprintf(
// translators: 1: HTML break, 2: HTML anchor open tag, 3: HTML anchor close tag, 4: HTML anchor open tag.
__(
'Please see %2$sAmazon\'s CloudFront documentation -- Paying for file invalidation%3$s:%1$sThe first 1,000 invalidation paths that you submit per month are free; you pay for each invalidation path over 1,000 in a month.%1$sYou can disable automatic purging by enabling %4$sOnly purge CDN manually%3$s.',
'w3-total-cache'
),
'
',
'',
'',
''
),
array(
'a' => array(
'target' => array(),
'href' => array(),
),
'br' => array(),
)
),
)
);
}
switch ( $this->_page ) {
case 'w3tc_minify':
case 'w3tc_cachegroups':
wp_enqueue_script(
'w3tc_cachegroups',
plugins_url( 'CacheGroups_Plugin_Admin_View.js', W3TC_FILE ),
array(
'jquery',
'jquery-ui-sortable',
),
W3TC_VERSION,
true
);
case 'w3tc_cdn':
wp_enqueue_script( 'jquery-ui-sortable' );
break;
}
if ( $this->_page=='w3tc_cdn' )
wp_enqueue_script( 'jquery-ui-dialog' );
if ( $this->_page=='w3tc_dashboard' )
wp_enqueue_script( 'w3tc-jquery-masonry' );
}
function load_plugins_page_js() {
wp_enqueue_script( 'w3tc-options' );
}
function print_plugins_page_css() {
echo "
";
}
/**
* Contextual help list filter
*
* @param string $list
* @return string
*/
function add_help_tabs() {
$screen = get_current_screen();
$sections = Generic_Faq::sections();
$n = 0;
foreach ( $sections as $section => $data ) {
$content = '';
$screen->add_help_tab( array(
'id' => 'w3tc_faq_' . $n,
'title' => $section,
'content' => $content
) );
$n++;
}
}
public function w3tc_ajax_faq() {
$section = Util_Request::get_string( 'section' );
$entries = Generic_Faq::parse( $section );
$response = array();
ob_start();
include W3TC_DIR . '/Generic_Plugin_Admin_View_Faq.php';
$content = ob_get_contents();
ob_end_clean();
echo json_encode( array( 'content' => $content ) );
}
/**
* Plugin action links filter
*
* @param array $links
* @return array
*/
function plugin_action_links( $links ) {
array_unshift( $links,
'Settings' );
array_unshift( $links,
'Premium Support' );
if ( !is_writable( WP_CONTENT_DIR ) ||
!is_writable( Util_Rule::get_browsercache_rules_cache_path() ) ) {
$delete_link = 'Uninstall';
array_unshift( $links, $delete_link );
}
return $links;
}
/**
* favorite_actions filter
*
* @param array $actions
* @return array
*/
function favorite_actions( $actions ) {
$actions[wp_nonce_url( admin_url( 'admin.php?page=w3tc_dashboard&w3tc_flush_all' ), 'w3tc' )] = array(
__( 'Empty Caches', 'w3-total-cache' ),
apply_filters( 'w3tc_capability_favorite_action_flush_all', 'manage_options' )
);
return $actions;
}
/**
* Active plugins pre update option filter
*
* @param string $new_value
* @return string
*/
function pre_update_option_active_plugins( $new_value ) {
$old_value = (array) get_option( 'active_plugins' );
if ( $new_value !== $old_value && in_array( W3TC_FILE, (array) $new_value ) && in_array( W3TC_FILE, (array) $old_value ) ) {
$state_note = Dispatcher::config_state_note();
$state_note->set( 'common.show_note.plugins_updated', true );
}
return $new_value;
}
/**
* Show plugin changes
*
* @return void
*/
function in_plugin_update_message() {
$response = Util_Http::get( W3TC_README_URL );
if ( is_wp_error( $response ) || $response['response']['code'] != 200 )
return;
$matches = null;
$regexp = '~==\s*Changelog\s*==\s*=\s*[0-9.]+\s*=(.*)(=\s*' . preg_quote( W3TC_VERSION ) . '\s*=|$)~Uis';
$body = $response['body'];
if ( !preg_match( $regexp, $body, $matches ) )
return;
$changelog = (array) preg_split( '~[\r\n]+~', trim( $matches[1] ) );
echo '