-
Notifications
You must be signed in to change notification settings - Fork 9
/
disciple-tools-plugin-starter-template.php
executable file
·364 lines (330 loc) · 14.4 KB
/
disciple-tools-plugin-starter-template.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
355
356
357
358
359
360
361
362
363
364
<?php
/**
* Plugin Name: Disciple.Tools - Plugin Starter Template
* Plugin URI: https://github.com/DiscipleTools/disciple-tools-plugin-starter-template
* Description: Disciple.Tools - Plugin Starter Template is intended to help developers and integrator jumpstart their extension of the Disciple.Tools system.
* Text Domain: disciple-tools-plugin-starter-template
* Domain Path: /languages
* Version: 0.1
* Author URI: https://github.com/DiscipleTools
* GitHub Plugin URI: https://github.com/DiscipleTools/disciple-tools-plugin-starter-template
* 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: 5.6
*
* @package Disciple_Tools
* @link https://github.com/DiscipleTools
* @license GPL-2.0 or later
* https://www.gnu.org/licenses/gpl-2.0.html
*/
/**
* Refactoring (renaming) this plugin as your own:
* 1. @todo Rename the `disciple-tools-plugin-starter-template.php file.
* 2. @todo Refactor all occurrences of the name Disciple_Tools_Plugin_Starter_Template, disciple_tools_plugin_starter_template, disciple-tools-plugin-starter-template, starter_post_type, and "Plugin Starter Template"
* 3. @todo Update the README.md and LICENSE
* 4. @todo Update the default.pot file if you intend to make your plugin multilingual. Use a tool like POEdit
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
/**
* Gets the instance of the `Disciple_Tools_Plugin_Starter_Template` class.
*
* @since 0.1
* @access public
* @return object|bool
*/
function disciple_tools_plugin_starter_template() {
$disciple_tools_plugin_starter_template_required_dt_theme_version = '1.19';
$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, $disciple_tools_plugin_starter_template_required_dt_theme_version, '<' ) ) {
add_action( 'admin_notices', 'disciple_tools_plugin_starter_template_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';
}
return Disciple_Tools_Plugin_Starter_Template::instance();
}
add_action( 'after_setup_theme', 'disciple_tools_plugin_starter_template', 20 );
//register the D.T Plugin
add_filter( 'dt_plugins', function ( $plugins ){
$plugin_data = get_file_data( __FILE__, [ 'Version' => 'Version', 'Plugin Name' => 'Plugin Name' ], false );
$plugins['disciple-tools-plugin-starter-template'] = [
'plugin_url' => trailingslashit( plugin_dir_url( __FILE__ ) ),
'version' => $plugin_data['Version'] ?? null,
'name' => $plugin_data['Plugin Name'] ?? null,
];
return $plugins;
});
/**
* Singleton class for setting up the plugin.
*
* @since 0.1
* @access public
*/
class Disciple_Tools_Plugin_Starter_Template {
private static $_instance = null;
public static function instance() {
if ( is_null( self::$_instance ) ) {
self::$_instance = new self();
}
return self::$_instance;
}
private function __construct() {
$is_rest = dt_is_rest();
/**
* @todo Decide if you want to use the REST API example
* To remove: delete this following line and remove the folder named /rest-api
*/
if ( $is_rest && strpos( dt_get_url_path(), 'disciple-tools-plugin-starter-template' ) !== false ) {
require_once( 'rest-api/rest-api.php' ); // adds starter rest api class
}
/**
* @todo Decide if you want to create a new post type
* To remove: delete the line below and remove the folder named /post-type
*/
require_once( 'post-type/loader.php' ); // add starter post type extension to Disciple.Tools system
/**
* @todo Decide if you want to create a custom site-to-site link
* To remove: delete the line below and remove the folder named /site-link
*/
require_once( 'site-link/custom-site-to-site-links.php' ); // add site to site link class and capabilities
/**
* @todo Decide if you want to add new charts to the metrics section
* To remove: delete the line below and remove the folder named /charts
*/
if ( strpos( dt_get_url_path(), 'metrics' ) !== false || ( $is_rest && strpos( dt_get_url_path(), 'disciple-tools-plugin-starter-template-metrics' ) !== false ) ){
require_once( 'charts/charts-loader.php' ); // add custom charts to the metrics area
}
/**
* @todo Decide if you want to add a custom tile or settings page tile
* To remove: delete the lines below and remove the folder named /tile
*/
require_once( 'tile/custom-tile.php' ); // add custom tile
if ( 'settings' === dt_get_url_path() && ! $is_rest ) {
require_once( 'tile/profile-settings-tile.php' ); // add custom settings page tile
}
/**
* @todo Decide if you want to create a magic link
* To remove: delete the line below and remove the folder named /magic-link
*/
require_once( 'magic-link/post-type-magic-link/magic-link-post-type.php' );
require_once( 'magic-link/magic-link-user-app.php' );
require_once( 'magic-link/magic-link-login-user-app.php' );
require_once( 'magic-link/magic-link-non-object.php' );
require_once( 'magic-link/magic-link-map.php' );
// require_once( 'magic-link/magic-link-home.php' );
/**
* @todo Decide if you want to add a custom admin page in the admin area
* To remove: delete the 3 lines below and remove the folder named /admin
*/
if ( is_admin() ) {
require_once( 'admin/admin-menu-and-tabs.php' ); // adds starter admin page and section for plugin
}
/**
* @todo Decide if you want to support localization of your plugin
* To remove: delete the line below and remove the folder named /languages
*/
$this->i18n();
/**
* @todo Decide if you want to customize links for your plugin in the plugin admin area
* To remove: delete the lines below and remove the function named "plugin_description_links"
*/
if ( is_admin() ) { // adds links to the plugin description area in the plugin admin list.
add_filter( 'plugin_row_meta', [ $this, 'plugin_description_links' ], 10, 4 );
}
/**
* @todo Decide if you want to create default workflows
* To remove: delete the line below and remove the folder named /workflows
*/
require_once( 'workflows/workflows.php' );
}
/**
* Filters the array of row meta for each/specific plugin in the Plugins list table.
* Appends additional links below each/specific plugin on the plugins page.
*/
public function plugin_description_links( $links_array, $plugin_file_name, $plugin_data, $status ) {
if ( strpos( $plugin_file_name, basename( __FILE__ ) ) ) {
// You can still use `array_unshift()` to add links at the beginning.
$links_array[] = '<a href="https://disciple.tools">Disciple.Tools Community</a>'; // @todo replace with your links.
// @todo add other links here
}
return $links_array;
}
/**
* Method that runs only when the plugin is activated.
*
* @since 0.1
* @access public
* @return void
*/
public static function activation() {
// add elements here that need to fire on activation
}
/**
* Method that runs only when the plugin is deactivated.
*
* @since 0.1
* @access public
* @return void
*/
public static function deactivation() {
// add functions here that need to happen on deactivation
delete_option( 'dismissed-disciple-tools-plugin-starter-template' );
}
/**
* Loads the translation files.
*
* @since 0.1
* @access public
* @return void
*/
public function i18n() {
$domain = 'disciple-tools-plugin-starter-template';
load_plugin_textdomain( $domain, false, trailingslashit( dirname( plugin_basename( __FILE__ ) ) ). 'languages' );
}
/**
* 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-plugin-starter-template';
}
/**
* Magic method to keep the object from being cloned.
*
* @since 0.1
* @access public
* @return void
*/
public function __clone() {
_doing_it_wrong( __FUNCTION__, 'Whoah, partner!', '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__, 'Whoah, partner!', '0.1' );
}
/**
* Magic method to prevent a fatal error when calling a method that doesn't exist.
*
* @param string $method
* @param array $args
* @return null
* @since 0.1
* @access public
*/
public function __call( $method = '', $args = array() ) {
_doing_it_wrong( 'disciple_tools_plugin_starter_template::' . esc_html( $method ), 'Method does not exist.', '0.1' );
unset( $method, $args );
return null;
}
}
// Register activation hook.
register_activation_hook( __FILE__, [ 'Disciple_Tools_Plugin_Starter_Template', 'activation' ] );
register_deactivation_hook( __FILE__, [ 'Disciple_Tools_Plugin_Starter_Template', 'deactivation' ] );
if ( ! function_exists( 'disciple_tools_plugin_starter_template_hook_admin_notice' ) ) {
function disciple_tools_plugin_starter_template_hook_admin_notice() {
global $disciple_tools_plugin_starter_template_required_dt_theme_version;
$wp_theme = wp_get_theme();
$current_version = $wp_theme->version;
$message = "'Disciple.Tools - Plugin Starter Template' 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( $disciple_tools_plugin_starter_template_required_dt_theme_version ) );
}
// Check if it's been dismissed...
if ( ! get_option( 'dismissed-disciple-tools-plugin-starter-template', false ) ) { ?>
<div class="notice notice-error notice-disciple-tools-plugin-starter-template is-dismissible" data-notice="disciple-tools-plugin-starter-template">
<p><?php echo esc_html( $message );?></p>
</div>
<script>
jQuery(function($) {
$( document ).on( 'click', '.notice-disciple-tools-plugin-starter-template .notice-dismiss', function () {
$.ajax( ajaxurl, {
type: 'POST',
data: {
action: 'dismissed_notice_handler',
type: 'disciple-tools-plugin-starter-template',
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 );
}
}
}
/**
* Plugin Releases and updates
* @todo Uncomment and change the url if you want to support remote plugin updating with new versions of your plugin
* To remove: delete the section of code below and delete the file called version-control.json in the plugin root
*
* This section runs the remote plugin updating service, so you can issue distributed updates to your plugin
*
* @note See the instructions for version updating to understand the steps involved.
* @link https://github.com/DiscipleTools/disciple-tools-plugin-starter-template/wiki/Configuring-Remote-Updating-System
*
* @todo Enable this section with your own hosted file
* @todo An example of this file can be found in (version-control.json)
* @todo Github is a good option for delivering static json.
*/
/**
* 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() ){
// // Check for plugin updates
// if ( ! class_exists( 'Puc_v4_Factory' ) ) {
// if ( file_exists( get_template_directory() . '/dt-core/libraries/plugin-update-checker/plugin-update-checker.php' )){
// require( get_template_directory() . '/dt-core/libraries/plugin-update-checker/plugin-update-checker.php' );
// }
// }
// if ( class_exists( 'Puc_v4_Factory' ) ){
// Puc_v4_Factory::buildUpdateChecker(
// 'https://raw.githubusercontent.com/DiscipleTools/disciple-tools-plugin-starter-template/master/version-control.json',
// __FILE__,
// 'disciple-tools-plugin-starter-template'
// );
//
// }
// }
//} );