Commit realizado el 12:13:52 08-04-2024
This commit is contained in:
@@ -0,0 +1,98 @@
|
||||
<?php
|
||||
/**
|
||||
* The AJAX.
|
||||
*
|
||||
* @since 0.9.0
|
||||
* @package RankMath
|
||||
* @subpackage RankMath\Traits
|
||||
* @author Rank Math <support@rankmath.com>
|
||||
*/
|
||||
|
||||
namespace RankMath\Traits;
|
||||
|
||||
use RankMath\Helper;
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
/**
|
||||
* Ajax class.
|
||||
*/
|
||||
trait Ajax {
|
||||
|
||||
/**
|
||||
* Hooks a function on to a specific ajax action
|
||||
*
|
||||
* @param string $tag The name of the action to which the $function_to_add is hooked.
|
||||
* @param callable $function_to_add The name of the function you wish to be called.
|
||||
* @param int $priority Optional. Used to specify the order in which the functions
|
||||
* associated with a particular action are executed. Default 10.
|
||||
* Lower numbers correspond with earlier execution,
|
||||
* and functions with the same priority are executed
|
||||
* in the order in which they were added to the action.
|
||||
*/
|
||||
protected function ajax( $tag, $function_to_add, $priority = 10 ) {
|
||||
\add_action( 'wp_ajax_rank_math_' . $tag, [ $this, $function_to_add ], $priority );
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify request nonce
|
||||
*
|
||||
* @param string $action The nonce action name.
|
||||
*/
|
||||
public function verify_nonce( $action ) {
|
||||
if ( ! isset( $_REQUEST['security'] ) || ! \wp_verify_nonce( $_REQUEST['security'], $action ) ) {
|
||||
$this->error( __( 'Error: Nonce verification failed', 'rank-math' ) );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the current user has a specific capability. If not die with error.
|
||||
*
|
||||
* @see has_cap()
|
||||
*
|
||||
* @param string $capability Capability name.
|
||||
* @return boolean Whether the current user has the given capability.
|
||||
*/
|
||||
public function has_cap_ajax( $capability ) {
|
||||
|
||||
if ( ! Helper::has_cap( $capability ) ) {
|
||||
$this->error( esc_html__( 'You are not authorized to perform this action.', 'rank-math' ) );
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrapper function for sending success response
|
||||
*
|
||||
* @param mixed $data Data to send to response.
|
||||
*/
|
||||
public function success( $data = null ) {
|
||||
$this->send( $data );
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrapper function for sending error
|
||||
*
|
||||
* @param mixed $data Data to send to response.
|
||||
*/
|
||||
public function error( $data = null ) {
|
||||
$this->send( $data, false );
|
||||
}
|
||||
|
||||
/**
|
||||
* Send AJAX response.
|
||||
*
|
||||
* @param array $data Data to send using ajax.
|
||||
* @param boolean $success Optional. If this is an error. Defaults: true.
|
||||
*/
|
||||
private function send( $data, $success = true ) {
|
||||
|
||||
if ( is_string( $data ) ) {
|
||||
$data = $success ? [ 'message' => $data ] : [ 'error' => $data ];
|
||||
}
|
||||
$data['success'] = isset( $data['success'] ) ? $data['success'] : $success;
|
||||
|
||||
\wp_send_json( $data );
|
||||
}
|
||||
}
|
@@ -0,0 +1,107 @@
|
||||
<?php
|
||||
/**
|
||||
* The Cache Trait.
|
||||
*
|
||||
* @since 1.0.99
|
||||
* @package RankMath
|
||||
* @subpackage RankMath\Traits
|
||||
* @author Rank Math <support@rankmath.com>
|
||||
*/
|
||||
|
||||
namespace RankMath\Traits;
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
/**
|
||||
* Cache class.
|
||||
*/
|
||||
trait Cache {
|
||||
|
||||
/**
|
||||
* To generate hash of object.
|
||||
*
|
||||
* @param string|array|object $object Object for that hash need to generate.
|
||||
*
|
||||
* @return string Hash value of provided object.
|
||||
*/
|
||||
public function generate_hash( $object ) {
|
||||
|
||||
if ( empty( $object ) ) {
|
||||
return '';
|
||||
}
|
||||
|
||||
if ( is_object( $object ) ) {
|
||||
$object = (array) $object;
|
||||
}
|
||||
|
||||
if ( is_array( $object ) ) {
|
||||
ksort( $object );
|
||||
$object = wp_json_encode( $object );
|
||||
}
|
||||
|
||||
$object = trim( $object );
|
||||
$hash = hash( 'sha256', $object );
|
||||
|
||||
return $hash;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a value in cache.
|
||||
*
|
||||
* The value is set whether or not this key already exists in Redis.
|
||||
*
|
||||
* @param string $key The key under which to store the value.
|
||||
* @param mixed $data The value to store.
|
||||
* @param string $group The group value appended to the $key.
|
||||
* @param int $expire The expiration time, defaults to 0.
|
||||
*
|
||||
* @return bool Returns TRUE on success or FALSE on failure.
|
||||
*/
|
||||
public function set_cache( $key, $data, $group = '', $expire = 0 ) {
|
||||
if ( false === wp_using_ext_object_cache() ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return wp_cache_set( $key, $data, $group, $expire );
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve object from cache.
|
||||
*
|
||||
* Gets an object from cache based on $key and $group.
|
||||
*
|
||||
* @param string $key The key under which to store the value.
|
||||
* @param string $group The group value appended to the $key.
|
||||
*
|
||||
* @return bool|mixed Cached object value.
|
||||
*/
|
||||
public function get_cache( $key, $group ) {
|
||||
if ( false === wp_using_ext_object_cache() ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return wp_cache_get( $key, $group );
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes all cache items in a group, if the object cache implementation supports it.
|
||||
*
|
||||
* @param string $group Name of group to remove from cache.
|
||||
*
|
||||
* @return bool True if group was flushed, false otherwise.
|
||||
*/
|
||||
public function cache_flush_group( $group ) {
|
||||
if ( false === wp_using_ext_object_cache() ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
global $wp_object_cache;
|
||||
if ( ! isset( $wp_object_cache->cache[ $group ] ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$wp_object_cache->delete_multiple( array_keys( $wp_object_cache->cache[ $group ] ), $group );
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
@@ -0,0 +1,136 @@
|
||||
<?php
|
||||
/**
|
||||
* The Hooker.
|
||||
*
|
||||
* @since 0.9.0
|
||||
* @package RankMath
|
||||
* @subpackage RankMath\Traits
|
||||
* @author Rank Math <support@rankmath.com>
|
||||
*/
|
||||
|
||||
namespace RankMath\Traits;
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
/**
|
||||
* Hooker class.
|
||||
*/
|
||||
trait Hooker {
|
||||
/**
|
||||
* Hooks a function on to a specific action
|
||||
*
|
||||
* @param string $tag The name of the action to which the $function_to_add is hooked.
|
||||
* @param callable $function_to_add The name of the function you wish to be called.
|
||||
* @param int $priority Optional. Used to specify the order in which the functions
|
||||
* associated with a particular action are executed. Default 10.
|
||||
* Lower numbers correspond with earlier execution,
|
||||
* and functions with the same priority are executed
|
||||
* in the order in which they were added to the action.
|
||||
* @param int $accepted_args Optional. The number of arguments the function accepts. Default 1.
|
||||
* @return true Will always return true.
|
||||
*/
|
||||
protected function action( $tag, $function_to_add, $priority = 10, $accepted_args = 1 ) {
|
||||
return \add_action( $tag, [ $this, $function_to_add ], $priority, $accepted_args );
|
||||
}
|
||||
|
||||
/**
|
||||
* Hook a function or method to a specific filter action
|
||||
*
|
||||
* @param string $tag The name of the filter to hook the $function_to_add callback to.
|
||||
* @param callable $function_to_add The callback to be run when the filter is applied.
|
||||
* @param int $priority Optional. Used to specify the order in which the functions
|
||||
* associated with a particular action are executed. Default 10.
|
||||
* Lower numbers correspond with earlier execution,
|
||||
* and functions with the same priority are executed
|
||||
* in the order in which they were added to the action.
|
||||
* @param int $accepted_args Optional. The number of arguments the function accepts. Default 1.
|
||||
* @return true
|
||||
*/
|
||||
protected function filter( $tag, $function_to_add, $priority = 10, $accepted_args = 1 ) {
|
||||
return \add_filter( $tag, [ $this, $function_to_add ], $priority, $accepted_args );
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes a function from a specified action hook
|
||||
*
|
||||
* @param string $tag The action hook to which the function to be removed is hooked.
|
||||
* @param callable $function_to_remove The name of the function which should be removed.
|
||||
* @param int $priority Optional. The priority of the function. Default 10.
|
||||
* @return bool Whether the function is removed.
|
||||
*/
|
||||
protected function remove_action( $tag, $function_to_remove, $priority = 10 ) {
|
||||
return \remove_action( $tag, [ $this, $function_to_remove ], $priority );
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes a function from a specified filter hook
|
||||
*
|
||||
* @param string $tag The filter hook to which the function to be removed is hooked.
|
||||
* @param callable $function_to_remove The name of the function which should be removed.
|
||||
* @param int $priority Optional. The priority of the function. Default 10.
|
||||
* @return bool Whether the function existed before it was removed.
|
||||
*/
|
||||
protected function remove_filter( $tag, $function_to_remove, $priority = 10 ) {
|
||||
return \remove_filter( $tag, [ $this, $function_to_remove ], $priority );
|
||||
}
|
||||
|
||||
/**
|
||||
* Do action with league as prefix
|
||||
*
|
||||
* @param array ...$args Action args.
|
||||
*/
|
||||
protected function do_action( ...$args ) {
|
||||
if ( empty( $args[0] ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$action = 'rank_math/' . $args[0];
|
||||
unset( $args[0] );
|
||||
|
||||
\do_action_ref_array( $action, \array_merge( [], $args ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Do filter with league as prefix
|
||||
*
|
||||
* @param array ...$args Action args.
|
||||
*/
|
||||
protected function do_filter( ...$args ) {
|
||||
if ( empty( $args[0] ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$action = 'rank_math/' . $args[0];
|
||||
unset( $args[0] );
|
||||
|
||||
return \apply_filters_ref_array( $action, \array_merge( [], $args ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Inject config into class
|
||||
*
|
||||
* @param array $config Array of configuration.
|
||||
*/
|
||||
protected function config( $config = [] ) {
|
||||
// Bail early if no config.
|
||||
if ( empty( $config ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ( $config as $key => $value ) {
|
||||
$this->$key = $value;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove 'view_query_monitor' capability for current page.
|
||||
*
|
||||
* @param bool|array $user_caps Concerned user's capabilities.
|
||||
* @return bool|array Concerned user's capabilities.
|
||||
*/
|
||||
public function filter_user_has_cap( array $user_caps ) {
|
||||
$user_caps['view_query_monitor'] = false;
|
||||
|
||||
return $user_caps;
|
||||
}
|
||||
}
|
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
/**
|
||||
* The Metadata.
|
||||
*
|
||||
* @since 0.9.0
|
||||
* @package RankMath
|
||||
* @subpackage RankMath\Traits
|
||||
* @author Rank Math <support@rankmath.com>
|
||||
*/
|
||||
|
||||
namespace RankMath\Traits;
|
||||
|
||||
use RankMath\Helper;
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
/**
|
||||
* Meta class.
|
||||
*/
|
||||
trait Meta {
|
||||
|
||||
/**
|
||||
* Get meta by object type.
|
||||
*
|
||||
* @param string $object_type Object type for destination where to save.
|
||||
* @param int $object_id Object id for destination where to save.
|
||||
* @param string $key The meta key to retrieve. If no key is provided, fetches all metadata.
|
||||
* @param bool $single Whether to return a single value.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function get_meta( $object_type, $object_id, $key = '', $single = true ) {
|
||||
$func = "get_{$object_type}_meta";
|
||||
|
||||
return $func( $object_id, $key, $single );
|
||||
}
|
||||
|
||||
/**
|
||||
* Update meta by object type.
|
||||
*
|
||||
* @param string $object_type Object type for destination where to save.
|
||||
* @param int $object_id Object id for destination where to save.
|
||||
* @param string $key Metadata key.
|
||||
* @param mixed $value Metadata value.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function update_meta( $object_type, $object_id, $key, $value ) {
|
||||
$func = "update_{$object_type}_meta";
|
||||
|
||||
if ( is_string( $key ) && is_protected_meta( $key ) && ( is_scalar( $value ) || is_array( $value ) ) ) {
|
||||
return $func( $object_id, $key, $value );
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
/**
|
||||
* The Shortcode trait.
|
||||
*
|
||||
* @since 0.9.0
|
||||
* @package RankMath
|
||||
* @subpackage RankMath\Traits
|
||||
* @author Rank Math <support@rankmath.com>
|
||||
*/
|
||||
|
||||
namespace RankMath\Traits;
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
/**
|
||||
* Trait Shortcode
|
||||
*/
|
||||
trait Shortcode {
|
||||
|
||||
/**
|
||||
* Adds a new shortcode
|
||||
*
|
||||
* @param string $tag Shortcode tag to be searched in post content.
|
||||
* @param callable $func The callback function to run when the shortcode is found.
|
||||
*/
|
||||
protected function add_shortcode( $tag, $func ) {
|
||||
\add_shortcode( $tag, [ $this, $func ] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes hook for shortcode.
|
||||
*
|
||||
* @param string $tag Shortcode tag to remove hook for.
|
||||
*/
|
||||
protected function remove_shortcode( $tag ) {
|
||||
\remove_shortcode( $tag );
|
||||
}
|
||||
}
|
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
/**
|
||||
* The Wizard pages helper.
|
||||
*
|
||||
* @since 1.0.3
|
||||
* @package RankMath
|
||||
* @subpackage RankMath\Traits
|
||||
* @author Rank Math <support@rankmath.com>
|
||||
*/
|
||||
|
||||
namespace RankMath\Traits;
|
||||
|
||||
use RankMath\Helper as GlobalHelper;
|
||||
use RankMath\Helpers\Security;
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
/**
|
||||
* Wizard class.
|
||||
*/
|
||||
trait Wizard {
|
||||
|
||||
/**
|
||||
* Output the content for the current step.
|
||||
*/
|
||||
public function body() {
|
||||
if ( ! isset( $this->steps[ $this->step ] ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( ! is_null( $this->wizard_step ) ) {
|
||||
$this->wizard_step->render( $this );
|
||||
return;
|
||||
}
|
||||
|
||||
if ( is_callable( $this->steps[ $this->step ]['view'] ) ) {
|
||||
call_user_func( $this->steps[ $this->step ]['view'], $this );
|
||||
return;
|
||||
}
|
||||
|
||||
include_once $this->steps[ $this->step ]['view'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the next step link.
|
||||
*/
|
||||
public function step_next_link() {
|
||||
$keys = array_keys( $this->steps );
|
||||
$step = array_search( $this->step, $keys, true ) + 1;
|
||||
|
||||
return Security::add_query_arg_raw(
|
||||
'step',
|
||||
isset( $keys[ $step ] ) ? $keys[ $step ] : '',
|
||||
GlobalHelper::get_admin_url( 'wizard' )
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Is the page is currrent page.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function is_current_page() {
|
||||
$page = isset( $_GET['page'] ) && ! empty( $_GET['page'] ) ? filter_input( INPUT_GET, 'page' ) : false;
|
||||
return $page === $this->slug;
|
||||
}
|
||||
}
|
@@ -0,0 +1 @@
|
||||
<?php // Silence is golden.
|
Reference in New Issue
Block a user