-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfictioneer-base-plugin.php
219 lines (186 loc) · 6.14 KB
/
fictioneer-base-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
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
<?php
/**
* Plugin Name: Fictioneer Base Plugin
* Description: Example plugin for developers.
* Version: 1.0.2
* Requires at least: 6.1.0
* Requires PHP: 7.4.0
* Author: Tetrakern
* Author URI: https://github.com/Tetrakern
* Text Domain: FCNBP
*/
// No direct access!
defined( 'ABSPATH' ) OR exit;
// Version
define( 'FCNBP_VERSION', '1.0.2' );
// Text domain
define( 'FCNBP_TD', 'fcnbp' );
// =======================================================================================
// SETUP
// =======================================================================================
/**
* Makes plugin meta fields protected
*
* This prevents the meta fields used by the plugin to be changed via the
* editor interface. Alternatively, you could start them with an underscore
* (e.g. _fcnbp_some_meta) to make use of the WP default protection.
*
* @since 1.0.0
*
* @param bool $protected Whether the meta key is considered protected.
* @param string $meta_key The meta key to check.
*
* @return bool True if the meta key is protected, false otherwise.
*/
function fcnbp_make_plugin_meta_protected( $protected, $meta_key ) {
if ( strpos( $meta_key, 'fcnbp_' ) === 0 ) {
return true;
}
return $protected;
}
add_filter( 'is_protected_meta', 'fcnbp_make_plugin_meta_protected', 10, 2 );
/**
* Checks for the Fictioneer (parent) theme, deactivates plugin if not found
*
* Any plugin made for the Fictioneer theme should only run with the theme
* active to avoid errors.
*
* @since 1.0.0
* @since 1.0.1 - Ensure deactivate_plugins() is defined.
*/
function fcnbp_check_theme() {
// Setup
$current_theme = wp_get_theme();
// Child or parent theme?
if ( $current_theme->parent() ) {
$theme_name = $current_theme->parent()->get( 'Name' );
} else {
$theme_name = $current_theme->get( 'Name' );
}
// Theme name must be Fictioneer!
if ( $theme_name !== 'Fictioneer' ) {
require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
// Deactivate plugin
deactivate_plugins( plugin_basename( __FILE__ ) );
// Display an admin notice
add_action( 'admin_notices', 'fcnbp_admin_notice_wrong_theme' );
}
}
add_action( 'after_setup_theme', 'fcnbp_check_theme' );
/**
* Show admin notice if plugin has been deactivated due to wrong theme
*
* @since 1.0.0
*/
function fcnbp_admin_notice_wrong_theme() {
// Start HTML ---> ?>
<div class="notice notice-error is-dismissible">
<p><?php
_e( 'The Fictioneer Base Plugin requires the Fictioneer theme or a child theme to be active. The plugin has been deactivated.', FCNBP_TD );
?></p>
</div>
<?php // <--- End HTML
}
/**
* Adds plugin card to theme settings plugin tab
*
* @since 1.0.0
*/
function fcnbp_settings_card() {
// Start HTML ---> ?>
<div class="fictioneer-card fictioneer-card--plugin">
<div class="fictioneer-card__wrapper">
<h3 class="fictioneer-card__header"><?php _e( 'Fictioneer Base Plugin', FCNBP_TD ); ?></h3>
<div class="fictioneer-card__content">
<div class="fictioneer-card__row">
<p><?php
_e( '<strong>This is an example plugin.</strong> It is not meant for production, just as boilerplate to work off. Please replace any function prefixes, constants, names, and the text domain with your own.', FCNBP_TD );
?></p>
</div>
<div class="fictioneer-card__row fictioneer-card__row--meta">
<?php printf( __( 'Version %s', FCNBP_TD ), FCNBP_VERSION ); ?>
|
<?php printf( __( 'By <a href="%s">Tetrakern</a>', FCNBP_TD ), 'https://github.com/Tetrakern' ); ?>
</div>
</div>
</div>
</div>
<?php // <--- End HTML
}
add_action( 'fictioneer_admin_settings_plugins', 'fcnbp_settings_card' );
// =======================================================================================
// SCRIPTS AND STYLES
// =======================================================================================
/**
* Compare installed WordPress version against version string
*
* @since 1.0.2
* @global wpdb $wp_version Current WordPress version string.
*
* @param string $version The version string to test against.
* @param string $operator Optional. How to compare. Default '>='.
*
* @return boolean True or false.
*/
function fcnbp_compare_wp_version( $version, $operator = '>=' ) {
global $wp_version;
return version_compare( $wp_version, $version, $operator );
}
/**
* Enqueues styles and scripts on the frontend
*
* @since 1.0.0
* @since 1.0.2 - Added $strategy and $dependency.
*/
function fcnbp_enqueue_frontend_scripts() {
// Setup
// $strategy = fcnbp_compare_wp_version( '6.3' ) ? array( 'strategy' => 'defer' ) : true; // Defer or load in footer
// $dependency = get_option( 'fictioneer_bundle_scripts' ) ?
// ['fictioneer-complete-scripts'] : ['fictioneer-application-scripts'];
// Styles
// wp_enqueue_style(
// 'fictioneer-base-plugin-styles',
// plugin_dir_url( __FILE__ ) . '/css/fictioneer-base-plugin.css',
// ['fictioneer-application'],
// FCNBP_VERSION
// );
// Scripts
// wp_enqueue_script(
// 'fictioneer-base-plugin-scripts',
// plugin_dir_url( __FILE__ ) . 'js/fictioneer-base-plugin.js',
// [], // Or use $dependency if you need it
// FCNBP_VERSION,
// $strategy
// );
}
add_action( 'wp_enqueue_scripts', 'fcnbp_enqueue_frontend_scripts' );
/**
* Enqueues styles and scripts in the admin
*
* @since 1.0.0
*
* @param string $hook_suffix The current admin page.
*/
function fcnbp_enqueue_admin_scripts( $hook_suffix ) {
// Only on the theme's plugin settings tab
if ( $hook_suffix !== 'fictioneer_page_fictioneer_plugins' ) {
return;
}
// Styles
// wp_enqueue_style(
// 'fictioneer-base-plugin-admin-styles',
// plugin_dir_url( __FILE__ ) . '/css/fictioneer-base-plugin-admin.css',
// ['fictioneer-admin-panel'],
// FCNBP_VERSION
// );
// Scripts
// wp_enqueue_script(
// 'fictioneer-base-plugin-admin-scripts',
// plugin_dir_url( __FILE__ ) . '/js/fictioneer-base-plugin-admin.js',
// ['fictioneer-utility-scripts'],
// FCNBP_VERSION,
// true
// );
}
add_action( 'admin_enqueue_scripts', 'fcnbp_enqueue_admin_scripts' );
?>