This repository has been archived by the owner on Oct 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Pods_AJAX_Views.php
195 lines (157 loc) · 5.23 KB
/
Pods_AJAX_Views.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php
/**
* Class Pods_AJAX_Views
*/
class Pods_AJAX_Views {
/**
* @var bool $compatible Whether plugin is compatible with Pods install
*/
public static $compatible = null;
/**
* Setup default constants, add hooks
*/
public static function init() {
// Default stats tracking and advanced functionality is off
if ( ! defined( 'PODS_AJAX_VIEWS_STATS' ) ) {
define( 'PODS_AJAX_VIEWS_STATS', false );
}
if ( ! defined( 'PODS_AJAX_VIEWS_OVERRIDE' ) ) {
define( 'PODS_AJAX_VIEWS_OVERRIDE', false );
}
if ( is_admin() ) {
include_once 'Pods_AJAX_Views_Admin.php';
// Init admin
add_action( 'init', [ 'Pods_AJAX_Views_Admin', 'init' ] );
// Register assets
add_action( 'admin_enqueue_scripts', [ __CLASS__, 'register_assets' ] );
} else {
include_once 'Pods_AJAX_Views_Frontend.php';
// Init frontend
add_action( 'init', [ 'Pods_AJAX_Views_Frontend', 'init' ] );
// Register assets
add_action( 'wp_enqueue_scripts', [ __CLASS__, 'register_assets' ] );
}
}
/**
* Check if plugin is compatible with Pods install
*
* @return bool
*/
public static function is_compatible() {
// See if compatible has been checked yet, if not, check it and set it
if ( null === self::$compatible ) {
// Default compatible is false
self::$compatible = false;
// Check if Pods is installed, that it's 2.4+, and that pods_view exists
if ( defined( 'PODS_VERSION' ) && version_compare( '2.4.1', PODS_VERSION, '<=' ) && function_exists( 'pods_view' ) ) {
// Set compatible to true for future reference
self::$compatible = true;
// Setup plugin if not yet setup
if ( PODS_AJAX_VIEWS_VERSION !== get_option( 'pods_ajax_views_version' ) ) {
self::activate();
}
}
}
return self::$compatible;
}
/**
* Activate plugin routine
*/
public static function activate() {
// Create table for stats tracking and other advanced features
if ( defined( 'PODS_AJAX_VIEWS_STATS' ) && PODS_AJAX_VIEWS_STATS ) {
/**
* @var $wpdb wpdb
*/ global $wpdb;
// Table definitions
$tables = [];
$tables[] = "
CREATE TABLE `{$wpdb->prefix}podsviews` (
`view_id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`cache_key` VARCHAR(255) NOT NULL,
`cache_mode` VARCHAR(14) NOT NULL,
`uri` VARCHAR(255) NOT NULL,
`view` LONGTEXT NOT NULL,
`view_data` LONGTEXT NOT NULL,
`expires` INT(10) UNSIGNED NOT NULL,
`avg_time` DECIMAL(20,3) NOT NULL,
`total_time` DECIMAL(20,3) NOT NULL,
`total_calls` BIGINT(20) UNSIGNED NOT NULL,
`last_generated` DATETIME NOT NULL,
`tracking_data` LONGTEXT NOT NULL,
PRIMARY KEY (`view_id`),
UNIQUE KEY `cache_key_mode_uri` (`cache_key`, `cache_mode`, `uri`)
)
";
// Create / alter table handling
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $tables );
}
// Update version in DB
update_option( 'pods_ajax_views_version', PODS_AJAX_VIEWS_VERSION );
}
/**
* Deactivate plugin routine
*/
public static function deactivate() {
/**
* @var $wpdb wpdb
*/ global $wpdb;
include_once 'Pods_AJAX_Views_Frontend.php';
// Reset AJAX View data
Pods_AJAX_Views_Frontend::reset_ajax_views();
// Delete table if it exists
$wpdb->query( "DROP TABLE IF EXISTS `{$wpdb->prefix}podsviews`" );
}
/**
* Register assets for Pods AJAX Views
*/
public static function register_assets() {
// Register JS script for Pods AJAX View processing
wp_register_script( 'pods-ajax-views', plugins_url( 'js/pods-ajax-views.js', __FILE__ ), [ 'jquery' ], PODS_AJAX_VIEWS_VERSION, true );
$is_admin = is_admin();
$additional_urls = [];
// Clear page cache for URL when finished loading all AJAX Views for a page
// so that the next time the page is loaded, no AJAX is used
if ( ! $is_admin && ! is_user_logged_in() ) {
$clean_anon_cache = false;
// WPEngine
if ( defined( 'WPE_PLUGIN_VERSION' ) ) {
$clean_anon_cache = true;
} // W3 Total Cache
elseif ( 1 == 0 ) {
$clean_anon_cache = true;
} // Others?
elseif ( 1 == 0 ) {
$clean_anon_cache = true;
}
// Add AJAX request to clean anon cache
if ( $clean_anon_cache ) {
include_once 'Pods_AJAX_Views_Frontend.php';
$uri = Pods_AJAX_Views_Frontend::get_uri();
// Build nonce action from request
$nonce_action = 'pods-ajax-view-' . md5( $uri ) . '/clean';
// Build nonce from action
$nonce = wp_create_nonce( $nonce_action );
$ajax_args = [
'pods_ajax_view_action' => 'clean_anon_cache',
'pods_ajax_view_url' => $uri,
'pods_ajax_view_nonce' => $nonce,
];
$ajax_uri = add_query_arg( $ajax_args, $uri );
$additional_urls[] = $ajax_uri;
}
}
// Setup config values for reference
$config = [
'ajax_url' => admin_url( 'admin-ajax.php' ),
'version' => PODS_AJAX_VIEWS_VERSION,
'is_admin' => $is_admin,
'status_complete' => __( 'Pods AJAX View generated successfully', 'pods-ajax-views' ),
'status_complete_plural' => __( 'Pods AJAX Views generated successfully', 'pods-ajax-views' ),
'additional_urls' => $additional_urls,
];
// Setup variable for output when JS enqueued
wp_localize_script( 'pods-ajax-views', 'pods_ajax_views_config', $config );
}
}