forked from mpeshev/DX-Plugin-Base
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdx-plugin-base.php
407 lines (355 loc) · 12.7 KB
/
dx-plugin-base.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
<?php
/**
* Plugin Name: DX Plugin Base
* Plugin URI: http://example.org/
* Author: nofearinc
* Author URI: http://devwp.eu/
* Version: 1.2
* Text Domain: dx-sample-plugin
* License: GPL2
Copyright 2011 mpeshev (email : mpeshev AT devrix DOT com)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* Get some constants ready for paths when your plugin grows
*
*/
define( 'DXP_VERSION', '1.2' );
define( 'DXP_PATH', dirname( __FILE__ ) );
define( 'DXP_PATH_INCLUDES', dirname( __FILE__ ) . '/inc' );
define( 'DXP_FOLDER', basename( DXP_PATH ) );
define( 'DXP_URL', plugins_url() . '/' . DXP_FOLDER );
define( 'DXP_URL_INCLUDES', DXP_URL . '/inc' );
/**
*
* The plugin base class - the root of all WP goods!
*
* @author nofearinc
*
*/
class DX_Plugin_Base {
/**
*
* Assign everything as a call from within the constructor
*/
function __construct() {
// add script and style calls the WP way
// it's a bit confusing as styles are called with a scripts hook
// @blamenacin - http://make.wordpress.org/core/2011/12/12/use-wp_enqueue_scripts-not-wp_print_styles-to-enqueue-scripts-and-styles-for-the-frontend/
add_action( 'wp_enqueue_scripts', array( $this, 'dx_add_JS' ) );
add_action( 'wp_enqueue_scripts', array( $this, 'dx_add_CSS' ) );
// add scripts and styles only available in admin
add_action( 'admin_enqueue_scripts', array( $this, 'dx_add_admin_JS' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'dx_add_admin_CSS' ) );
// register admin pages for the plugin
add_action( 'admin_menu', array( $this, 'dx_admin_pages_callback' ) );
// register meta boxes for Pages (could be replicated for posts and custom post types)
add_action( 'add_meta_boxes', array( $this, 'dx_meta_boxes_callback' ) );
// Register custom post types and taxonomies
add_action( 'init', array( $this, 'dx_custom_post_types_callback' ), 5 );
add_action( 'init', array( $this, 'dx_custom_taxonomies_callback' ), 6 );
// Register activation and deactivation hooks
register_activation_hook( __FILE__, 'dx_on_activate_callback' );
register_deactivation_hook( __FILE__, 'dx_on_deactivate_callback' );
// Translation-ready
add_action( 'plugins_loaded', array( $this, 'dx_add_textdomain' ) );
// Add earlier execution as it needs to occur before admin page display
add_action( 'admin_init', array( $this, 'dx_register_settings' ), 5 );
// Add a sample shortcode
add_action( 'init', array( $this, 'dx_sample_shortcode' ) );
// Add a sample widget
add_action( 'widgets_init', array( $this, 'dx_sample_widget' ) );
/*
* TODO:
* AJAX calls
* HTTP request
* template_redirect
*/
// Add actions for storing value and fetching URL
// use the wp_ajax_nopriv_ hook for non-logged users (handle guest actions)
add_action( 'wp_ajax_store_ajax_value', array( $this, 'store_ajax_value' ) );
add_action( 'wp_ajax_fetch_ajax_url_http', array( $this, 'fetch_ajax_url_http' ) );
// TODO: add some filters so that this may not be edited at all but hooked instead
}
/**
*
* Adding JavaScript scripts
*
* Loading existing scripts from wp-includes or adding custom ones
*
*/
function dx_add_JS() {
wp_enqueue_script( 'jquery' );
// load custom JSes and put them in footer
wp_register_script( 'samplescript', plugins_url( '/js/samplescript.js' , __FILE__ ), array('jquery'), '1.0', true );
wp_enqueue_script( 'samplescript' );
}
/**
*
* Adding JavaScript scripts for the admin pages only
*
* Loading existing scripts from wp-includes or adding custom ones
*
*/
function dx_add_admin_JS() {
wp_enqueue_script( 'jquery' );
wp_register_script( 'samplescript-admin', plugins_url( '/js/samplescript-admin.js' , __FILE__ ), array('jquery'), '1.0', true );
wp_enqueue_script( 'samplescript-admin' );
}
/**
*
* Add CSS styles
*
*/
function dx_add_CSS() {
wp_register_style( 'samplestyle', plugins_url( '/css/samplestyle.css', __FILE__ ), array(), '1.0', 'screen' );
wp_enqueue_style( 'samplestyle' );
}
/**
*
* Add admin CSS styles - available only on admin
*
*/
function dx_add_admin_CSS() {
wp_register_style( 'samplestyle-admin', plugins_url( '/css/samplestyle-admin.css', __FILE__ ), array(), '1.0', 'screen' );
wp_enqueue_style( 'samplestyle-admin' );
}
/**
*
* Callback for registering pages
*
* This demo registers a custom page for the plugin and a subpage
*
*/
function dx_admin_pages_callback() {
add_menu_page(__( "Plugin Base Admin", 'dxbase' ), __( "Plugin Base Admin", 'dxbase' ), 'edit_themes', 'dx-plugin-base', array( $this, 'dx_plugin_base' ) );
add_submenu_page( 'dx-plugin-base', __( "Base Subpage", 'dxbase' ), __( "Base Subpage", 'dxbase' ), 'edit_themes', 'dx-base-subpage', array( $this, 'dx_plugin_subpage' ) );
add_submenu_page( 'dx-plugin-base', __( "Remote Subpage", 'dxbase' ), __( "Remote Subpage", 'dxbase' ), 'edit_themes', 'dx-remote-subpage', array( $this, 'dx_plugin_side_access_page' ) );
}
/**
*
* The content of the base page
*
*/
function dx_plugin_base() {
include_once( DXP_PATH_INCLUDES . '/base-page-template.php' );
}
function dx_plugin_side_access_page() {
include_once( DXP_PATH_INCLUDES . '/remote-page-template.php' );
}
/**
*
* The content of the subpage
*
* Use some default UI from WordPress guidelines echoed here (the sample above is with a template)
*
* @see http://www.onextrapixel.com/2009/07/01/how-to-design-and-style-your-wordpress-plugin-admin-panel/
*
*/
function dx_plugin_subpage() {
echo '<div class="wrap">';
_e( "<h2>DX Plugin Subpage</h2> ", 'dxbase' );
_e( "I'm a subpage and I know it!", 'dxbase' );
echo '</div>';
}
/**
*
* Adding right and bottom meta boxes to Pages
*
*/
function dx_meta_boxes_callback() {
// register side box
add_meta_box(
'dx_side_meta_box',
__( "DX Side Box", 'dxbase' ),
array( $this, 'dx_side_meta_box' ),
'page', // leave empty quotes as '' if you want it on all custom post add/edit screens
'side',
'high'
);
// register bottom box
add_meta_box(
'dx_bottom_meta_box',
__( "DX Bottom Box", 'dxbase' ),
array( $this, 'dx_bottom_meta_box' ),
'' // leave empty quotes as '' if you want it on all custom post add/edit screens or add a post type slug
);
}
/**
*
* Init right side meta box here
* @param post $post the post object of the given page
* @param metabox $metabox metabox data
*/
function dx_side_meta_box($post, $metabox) {
_e("<p>Side meta content here</p>", 'dxbase');
}
/**
*
* Init bottom meta box here
* @param post $post the post object of the given page
* @param metabox $metabox metabox data
*/
function dx_bottom_meta_box($post, $metabox) {
_e( "<p>Bottom meta content here</p>", 'dxbase' );
}
/**
* Register custom post types
*
*/
function dx_custom_post_types_callback() {
register_post_type( 'pluginbase', array(
'labels' => array(
'name' => __("Base Items", 'dxbase'),
'singular_name' => __("Base Item", 'dxbase'),
'add_new' => _x("Add New", 'pluginbase', 'dxbase' ),
'add_new_item' => __("Add New Base Item", 'dxbase' ),
'edit_item' => __("Edit Base Item", 'dxbase' ),
'new_item' => __("New Base Item", 'dxbase' ),
'view_item' => __("View Base Item", 'dxbase' ),
'search_items' => __("Search Base Items", 'dxbase' ),
'not_found' => __("No base items found", 'dxbase' ),
'not_found_in_trash' => __("No base items found in Trash", 'dxbase' ),
),
'description' => __("Base Items for the demo", 'dxbase'),
'public' => true,
'publicly_queryable' => true,
'query_var' => true,
'rewrite' => true,
'exclude_from_search' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 40, // probably have to change, many plugins use this
'supports' => array(
'title',
'editor',
'thumbnail',
'custom-fields',
'page-attributes',
),
'taxonomies' => array( 'post_tag' )
));
}
/**
* Register custom taxonomies
*
*/
function dx_custom_taxonomies_callback() {
register_taxonomy( 'pluginbase_taxonomy', 'pluginbase', array(
'hierarchical' => true,
'labels' => array(
'name' => _x( "Base Item Taxonomies", 'taxonomy general name', 'dxbase' ),
'singular_name' => _x( "Base Item Taxonomy", 'taxonomy singular name', 'dxbase' ),
'search_items' => __( "Search Taxonomies", 'dxbase' ),
'popular_items' => __( "Popular Taxonomies", 'dxbase' ),
'all_items' => __( "All Taxonomies", 'dxbase' ),
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => __( "Edit Base Item Taxonomy", 'dxbase' ),
'update_item' => __( "Update Base Item Taxonomy", 'dxbase' ),
'add_new_item' => __( "Add New Base Item Taxonomy", 'dxbase' ),
'new_item_name' => __( "New Base Item Taxonomy Name", 'dxbase' ),
'separate_items_with_commas' => __( "Separate Base Item taxonomies with commas", 'dxbase' ),
'add_or_remove_items' => __( "Add or remove Base Item taxonomy", 'dxbase' ),
'choose_from_most_used' => __( "Choose from the most used Base Item taxonomies", 'dxbase' )
),
'show_ui' => true,
'query_var' => true,
'rewrite' => true,
));
register_taxonomy_for_object_type( 'pluginbase_taxonomy', 'pluginbase' );
}
/**
* Initialize the Settings class
*
* Register a settings section with a field for a secure WordPress admin option creation.
*
*/
function dx_register_settings() {
require_once( DXP_PATH . '/dx-plugin-settings.class.php' );
new DX_Plugin_Settings();
}
/**
* Register a sample shortcode to be used
*
* First parameter is the shortcode name, would be used like: [dxsampcode]
*
*/
function dx_sample_shortcode() {
add_shortcode( 'dxsampcode', array( $this, 'dx_sample_shortcode_body' ) );
}
/**
* Returns the content of the sample shortcode, like [dxsamplcode]
* @param array $attr arguments passed to array, like [dxsamcode attr1="one" attr2="two"]
* @param string $content optional, could be used for a content to be wrapped, such as [dxsamcode]somecontnet[/dxsamcode]
*/
function dx_sample_shortcode_body( $attr, $content = null ) {
/*
* Manage the attributes and the content as per your request and return the result
*/
return __( 'Sample Output', 'dxbase');
}
/**
* Hook for including a sample widget with options
*/
function dx_sample_widget() {
include_once DXP_PATH_INCLUDES . '/dx-sample-widget.class.php';
}
/**
* Add textdomain for plugin
*/
function dx_add_textdomain() {
load_plugin_textdomain( 'dxbase', false, dirname( plugin_basename( __FILE__ ) ) . '/lang/' );
}
/**
* Callback for saving a simple AJAX option with no page reload
*/
function store_ajax_value() {
if( isset( $_POST['data'] ) && isset( $_POST['data']['dx_option_from_ajax'] ) ) {
update_option( 'dx_option_from_ajax' , $_POST['data']['dx_option_from_ajax'] );
}
die();
}
/**
* Callback for getting a URL and fetching it's content in the admin page
*/
function fetch_ajax_url_http() {
if( isset( $_POST['data'] ) && isset( $_POST['data']['dx_url_for_ajax'] ) ) {
$ajax_url = $_POST['data']['dx_url_for_ajax'];
$response = wp_remote_get( $ajax_url );
if( isset( $response['body'] ) ) {
if( preg_match( '/<title>(.*)<\/title>/', $response['body'], $matches ) ) {
echo json_encode( $matches[1] );
die();
}
}
}
echo json_encode( __( 'No title found or site was not fetched properly', 'dxbase' ) );
die();
}
}
/**
* Register activation hook
*
*/
function dx_on_activate_callback() {
// do something on activation
}
/**
* Register deactivation hook
*
*/
function dx_on_deactivate_callback() {
// do something when deactivated
}
// Initialize everything
$dx_plugin_base = new DX_Plugin_Base();