forked from weDevsOfficial/wedocs-plugin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwedocs.php
448 lines (382 loc) · 14.2 KB
/
wedocs.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
<?php
/*
Plugin Name: DO NOT UPDATE - weDocs
Plugin URI: http://wedevs.com/
Description: A documentation plugin for WordPress
Version: 0.1
Author: Tareq Hasan
Author URI: https://tareq.co/
License: GPL2
Text Domain: wedocs
Domain Path: /languages
*/
/**
* Copyright (c) 2016 Tareq Hasan (email: tareq@wedevs.com). All rights reserved.
*
* Released under the GPL license
* http://www.opensource.org/licenses/gpl-license.php
*
* This is an add-on for WordPress
* http://wordpress.org/
*
* **********************************************************************
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* 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
* **********************************************************************
*/
// don't call the file directly
if ( !defined( 'ABSPATH' ) ) exit;
/**
* WeDocs class
*
* @class WeDocs The class that holds the entire WeDocs plugin
*/
class WeDocs {
public $plugin_url;
public $plugin_path;
public $theme_dir_path;
private $post_type = 'docs';
/**
* Initializes the WeDocs() class
*
* Checks for an existing WeDocs() instance
* and if it doesn't find one, creates it.
*/
public static function init() {
static $instance = false;
if ( ! $instance ) {
$instance = new WeDocs();
$instance->plugin_init();
}
return $instance;
}
function plugin_init() {
$this->theme_dir_path = apply_filters( 'wedocs_theme_dir_path', 'wedocs/' );
$this->file_includes();
// Localize our plugin
add_action( 'init', array( $this, 'localization_setup' ) );
// custom post types and taxonomies
add_action( 'init', array( $this, 'register_post_type' ) );
add_action( 'init', array( $this, 'register_taxonomy' ) );
add_action( 'init', array( $this, 'register_faq_categories_taxonomy' ) );
// filter the search result
add_action( 'pre_get_posts', array( $this, 'docs_search_filter' ) );
// registeer our widget
add_action( 'widgets_init', array( $this, 'register_widget' ) );
// override the theme template
add_filter( 'template_include', array( $this, 'template_loader' ), 20 );
// Loads frontend scripts and styles
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
}
/**
* Load the required files
*
* @return void
*/
function file_includes() {
include_once dirname( __FILE__ ) . '/includes/functions.php';
include_once dirname( __FILE__ ) . '/includes/class-walker-docs.php';
include_once dirname( __FILE__ ) . '/includes/class-search-widget.php';
if ( is_admin() ) {
include_once dirname( __FILE__ ) . '/includes/admin/class-admin.php';
} else {
include_once dirname( __FILE__ ) . '/includes/class-shortcode.php';
}
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
include_once dirname( __FILE__ ) . '/includes/class-ajax.php';
}
}
/**
* Initialize plugin for localization
*
* @uses load_plugin_textdomain()
*/
public function localization_setup() {
load_plugin_textdomain( 'wedocs', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
}
/**
* Enqueue admin scripts
*
* Allows plugin assets to be loaded.
*
* @uses wp_enqueue_script()
* @uses wp_localize_script()
* @uses wp_enqueue_style
*/
public function enqueue_scripts() {
/**
* All styles goes here
*/
wp_enqueue_style( 'wedocs-styles', plugins_url( 'assets/css/frontend.css', __FILE__ ), false, date( 'Ymd' ) );
/**
* All scripts goes here
*/
wp_enqueue_script( 'wedocs-scripts', plugins_url( 'assets/js/frontend.js', __FILE__ ), array( 'jquery' ), false, true );
wp_localize_script( 'wedocs-scripts', 'weDocs', array(
'ajaxurl' => admin_url( 'admin-ajax.php' ),
'nonce' => wp_create_nonce( 'wedocs-ajax' )
) );
}
/**
* Register the post type
*
* @return void
*/
function register_post_type() {
$labels = array(
'name' => _x( 'Docs', 'Post Type General Name', 'wedocs' ),
'singular_name' => _x( 'Doc', 'Post Type Singular Name', 'wedocs' ),
'menu_name' => __( 'Documentation', 'wedocs' ),
'parent_item_colon' => __( 'Parent Doc', 'wedocs' ),
'all_items' => __( 'All Documentations', 'wedocs' ),
'view_item' => __( 'View Documentation', 'wedocs' ),
'add_new_item' => __( 'Add Documentation', 'wedocs' ),
'add_new' => __( 'Add New', 'wedocs' ),
'edit_item' => __( 'Edit Documentation', 'wedocs' ),
'update_item' => __( 'Update Documentation', 'wedocs' ),
'search_items' => __( 'Search Documentation', 'wedocs' ),
'not_found' => __( 'Not documentation found', 'wedocs' ),
'not_found_in_trash' => __( 'Not found in Trash', 'wedocs' ),
);
$rewrite = array(
'slug' => 'docs',
'with_front' => true,
'pages' => true,
'feeds' => true,
);
$args = array(
'labels' => $labels,
'supports' => array( 'title', 'editor', 'thumbnail', 'revisions', ),
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_in_menu' => false,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 5,
'menu_icon' => 'dashicons-portfolio',
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'rewrite' => $rewrite,
'capability_type' => 'page',
);
register_post_type( $this->post_type, $args );
}
/**
* Register doc tags taxonomy
*
* @return void
*/
function register_taxonomy() {
$labels = array(
'name' => _x( 'Tags', 'Taxonomy General Name', 'wedocs' ),
'singular_name' => _x( 'Tag', 'Taxonomy Singular Name', 'wedocs' ),
'menu_name' => __( 'Tags', 'wedocs' ),
'all_items' => __( 'All Tags', 'wedocs' ),
'parent_item' => __( 'Parent Tag', 'wedocs' ),
'parent_item_colon' => __( 'Parent Tag:', 'wedocs' ),
'new_item_name' => __( 'New Tag Tag', 'wedocs' ),
'add_new_item' => __( 'Add New Item', 'wedocs' ),
'edit_item' => __( 'Edit Tag', 'wedocs' ),
'update_item' => __( 'Update Tag', 'wedocs' ),
'view_item' => __( 'View Tag', 'wedocs' ),
'separate_items_with_commas' => __( 'Separate items with commas', 'wedocs' ),
'add_or_remove_items' => __( 'Add or remove items', 'wedocs' ),
'choose_from_most_used' => __( 'Choose from the most used', 'wedocs' ),
'popular_items' => __( 'Popular Tags', 'wedocs' ),
'search_items' => __( 'Search Tags', 'wedocs' ),
'not_found' => __( 'Not Found', 'wedocs' ),
'no_terms' => __( 'No items', 'wedocs' ),
'items_list' => __( 'Tags list', 'wedocs' ),
'items_list_navigation' => __( 'Tags list navigation', 'wedocs' ),
);
$rewrite = array(
'slug' => 'doc-tag',
'with_front' => true,
'hierarchical' => false,
);
$args = array(
'labels' => $labels,
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => true,
'rewrite' => $rewrite
);
register_taxonomy( 'doc_tag', array( 'docs' ), $args );
}
/**
* Register FAQ categories taxonomy.
*
* @author Vova Feldman
*
* @return void
*/
function register_faq_categories_taxonomy(){
$labels = array(
'name' => _x( 'Categories', 'Taxonomy General Name', 'freemius' ),
'singular_name' => _x( 'Category', 'Taxonomy Singular Name', 'freemius' ),
'menu_name' => __( 'Categories', 'freemius' ),
'all_items' => __( 'All Categories', 'freemius' ),
'parent_item' => __( 'Parent Category', 'freemius' ),
'parent_item_colon' => __( 'Parent Category:', 'freemius' ),
'new_item_name' => __( 'New Category Name', 'freemius' ),
'add_new_item' => __( 'Add New Category', 'freemius' ),
'edit_item' => __( 'Edit Category', 'freemius' ),
'update_item' => __( 'Update Category', 'freemius' ),
'separate_items_with_commas' => __( 'Separate categories with commas', 'freemius' ),
'search_items' => __( 'Search Categories', 'freemius' ),
'add_or_remove_items' => __( 'Add or remove categories', 'freemius' ),
'choose_from_most_used' => __( 'Choose from the most used categories', 'freemius' ),
'not_found' => __( 'Not Found', 'freemius' ),
);
$rewrite = array(
'slug' => 'help/faq',
'with_front' => true,
'hierarchical' => false,
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => false,
'rewrite' => $rewrite
);
register_taxonomy( 'doc_category', array( 'docs' ), $args );
}
/**
* Register the search widget
*
* @return void
*/
public function register_widget() {
register_widget( 'WeDocs_Search_Widget' );
}
/**
* Get the plugin url.
*
* @access public
* @return string
*/
public function plugin_url() {
if ( $this->plugin_url ) {
return $this->plugin_url;
}
return $this->plugin_url = untrailingslashit( plugins_url( '/', __FILE__ ) );
}
/**
* Get the plugin path.
*
* @access public
* @return string
*/
public function plugin_path() {
if ( $this->plugin_path ) return $this->plugin_path;
return $this->plugin_path = untrailingslashit( plugin_dir_path( __FILE__ ) );
}
/**
* Get the template path.
*
* @access public
* @return string
*/
public function template_path() {
return $this->plugin_path() . '/templates/';
}
/**
* If the theme doesn't have any single doc handler, load that from
* the plugin
*
* @param string $template
*
* @return string
*/
function template_loader( $template ) {
$find = array( $this->post_type . '.php' );
$file = '';
if ( is_single() && get_post_type() == $this->post_type ) {
$file = 'single-' . $this->post_type . '.php';
$find[] = $file;
$find[] = $this->theme_dir_path. $file;
}
if ( $file ) {
$template = locate_template( $find );
if ( ! $template ) {
$template = $this->template_path() . $file;
}
}
return $template;
}
/**
* Handle the search filtering in search page
*
* @param \WP_Query $query
*
* @return \WP_Query
*/
function docs_search_filter( $query ) {
if ( ! is_admin() && is_search() && $query->is_main_query() ) {
$param = isset( $_GET['search_in_doc'] ) ? sanitize_text_field( $_GET['search_in_doc'] ) : false;
if ( $param ) {
if ( $param != 'all' ) {
$parent_doc_id = intval( $param );
$post__in = array( $parent_doc_id => $parent_doc_id );
$children_docs = wedocs_get_posts_children( $parent_doc_id, 'docs' );
if ( $children_docs ) {
$post__in = array_merge( $post__in, wp_list_pluck( $children_docs, 'ID' ) );
}
$query->set( 'post__in', $post__in );
}
}
}
return $query;
}
/**
* Flush rewrites.
*/
public static function activation() {
wedocs()->register_post_type();
wedocs()->register_taxonomy();
wedocs()->register_faq_categories_taxonomy();
$category_all = get_term_by( 'slug', 'all', 'doc_category' );
// Create All category if not yet exist.
if ( false === $category_all ) {
wp_insert_term(
__( 'All', 'wedocs' ),
'doc_category',
array(
'slug' => 'all',
)
);
}
}
} // WeDocs
/**
* Initialize the plugin
*
* @return \WeDocs
*/
function wedocs() {
return WeDocs::init();
}
// kick it off
wedocs();
register_activation_hook( __FILE__, array( 'WeDocs', 'activation' ) );