-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin-starter.php
520 lines (382 loc) · 11.6 KB
/
plugin-starter.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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
<?php
/**
*
* @wordpress-plugin
* Plugin Name: Plugin Starter
* Description: The Agency WordPress plugin boilerplate
* Author: Agency
* Version: 1.0
* Author URI: http://agency.sc/
*/
/*
* GETTING STARTED WITH THE STARTER
* ---------------------------------------------------------
*
* Every time you use the plugin starter you need to do
* run a few find/replaces on the files.
*
* On this file you need to process the following strings:
*
* 1. Plugin_Starter
* 2. Plugin Starter
* 3. plugin_starter
* 4. plugin-starter
*
* Then rename this file, the template tags file and the title in the plugin options.
*
* FOLDER STRUCTURE
* Should you need to add any vendor libraries (think API wrappers),
* add them to a folder called 'libs'. Initiate the classes in __construct.
*
* CONSISTENT DOCUMENTATION
* All functions you write in this class should be documented in phpDoc,
* including what the function does, what params it takes and what it outputs.
* You can see the full phpDoc syntax here: goo.gl/09S2wc
*
* WRAPPING UP
* Keep code clean. Once you've done these steps, remove these instructions
*
*/
if (!defined('ABSPATH')) {
exit; // Exit if accessed directly
}
/*========== Activation Hook ==========*/
register_activation_hook( __FILE__, array( 'Plugin_Starter', 'install' ) );
/**
* Main Plugin_Starter Class
*
* @class Plugin_Starter
* @version 0.1
*/
class Plugin_Starter {
public $errors = false;
public $notices = false;
public $slug = 'plugin-starter';
function __construct() {
$this->path = plugin_dir_path(__FILE__);
$this->folder = basename($this->path);
$this->dir = plugin_dir_url(__FILE__);
$this->version = '1.0';
$this->errors = false;
$this->notice = false;
// Actions
add_action('init', array($this, 'setup'), 10, 0);
add_action('wp_enqueue_scripts', array($this, 'scripts'));
add_action('wp_loaded', array($this , 'forms'));
add_action('parse_request', array($this , 'custom_url_paths'));
add_action('admin_menu', array($this, 'register_options_page'));
// Shortcodes
// add_shortcode('students', array($this, 'shortcode'));
// Notices (add these when you need to show the notice)
// add_action( 'admin_notices', array($this, 'admin_success'));
// add_action( 'admin_notices', array($this, 'admin_error'));
}
/**
* Install
* ---------------------------------------------
* @return false
* ---------------------------------------------
**/
public static function install() {
/**
*
* Add methods here that should be run when the plugin is activated.
*
**/
}
/**
* Setup
* ---------------------------------------------
* @return false
* ---------------------------------------------
**/
public function setup() {
// register types
$this->register_types();
}
/**
* Register Types
* ---------------------------------------------
* @return false
* ---------------------------------------------
**/
public function register_types() {
/*========== POST TYPE NAME ==========*/
$args = array(
'labels' => self::build_type_labels('Topic', 'Topics'),
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'topics' ),
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'menu_icon' => 'dashicons-welcome-learn-more',
'menu_position' => 40,
'supports' => array( 'title', 'editor', 'page-attributes' )
);
// register_post_type( 'topic', $args );
}
/**
* Build Type Labels
* ---------------------------------------------
* @param $name | String | Singular Name
* @param $plural | String | Plural Name
* @return Array
* ---------------------------------------------
**/
private static function build_type_labels($name, $plural) {
return array(
'name' => $plural,
'singular_name' => $name,
'add_new' => "Add New",
'add_new_item' => "Add New $name",
'edit_item' => "Edit $name",
'new_item' => "New $name",
'all_items' => "All $plural",
'view_item' => "View $name",
'search_items' => "Search $plural",
'not_found' => "No " . strtolower($plural) . " found",
'not_found_in_trash' => "No " . strtolower($plural) . " found in trash",
'parent_item_colon' => '',
'menu_name' => $plural
);
}
/**
* Scripts
* ---------------------------------------------
* @return null
* ---------------------------------------------
**/
public function scripts() {
// wp_enqueue_script('jquery.validate', '//cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.11.1/jquery.validate.min.js', array('jquery'), $this->version, true);
}
/**
* Forms
* ---------------------------------------------
* @return false
* ---------------------------------------------
**/
public function forms() {
if (!isset($_POST['plugin_starter_action'])) return;
switch ($_POST['plugin_starter_action']) {
case 'action':
// $this->action($_POST);
break;
default:
break;
}
}
/**
* Custom URL Paths
* ---------------------------------------------
* @param $wp | Object
* @return false
* ---------------------------------------------
**/
public function custom_url_paths($wp) {
$pagename = (isset($wp->query_vars['pagename'])) ? $wp->query_vars['pagename'] : $wp->request;
switch ($pagename) {
case 'pluginstarter/api':
// $this->api($_GET);
break;
default:
break;
}
}
/**
* Register Options Page
* ---------------------------------------------
* @return false
* ---------------------------------------------
**/
public function register_options_page() {
// main page
add_options_page('Plugin Starter', 'Plugin Starter', 'manage_options', 'plugin_starter_options', array($this, 'include_options'));
add_action('admin_init', array($this, 'plugin_options'));
}
/**
* Include Options Page
* ---------------------------------------------
* @return false
* ---------------------------------------------
**/
public function include_options() { require('templates/options.php'); }
/**
* Plugin Options
* ---------------------------------------------
* @return false
* ---------------------------------------------
**/
public function plugin_options() {
$options = array(
'plugin_starter_test',
'plugin_starter_test2'
);
foreach ($options as $option) {
register_setting('plugin_starter_options', $option);
}
}
/**
* Shortcode Include
*/
public function shortcode() {
$errors = $this->errors;
ob_start();
// include $this->template('template.php');
return ob_get_clean();
}
/**
* Outputs a WordPress error notice
*
* Push your error to $this->errors then show with:
* add_action( 'admin_notices', array($this, 'admin_error'));
*/
public function admin_error() {
if(!$this->errors) return;
foreach($this->errors as $error) :
?>
<div class="error settings-error notice is-dismissible">
<p><strong><?php print $error ?></strong></p>
<button type="button" class="notice-dismiss"><span class="screen-reader-text">Dismiss this notice.</span></button>
</div>
<?php
endforeach;
}
/**
* Outputs a WordPress notice
*
* Push your error to $this->notices then show with:
* add_action( 'admin_notices', array($this, 'admin_success'));
*/
public function admin_success() {
if(!$this->notices) return;
foreach($this->notices as $notice) :
?>
<div class="updated settings-error notice is-dismissible">
<p><strong><?php print $notice ?></strong></p>
<button type="button" class="notice-dismiss"><span class="screen-reader-text">Dismiss this notice.</span></button>
</div>
<?php
endforeach;
}
/**
* Email
* ---------------------------------------------
* @param $to | String | To email address
* @param $subject | String | The email Subject
* @param $message | String | The email body
* @param $replacements | Array | Key=>Value string replacements
* @return false
* ---------------------------------------------
**/
public function email($to, $subject, $message, $replacements = array()) {
//replacements
foreach ($replacements as $variable => $replacement) {
$message = str_replace($variable, $replacement, $message);
$subject = str_replace($variable, $replacement, $subject);
}
//Send from the site email
$headers = array(
'Content-Type: text/html; charset=UTF-8',
'From: ' . get_bloginfo('name') . ' <' . get_bloginfo('admin_email') . '>'
);
//WP mail function
wp_mail( $to, $subject, $message , $headers);
}
/**
* Template
* ---------------------------------------------
* @param $filename | String | name of the template
* @return false
* ---------------------------------------------
**/
public function template($filename) {
// check theme
$theme = get_template_directory() . '/'.$this->slug.'/' . $filename;
if (file_exists($theme)) {
$path = $theme;
} else {
$path = $this->path . 'templates/' . $filename;
}
return $path;
}
/**
* Template Include
* ---------------------------------------------
* @param $template | String | name of the template
* @param $data | Anything | Data to pass to a template
* @param $name | String | Data value name
* @return false
* ---------------------------------------------
**/
public function template_include($template,$data = null,$name = null){
if(isset($name)){ ${$name} = $data; }
$path = $this->template($template);
include($path);
}
/**
* Redirect
* ---------------------------------------------
* @param $path | String/Int | url of post id
* @return false
* ---------------------------------------------
**/
public function redirect($path) {
if(is_numeric($path)){ $path = get_permalink($path); }
wp_safe_redirect( $path );
exit();
}
/**
* Output CSV
* ---------------------------------------------
* @param $array | Array/Object | Data to output
* @param $filename | String | Name of the file to export
* @return false
* ---------------------------------------------
**/
public function output_csv($array, $filename = 'report.csv') {
ob_clean();
ob_start();
$file = fopen('php://output', 'w');
// generate csv lines from the inner arrays
$headings = array();
foreach ($array[0] as $key => $line) {
$headings[] = $key;
}
fputcsv($file, $headings);
foreach($array as $row) {
fputcsv($file, $row);
}
// rewind file
$output = stream_get_contents($file);
fclose($file);
// prep download
header("Content-type: text/x-csv");
header("Content-Transfer-Encoding: binary");
header('Content-Disposition: attachement; filename="' . $filename . '";');
header("Pragma: no-cache");
header("Expires: 0");
echo $output;
exit();
}
/**
* Output JSON
* ---------------------------------------------
* @param $array | Array/Object | Data to output
* @return false
* ---------------------------------------------
**/
public function output_json($array) {
header('Content-type: application/json');
echo json_encode($array);
exit();
}
}
/**
* @var class Plugin_Starter $plugin_starter
*/
require_once('plugin-starter-template-tags.php');
$plugin_starter = new Plugin_Starter();