-
Notifications
You must be signed in to change notification settings - Fork 0
/
mai-testimonials.php
390 lines (353 loc) · 13.1 KB
/
mai-testimonials.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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
<?php
/**
* Plugin Name: Mai Testimonials
* Plugin URI: https://bizbudding.com/products/mai-testimonials/
* Description: Manage and display testimonials on your website.
* Version: 2.7.2
*
* Author: BizBudding
* Author URI: https://bizbudding.com
*/
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) exit;
// Must be at the top of the file.
use YahnisElsts\PluginUpdateChecker\v5\PucFactory;
/**
* Main Mai_Testimonials_Plugin Class.
*
* @since 0.1.0
*/
final class Mai_Testimonials_Plugin {
/**
* @var Mai_Testimonials_Plugin The one true Mai_Testimonials_Plugin
* @since 0.1.0
*/
private static $instance;
/**
* Main Mai_Testimonials_Plugin Instance.
*
* Insures that only one instance of Mai_Testimonials_Plugin exists in memory at any one
* time. Also prevents needing to define globals all over the place.
*
* @since 0.1.0
* @static var array $instance
* @uses Mai_Testimonials_Plugin::setup_constants() Setup the constants needed.
* @uses Mai_Testimonials_Plugin::setup() Activate, deactivate, etc.
* @see Mai_Testimonials_Plugin()
* @return object | Mai_Testimonials_Plugin The one true Mai_Testimonials_Plugin
*/
static function instance() {
if ( ! isset( self::$instance ) ) {
// Setup the init
self::$instance = new Mai_Testimonials_Plugin;
// Methods
self::$instance->setup_constants();
self::$instance->includes();
self::$instance->hooks();
}
return self::$instance;
}
/**
* Throw error on object clone.
*
* The whole idea of the singleton design pattern is that there is a single
* object therefore, we don't want the object to be cloned.
*
* @since 0.1.0
* @access protected
* @return void
*/
function __clone() {
// Cloning instances of the class is forbidden.
_doing_it_wrong( __FUNCTION__, __( 'Cheatin’ huh?', 'mai-aec' ), '1.0' );
}
/**
* Disable unserializing of the class.
*
* @since 0.1.0
* @access protected
* @return void
*/
function __wakeup() {
// Unserializing instances of the class is forbidden.
_doing_it_wrong( __FUNCTION__, __( 'Cheatin’ huh?', 'mai-aec' ), '1.0' );
}
/**
* Setup plugin constants.
*
* @access private
* @since 0.1.0
* @return void
*/
private function setup_constants() {
// Plugin version.
if ( ! defined( 'MAI_TESTIMONIALS_VERSION' ) ) {
define( 'MAI_TESTIMONIALS_VERSION', '2.7.2' );
}
// Plugin Folder Path.
if ( ! defined( 'MAI_TESTIMONIALS_PLUGIN_DIR' ) ) {
define( 'MAI_TESTIMONIALS_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
}
// Plugin Classes Path.
if ( ! defined( 'MAI_TESTIMONIALS_CLASSES_DIR' ) ) {
define( 'MAI_TESTIMONIALS_CLASSES_DIR', MAI_TESTIMONIALS_PLUGIN_DIR . 'classes/' );
}
// Plugin Includes Path.
if ( ! defined( 'MAI_TESTIMONIALS_INCLUDES_DIR' ) ) {
define( 'MAI_TESTIMONIALS_INCLUDES_DIR', MAI_TESTIMONIALS_PLUGIN_DIR . 'includes/' );
}
// Plugin Folder URL.
if ( ! defined( 'MAI_TESTIMONIALS_PLUGIN_URL' ) ) {
define( 'MAI_TESTIMONIALS_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
}
// Plugin Root File.
if ( ! defined( 'MAI_TESTIMONIALS_PLUGIN_FILE' ) ) {
define( 'MAI_TESTIMONIALS_PLUGIN_FILE', __FILE__ );
}
// Plugin Base Name
if ( ! defined( 'MAI_TESTIMONIALS_BASENAME' ) ) {
define( 'MAI_TESTIMONIALS_BASENAME', dirname( plugin_basename( __FILE__ ) ) );
}
}
/**
* Include required files.
*
* @access private
* @since 0.5.3
* @return void
*/
private function includes() {
// Include vendor libraries.
require_once __DIR__ . '/vendor/autoload.php';
// Includes.
foreach ( glob( MAI_TESTIMONIALS_INCLUDES_DIR . '*.php' ) as $file ) { include $file; }
// Classes.
foreach ( glob( MAI_TESTIMONIALS_CLASSES_DIR . '*.php' ) as $file ) { include $file; }
}
/**
* Run hooks.
*
* @since 0.1.0
*
* @return void
*/
function hooks() {
register_activation_hook( __FILE__, [ $this, 'activate' ] );
register_deactivation_hook( __FILE__, 'flush_rewrite_rules' );
add_action( 'plugins_loaded', [ $this, 'updater' ], 12 );
add_action( 'init', [ $this, 'register_content_types' ] );
add_action( 'after_setup_theme', [ $this, 'run' ] ); // Plugins loaded is too early to check for engine version.
}
/**
* Setup the updater.
*
* composer require yahnis-elsts/plugin-update-checker
*
* @since 0.1.0
*
* @uses https://github.com/YahnisElsts/plugin-update-checker/
*
* @return void
*/
function updater() {
// Bail if plugin updater is not loaded.
if ( ! class_exists( 'YahnisElsts\PluginUpdateChecker\v5\PucFactory' ) ) {
return;
}
// Setup the updater.
$updater = PucFactory::buildUpdateChecker( 'https://github.com/maithemewp/mai-testimonials/', __FILE__, 'mai-testimonials' );
// Maybe set github api token.
if ( defined( 'MAI_GITHUB_API_TOKEN' ) ) {
$updater->setAuthentication( MAI_GITHUB_API_TOKEN );
}
// Add icons for Dashboard > Updates screen.
if ( function_exists( 'mai_get_updater_icons' ) && $icons = mai_get_updater_icons() ) {
$updater->addResultFilter(
function ( $info ) use ( $icons ) {
$info->icons = $icons;
return $info;
}
);
}
}
/**
* Flushes permalinks upon activation.
*
* @since 0.1.0
*
* @return void
*/
function activate() {
$this->register_content_types();
flush_rewrite_rules();
}
/**
* Registers post types and taxonomies.
*
* @since 0.1.0
*
* @return void
*/
function register_content_types() {
/***********************
* Custom Post Types *
***********************/
register_post_type( 'testimonial',
apply_filters( 'mai_testimonial_args',
[
'exclude_from_search' => false, // False here so it can show up in SearchWP/FacetWP options.
'has_archive' => false,
'hierarchical' => false,
'labels' => [
'name' => _x( 'Testimonials', 'testimonial general name' , 'mai-testimonials' ),
'singular_name' => _x( 'Testimonial' , 'testimonial singular name' , 'mai-testimonials' ),
'menu_name' => _x( 'Testimonials', 'testimonial admin menu' , 'mai-testimonials' ),
'name_admin_bar' => _x( 'Testimonial' , 'testimonial add new on admin bar', 'mai-testimonials' ),
'add_new' => _x( 'Add New' , 'Testimonial' , 'mai-testimonials' ),
'add_new_item' => __( 'Add New Testimonial' , 'mai-testimonials' ),
'new_item' => __( 'New Testimonial' , 'mai-testimonials' ),
'edit_item' => __( 'Edit Testimonial' , 'mai-testimonials' ),
'view_item' => __( 'View Testimonial' , 'mai-testimonials' ),
'all_items' => __( 'All Testimonials' , 'mai-testimonials' ),
'search_items' => __( 'Search Testimonials' , 'mai-testimonials' ),
'parent_item_colon' => __( 'Parent Testimonials:' , 'mai-testimonials' ),
'not_found' => __( 'No Testimonials found.' , 'mai-testimonials' ),
'not_found_in_trash' => __( 'No Testimonials found in Trash.' , 'mai-testimonials' ),
'featured_image' => __( 'Testimonial Image' , 'mai-testimonials' ),
'set_featured_image' => __( 'Set testimonial image' , 'mai-testimonials' ),
'remove_featured_image' => __( 'Remove testimonial image' , 'mai-testimonials' ),
'use_featured_image' => __( 'Use testimonial image' , 'mai-testimonials' ),
],
'menu_icon' => 'dashicons-format-quote',
'public' => true, // Needs to be true for SearchWP/FacetWP.
'publicly_queryable' => false, // Hides from the front end. No singular view.
'show_in_menu' => true,
'show_in_nav_menus' => false,
'show_in_rest' => true,
'show_ui' => true,
'rewrite' => false,
'supports' => [ 'title', 'editor', 'thumbnail', 'page-attributes', 'genesis-cpt-archives-settings' ], // 'page-attributes' only here for sort order, especially with Simple Page Ordering plugin.
]
)
);
/***********************
* Custom Taxonomies *
***********************/
register_taxonomy( 'testimonial_cat', 'testimonial',
apply_filters( 'mai_testimonial_cat_args',
[
'exclude_from_search' => true,
'has_archive' => false,
'hierarchical' => true,
'labels' =>
[
'name' => _x( 'Testimonial Categories', 'taxonomy general name' , 'mai-testimonials' ),
'singular_name' => _x( 'Testimonial Category' , 'taxonomy singular name' , 'mai-testimonials' ),
'search_items' => __( 'Search Testimonial Categories' , 'mai-testimonials' ),
'popular_items' => __( 'Popular Testimonial Categories' , 'mai-testimonials' ),
'all_items' => __( 'All Categories' , 'mai-testimonials' ),
'edit_item' => __( 'Edit Testimonial Category' , 'mai-testimonials' ),
'update_item' => __( 'Update Testimonial Category' , 'mai-testimonials' ),
'add_new_item' => __( 'Add New Testimonial Category' , 'mai-testimonials' ),
'new_item_name' => __( 'New Testimonial Category Name' , 'mai-testimonials' ),
'separate_items_with_commas' => __( 'Separate Testimonial Categories with commas' , 'mai-testimonials' ),
'add_or_remove_items' => __( 'Add or remove Testimonial Categories' , 'mai-testimonials' ),
'choose_from_most_used' => __( 'Choose from the most used Testimonial Categories', 'mai-testimonials' ),
'not_found' => __( 'No Testimonial Categories found.' , 'mai-testimonials' ),
'menu_name' => __( 'Testimonial Categories' , 'mai-testimonials' ),
'parent_item' => null,
'parent_item_colon' => null,
],
'public' => false,
'rewrite' => false,
'show_admin_column' => true,
'show_in_menu' => true,
'show_in_nav_menus' => false,
'show_in_rest' => true,
'show_tagcloud' => false,
'show_ui' => true,
]
)
);
}
/**
* Runs plugin if Mai Engine is active.
*
* @since 1.0.0
*
* @return void
*/
function run() {
// Bail if Genesis is not running.
if ( ! function_exists( 'genesis' ) ) {
add_action( 'admin_notices', [ $this, 'admin_notice_genesis' ] );
return;
}
// Bail if no engine is anywhere.
if ( ! ( class_exists( 'Mai_Theme_Engine' ) || class_exists( 'Mai_Engine' ) ) ) {
add_action( 'admin_notices', [ $this, 'admin_notice_engine' ] );
return;
}
// Content type hooks and filters.
$types = new Mai_Testimonials_Content_Types;
// Bail if v2 engine is too old.
if ( class_exists( 'Mai_Engine' ) && ( ! function_exists( 'mai_get_version' ) || ! version_compare( mai_get_version(), '2.21', '>' ) ) ) {
add_action( 'admin_notices', [ $this, 'admin_notice_version' ] );
return;
}
// Mai Theme v1.
if ( class_exists( 'Mai_Theme_Engine' ) ) {
$grid = new Mai_Testimonials_Grid_Shortcode;
}
// Mai Theme v2.
elseif ( class_exists( 'Mai_Engine' ) ) {
$grid = new Mai_Testimonials_Grid_Block;
$block = new Mai_Testimonials_Block;
}
}
/**
* Displays admin notice if no genesis.
*
* @since 2.4.0
*
* @return void
*/
function admin_notice_genesis() {
printf( '<div class="notice notice-warning"><p>%s</p></div>', __( 'Mai Theme and Mai Testimonials requires Genesis as the parent theme.', 'mai-testimonials' ) );
}
/**
* Displays admin notice if no engine.
*
* @since 0.1.0
*
* @return void
*/
function admin_notice_engine() {
printf( '<div class="notice notice-warning"><p>%s</p></div>', __( 'Mai Testimonials requires Mai Theme and it\'s Engine plugin in order to run.', 'mai-testimonials' ) );
}
/**
* Displays admin notice if engine version is too old.
*
* @since 2.4.0
*
* @return void
*/
function admin_notice_version() {
printf( '<div class="notice notice-warning"><p>%s</p></div>', __( 'Mai Testimonials requires Mai Engine plugin version 2.21.0 or later. Please install/upgrade now to use the Mai Testimonials.', 'mai-testimonials' ) );
}
}
/**
* The main function for that returns Mai_Testimonials_Plugin
*
* The main function responsible for returning the one true Mai_Testimonials_Plugin
* Instance to functions everywhere.
*
* @since 0.1.0
*
* @return object|Mai_Testimonials_Plugin The one true Mai_Testimonials_Plugin Instance.
*/
function mai_testimonials() {
return Mai_Testimonials_Plugin::instance();
}
// Get Mai_Testimonials_Plugin Running.
mai_testimonials();