',
Util_Ui::button_hide_note( 'Hide this message', 'cloudflare_plugin' ),
'
'
),
array(
'div' => array(
'class' => array(),
),
'input' => array(
'class' => array(),
'value' => array(),
'type' => array(),
'onclick' => array(),
),
'p' => array(),
)
);
}
}
/**
* Adds Cloudflare-specific menu items to the WordPress admin bar.
*
* @param array $menu_items Existing admin bar menu items.
*
* @return array Updated admin bar menu items.
*/
public function w3tc_admin_bar_menu( $menu_items ) {
$menu_items['20810.cloudflare'] = array(
'id' => 'w3tc_flush_cloudflare',
'parent' => 'w3tc_flush',
'title' => __( 'Cloudflare', 'w3-total-cache' ),
'href' => wp_nonce_url( admin_url( 'admin.php?page=w3tc_dashboard&w3tc_cloudflare_flush' ), 'w3tc' ),
);
return $menu_items;
}
/**
* Checks and updates IP versions for Cloudflare.
*
* @return void
*/
public function check_ip_versions() {
$state = Dispatcher::config_state_master();
if ( $state->get_integer( 'extension.cloudflare.next_ips_check' ) < time() ) {
// update asap to avoid multiple processes entering the check.
$state->set( 'extension.cloudflare.next_ips_check', time() + 7 * 24 * 60 * 60 );
$data = array();
try {
$data = $this->api->get_ip_ranges();
} catch ( \Exception $ex ) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch
// Missing exception handling?
}
if ( isset( $data['ip4'] ) ) {
$state->set( 'extension.cloudflare.ips.ip4', $data['ip4'] );
}
if ( isset( $data['ip6'] ) ) {
$state->set( 'extension.cloudflare.ips.ip6', $data['ip6'] );
}
$state->save();
}
}
/**
* Handles AJAX requests for Cloudflare API actions.
*
* @return void
*
* @throws \Exception If API request fails.
*/
public function action_cloudflare_api_request() {
$result = false;
$response = null;
$actions = array(
'dev_mode',
'sec_lvl',
'fpurge_ts',
);
$email = Util_Request::get_string( 'email' );
$key = Util_Request::get_string( 'key' );
$zone = Util_Request::get_string( 'zone' );
$action = Util_Request::get_string( 'command' );
$value = Util_Request::get_string( 'value' );
$nonce = Util_Request::get_string( '_wpnonce' );
if ( ! wp_verify_nonce( $nonce, 'w3tc' ) ) {
$error = 'Access denied.';
} elseif ( ! $email ) {
$error = 'Empty email.';
} elseif ( ! filter_var( $email, FILTER_VALIDATE_EMAIL ) ) {
$error = 'Invalid email.';
} elseif ( ! $key ) {
$error = 'Empty token / global key.';
} elseif ( ! $zone ) {
$error = 'Empty zone.';
} elseif ( strpos( $zone, '.' ) === false ) {
$error = 'Invalid domain.';
} elseif ( ! in_array( $action, $actions, true ) ) {
$error = 'Invalid action.';
} else {
$config = array(
'email' => $email,
'key' => $key,
'zone' => $zone,
);
@$this->api = new Extension_CloudFlare_Api( $config );
@set_time_limit( $this->_config->get_integer( array( 'cloudflare', 'timelimit.api_request' ) ) );
$response = $this->api->api_request( $action, $value );
if ( $response ) {
if ( 'success' === $response->result ) {
$result = true;
$error = 'OK';
} else {
$error = $response->msg;
}
} else {
$error = 'Unable to make Cloudflare API request.';
}
}
$return = array(
'result' => $result,
'error' => $error,
'response' => $response,
);
wp_send_json( $return );
}
/**
* Adds Cloudflare admin actions for handling configuration.
*
* @param array $handlers Existing handlers.
*
* @return array Updated handlers.
*/
public function w3tc_admin_actions( $handlers ) {
$handlers['cloudflare'] = 'Extension_CloudFlare_AdminActions';
return $handlers;
}
/**
* Enqueues scripts for the Cloudflare dashboard.
*
* @return void
*/
public function admin_print_scripts_w3tc_dashboard() {
wp_enqueue_script(
'w3tc_extension_cloudflare_dashboard',
plugins_url( 'Extension_CloudFlare_View_Dashboard.js', W3TC_FILE ),
array( 'jquery' ),
'1.0',
true
);
}
/**
* Adds Cloudflare-related actions to the W3 Total Cache dashboard.
*
* @param array $actions Existing actions.
*
* @return array Updated actions.
*/
public function w3tc_dashboard_actions( $actions ) {
$email = $this->_config->get_string( array( 'cloudflare', 'email' ) );
$key = $this->_config->get_string( array( 'cloudflare', 'key' ) );
if ( empty( $email ) || empty( $key ) ) {
return $actions;
}
$modules = Dispatcher::component( 'ModuleStatus' );
$can_empty_memcache = $modules->can_empty_memcache();
$can_empty_opcode = $modules->can_empty_opcode();
$can_empty_file = $modules->can_empty_file();
$can_empty_varnish = $modules->can_empty_varnish();
$actions[] = sprintf(
'