Diff: STRATO-apps/wordpress_03/app/wp-content/plugins/seo-by-rank-math/includes/traits/class-ajax.php
Keine Baseline-Datei – Diff nur gegen leer.
1
-
1
+
<?php
2
+
/**
3
+
* The AJAX.
4
+
*
5
+
* @since 0.9.0
6
+
* @package RankMath
7
+
* @subpackage RankMath\Traits
8
+
* @author Rank Math <support@rankmath.com>
9
+
*/
10
+
11
+
namespace RankMath\Traits;
12
+
13
+
use RankMath\Helper;
14
+
15
+
defined( 'ABSPATH' ) || exit;
16
+
17
+
/**
18
+
* Ajax class.
19
+
*/
20
+
trait Ajax {
21
+
22
+
/**
23
+
* Hooks a function on to a specific ajax action
24
+
*
25
+
* @param string $tag The name of the action to which the $function_to_add is hooked.
26
+
* @param callable $function_to_add The name of the function you wish to be called.
27
+
* @param int $priority Optional. Used to specify the order in which the functions
28
+
* associated with a particular action are executed. Default 10.
29
+
* Lower numbers correspond with earlier execution,
30
+
* and functions with the same priority are executed
31
+
* in the order in which they were added to the action.
32
+
*/
33
+
protected function ajax( $tag, $function_to_add, $priority = 10 ) {
34
+
\add_action( 'wp_ajax_rank_math_' . $tag, [ $this, $function_to_add ], $priority );
35
+
}
36
+
37
+
/**
38
+
* Verify request nonce
39
+
*
40
+
* @param string $action The nonce action name.
41
+
*/
42
+
public function verify_nonce( $action ) {
43
+
if ( ! isset( $_REQUEST['security'] ) || ! \wp_verify_nonce( sanitize_key( $_REQUEST['security'] ), $action ) ) {
44
+
$this->error( __( 'Error: Nonce verification failed', 'rank-math' ) );
45
+
}
46
+
}
47
+
48
+
/**
49
+
* Whether the current user has a specific capability. If not die with error.
50
+
*
51
+
* @see has_cap()
52
+
*
53
+
* @param string $capability Capability name.
54
+
* @return boolean Whether the current user has the given capability.
55
+
*/
56
+
public function has_cap_ajax( $capability ) {
57
+
58
+
if ( ! Helper::has_cap( $capability ) ) {
59
+
$this->error( esc_html__( 'You are not authorized to perform this action.', 'rank-math' ) );
60
+
}
61
+
62
+
return true;
63
+
}
64
+
65
+
/**
66
+
* Wrapper function for sending success response
67
+
*
68
+
* @param mixed $data Data to send to response.
69
+
*/
70
+
public function success( $data = null ) {
71
+
$this->send( $data );
72
+
}
73
+
74
+
/**
75
+
* Wrapper function for sending error
76
+
*
77
+
* @param mixed $data Data to send to response.
78
+
*/
79
+
public function error( $data = null ) {
80
+
$this->send( $data, false );
81
+
}
82
+
83
+
/**
84
+
* Send AJAX response.
85
+
*
86
+
* @param array $data Data to send using ajax.
87
+
* @param boolean $success Optional. If this is an error. Defaults: true.
88
+
*/
89
+
private function send( $data, $success = true ) {
90
+
91
+
if ( is_string( $data ) ) {
92
+
$data = $success ? [ 'message' => $data ] : [ 'error' => $data ];
93
+
}
94
+
$data['success'] = isset( $data['success'] ) ? $data['success'] : $success;
95
+
96
+
\wp_send_json( $data );
97
+
}
98
+
}
99
+