-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathplugin.php
executable file
·144 lines (115 loc) · 4.09 KB
/
plugin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
/**
* Plugin Name: No-Admin-Ajax
* Plugin URI: https://github.com/devgeniem/wp-no-admin-ajax
* Description: A plugin that lightens the WP AJAX routine and directs the requests to front-end rather than admin back-end.
* Author: Miika Arponen / Geniem Oy
* Author URI: http://www.geniem.com
* License: MIT
* License URI: https://github.com/devgeniem/wp-no-admin-ajax/blob/master/LICENSE
* Version: 1.0.2
*/
namespace Geniem\Helper;
class No_Admin_Ajax {
public $keyword;
protected static $already_run = false;
public function __construct() {
// This class should be run only once
if ( self::$already_run === true ) {
return false;
}
else {
self::$already_run = true;
}
add_action( "after_setup_theme", array( $this, "init" ) );
}
public function init() {
// Rewrite only public side admin urls
if ( ! is_admin() ) {
add_filter( 'admin_url', array( $this, 'redirect_ajax_url' ), 11, 3 );
add_action( "template_redirect", array( $this, "run_ajax" ) );
}
// Register activation hook to flush the rewrites
register_activation_hook( __FILE__, array( $this, "activate" ) );
add_action( "init", array( $this, "rewrite" ) );
// Url keyword to use for the ajax calls. Is modifiable with filter "no-admin-ajax/keyword"
if ( defined('WP_NO_ADMIN_AJAX_URL') ) {
// keyword doesn't need to contain slashes because they are set in redirect_ajax_url()
// trim slashes to avoid confusion
$default_keyword = trim(WP_NO_ADMIN_AJAX_URL,'/');
} else {
$default_keyword = "no-admin-ajax";
}
$this->keyword = apply_filters( "no-admin-ajax/keyword", $default_keyword );
}
// Function that handles rewriting the admin-ajax url to the one we want
public function redirect_ajax_url( $url, $path, $blog_id ) {
if( strpos( $url, 'admin-ajax' ) ) {
return home_url( "/". $this->keyword ."/" );
}
else {
return $url;
}
}
// Creates the rewrite
public function rewrite() {
global $wp_rewrite;
add_rewrite_tag( "%no-admin-ajax%", "([0-9]+)" );
// The whole ajax url matching pattern can be altered with filter "no-admin-ajax/rule"
$default_rule = "^". $this->keyword ."/?$";
$rule = apply_filters( "no-admin-ajax/rule", $default_rule );
add_rewrite_rule(
$rule,
"index.php?no-admin-ajax=true",
"top"
);
}
// Runs the ajax calls. Equivalent to the real admin-ajax.php
public function run_ajax() {
global $wp_query;
if ( $wp_query->get("no-admin-ajax") ) {
// Constant for plugins to know that we are on an AJAX request
define("DOING_AJAX", true);
// If we don't have an action, do nothing
if ( ! isset( $_REQUEST["action"] ) ) {
die(0);
}
// Escape the parameter to prevent disastrous things
$action = esc_attr( $_REQUEST["action"] );
// Run customized no-admin-ajax methods with action "no-admin-ajax/before"
do_action( "no-admin-ajax/before" );
// Run customized no-admin-ajax methods for specific ajax actions with "no-admin-ajax/before/{action}"
do_action( "no-admin-ajax/before/". $action );
// Same headers as WordPress normal AJAX routine sends
$default_headers = array(
"Content-Type: text/html; charset=" . get_option( "blog_charset" ),
"X-Robots-Tag: noindex"
);
// Filter to customize the headers sent by ajax calls
$headers = apply_filters( "no-admin-ajax/headers", $default_headers );
// Send the headers to the user
if ( is_array( $headers ) && count( $headers ) > 0 ) {
foreach ( $headers as $header ) {
@header( $header );
}
}
send_nosniff_header();
nocache_headers();
// Run the actions
if(is_user_logged_in()) {
do_action( "wp_ajax_" . $action );
}
else {
do_action( "wp_ajax_nopriv_" . $action );
}
die(0);
}
}
// Run activate during plugin activation
public function activate() {
global $wp_rewrite;
$this->rewrite();
$wp_rewrite->flush_rules();
}
}
new No_Admin_Ajax();