-
Notifications
You must be signed in to change notification settings - Fork 3
/
disciple-tools-streams.php
executable file
·354 lines (316 loc) · 10.8 KB
/
disciple-tools-streams.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
<?php
/**
*Plugin Name: Disciple.Tools - Streams
* Plugin URI: https://github.com/DiscipleTools/disciple-tools-streams
* Description: Streams creates collections of work and a generational census reporting magic link system.
* Text Domain: disciple-tools-streams
* Domain Path: /languages
* Version: 3.1.6
* Author URI: https://github.com/DiscipleTools
* GitHub Plugin URI: https://github.com/DiscipleTools/disciple-tools-streams
* Requires at least: 4.7.0
* (Requires 4.7+ because of the integration of the REST API at 4.7 and the security requirements of this milestone version.)
* Tested up to: 6.2
*
* @package Disciple_Tools
* @link https://github.com/DiscipleTools
* @license GPL-2.0 or later
* https://www.gnu.org/licenses/gpl-2.0.html
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
$dt_streams_required_dt_theme_version = '1.0';
/**
* Gets the instance of the `DT_Streams` class.
*
* @since 0.1
* @access public
* @return object|bool;
*/
function dt_streams() {
global $dt_streams_required_dt_theme_version;
$wp_theme = wp_get_theme();
$version = $wp_theme->version;
/*
* Check if the Disciple.Tools theme is loaded and is the latest required version
*/
$is_theme_dt = class_exists( "Disciple_Tools" );
if ( $is_theme_dt && version_compare( $version, $dt_streams_required_dt_theme_version, "<" ) ) {
add_action( 'admin_notices', 'dt_streams_hook_admin_notice' );
add_action( 'wp_ajax_dismissed_notice_handler', 'dt_hook_ajax_notice_handler' );
return false;
}
if ( !$is_theme_dt ){
return false;
}
/**
* Load useful function from the theme
*/
if ( !defined( 'DT_FUNCTIONS_READY' ) ){
require_once get_template_directory() . '/dt-core/global-functions.php';
}
/*
* Don't load the plugin on every rest request. Only those with the metrics namespace
*/
return DT_Streams::get_instance();
}
add_action( 'after_setup_theme', 'dt_streams', 21 );
/**
* Singleton class for setting up the plugin.
*
* @since 0.1
* @access public
*/
class DT_Streams {
/**
* Declares public variables
*
* @since 0.1
* @access public
* @return object
*/
public $token;
public $version;
public $dir_path = '';
public $dir_uri = '';
public $img_uri = '';
public $includes_path;
/**
* Returns the instance.
*
* @since 0.1
* @access public
* @return object
*/
public static function get_instance() {
static $instance = null;
if ( is_null( $instance ) ) {
$instance = new dt_streams();
$instance->setup();
$instance->includes();
$instance->setup_actions();
}
return $instance;
}
/**
* Constructor method.
*
* @since 0.1
* @access private
* @return void
*/
private function __construct() {
}
/**
* Loads files needed by the plugin.
*
* @since 0.1
* @access public
* @return void
*/
private function includes() {
/* post type */
require_once( 'post-type/streams-post-type.php' );
require_once( 'post-type/bulk-list-extension.php' );
require_once( 'magic/magic-url-self-register.php' );
require_once( 'admin/customize-site-linking.php' );
/* metrics */
require_once( 'metrics/charts-loader.php' );
/* magic links */
require_once( 'magic/magic-map.php' );
require_once( 'magic/magic-reports.php' );
require_once( 'magic/magic-public-register.php' );
require_once( 'magic/magic-retrieve.php' );
require_once( 'magic/magic-join.php' );
require_once( 'magic/magic-create-child.php' );
}
/**
* Sets up globals.
*
* @since 0.1
* @access public
* @return void
*/
private function setup() {
// Main plugin directory path and URI.
$this->dir_path = trailingslashit( plugin_dir_path( __FILE__ ) );
$this->dir_uri = trailingslashit( plugin_dir_url( __FILE__ ) );
// Admin and settings variables
$this->token = 'dt_streams';
$this->version = '2.0';
}
/**
* Sets up main plugin actions and filters.
*
* @since 0.1
* @access public
* @return void
*/
private function setup_actions() {
// Internationalize the text strings used.
add_action( 'after_setup_theme', array( $this, 'i18n' ), 50 );
}
/**
* Method that runs only when the plugin is activated.
*
* @since 0.1
* @access public
* @return void
*/
public static function activation() {
// Confirm 'Administrator' has 'manage_dt' privilege. This is key in 'remote' configuration when
// Disciple.Tools theme is not installed, otherwise this will already have been installed by the Disciple.Tools Theme
$role = get_role( 'administrator' );
if ( !empty( $role ) ) {
$role->add_cap( 'manage_dt' ); // gives access to dt plugin options
}
}
/**
* Method that runs only when the plugin is deactivated.
*
* @since 0.1
* @access public
* @return void
*/
public static function deactivation() {
delete_option( 'dismissed-dt-events' );
}
/**
* Loads the translation files.
*
* @since 0.1
* @access public
* @return void
*/
public function i18n() {
$domain = 'disciple-tools-streams'; // this must be the same as the slug for the plugin
$locale = apply_filters(
'plugin_locale',
( is_admin() && function_exists( 'get_user_locale' ) ) ? get_user_locale() : get_locale(),
$domain
);
$mo_file = $domain . '-' . $locale . '.mo';
$path = realpath( dirname( __FILE__ ) . '/languages' );
if ($path && file_exists( $path )) {
load_textdomain( $domain, $path . '/' . $mo_file );
}
}
/**
* Magic method to output a string if trying to use the object as a string.
*
* @since 0.1
* @access public
* @return string
*/
public function __toString() {
return 'disciple-tools-streams';
}
/**
* Magic method to keep the object from being cloned.
*
* @since 0.1
* @access public
* @return void
*/
public function __clone() {
_doing_it_wrong( __FUNCTION__, esc_html__( 'Whoah, partner!', 'dt_streams' ), '0.1' );
}
/**
* Magic method to keep the object from being unserialized.
*
* @since 0.1
* @access public
* @return void
*/
public function __wakeup() {
_doing_it_wrong( __FUNCTION__, esc_html__( 'Whoah, partner!', 'dt_streams' ), '0.1' );
}
/**
* Magic method to prevent a fatal error when calling a method that doesn't exist.
*
* @since 0.1
* @access public
* @return null
*/
public function __call( $method = '', $args = array() ) {
// @codingStandardsIgnoreLine
_doing_it_wrong( "dt_streams::{$method}", esc_html__( 'Method does not exist.', 'dt_streams' ), '0.1' );
unset( $method, $args );
return null;
}
}
// end main plugin class
// Register activation hook.
register_activation_hook( __FILE__, [ 'DT_Streams', 'activation' ] );
register_deactivation_hook( __FILE__, [ 'DT_Streams', 'deactivation' ] );
function dt_streams_hook_admin_notice() {
global $dt_streams_required_dt_theme_version;
$wp_theme = wp_get_theme();
$current_version = $wp_theme->version;
$message = "'Disciple.Tools - Streams' plugin requires 'Disciple.Tools' theme to work. Please activate 'Disciple.Tools' theme or make sure it is latest version.";
if ( $wp_theme->get_template() === "disciple-tools-theme" ){
$message .= ' ' . sprintf( esc_html( 'Current Disciple.Tools version: %1$s, required version: %2$s' ), esc_html( $current_version ), esc_html( $dt_streams_required_dt_theme_version ) );
}
// Check if it's been dismissed...
if ( ! get_option( 'dismissed-dt-events', false ) ) { ?>
<div class="notice notice-error notice-dt-events is-dismissible" data-notice="dt-events">
<p><?php echo esc_html( $message );?></p>
</div>
<script>
jQuery(function($) {
$( document ).on( 'click', '.notice-dt-events .notice-dismiss', function () {
$.ajax( ajaxurl, {
type: 'POST',
data: {
action: 'dismissed_notice_handler',
type: 'dt-events',
security: '<?php echo esc_html( wp_create_nonce( 'wp_rest_dismiss' ) ) ?>'
}
})
});
});
</script>
<?php }
}
/**
* AJAX handler to store the state of dismissible notices.
*/
if ( !function_exists( "dt_hook_ajax_notice_handler" )){
function dt_hook_ajax_notice_handler(){
check_ajax_referer( 'wp_rest_dismiss', 'security' );
if ( isset( $_POST["type"] ) ){
$type = sanitize_text_field( wp_unslash( $_POST["type"] ) );
update_option( 'dismissed-' . $type, true );
}
}
}
/**
* Check for plugin updates even when the active theme is not Disciple.Tools
*
* Below is the publicly hosted .json file that carries the version information. This file can be hosted
* anywhere as long as it is publicly accessible. You can download the version file listed below and use it as
* a template.
* Also, see the instructions for version updating to understand the steps involved.
* @see https://github.com/DiscipleTools/disciple-tools-version-control/wiki/How-to-Update-the-Starter-Plugin
*/
add_action( 'plugins_loaded', function (){
if ( is_admin() && !( is_multisite() && class_exists( "DT_Multisite" ) ) || wp_doing_cron() ){
if ( ! class_exists( 'Puc_v4_Factory' ) ) {
// find the Disciple.Tools theme and load the plugin update checker.
foreach ( wp_get_themes() as $theme ){
if ( $theme->get( 'TextDomain' ) === "disciple_tools" && file_exists( $theme->get_stylesheet_directory() . '/dt-core/libraries/plugin-update-checker/plugin-update-checker.php' ) ){
require( $theme->get_stylesheet_directory() . '/dt-core/libraries/plugin-update-checker/plugin-update-checker.php' );
}
}
}
if ( class_exists( 'Puc_v4_Factory' ) ){
$hosted_json = "https://raw.githubusercontent.com/DiscipleTools/disciple-tools-streams/master/version-control.json";
Puc_v4_Factory::buildUpdateChecker(
$hosted_json,
__FILE__,
'disciple-tools-streams'
);
}
}
} );