| 1 |
<?php |
| 2 |
/* |
| 3 |
Plugin Name: weDocs |
| 4 |
Plugin URI: http://wedevs.com/ |
| 5 |
Description: A documentation plugin for WordPress |
| 6 |
Version: 1.4 |
| 7 |
Author: Tareq Hasan |
| 8 |
Author URI: https://tareq.co/ |
| 9 |
License: GPL2 |
| 10 |
Text Domain: wedocs |
| 11 |
Domain Path: /languages |
| 12 |
*/ |
| 13 |
|
| 14 |
/** |
| 15 |
* Copyright (c) 2018 Tareq Hasan (email: tareq@wedevs.com). All rights reserved. |
| 16 |
* |
| 17 |
* Released under the GPL license |
| 18 |
* http://www.opensource.org/licenses/gpl-license.php |
| 19 |
* |
| 20 |
* This is an add-on for WordPress |
| 21 |
* http://wordpress.org/ |
| 22 |
* |
| 23 |
* ********************************************************************** |
| 24 |
* This program is free software; you can redistribute it and/or modify |
| 25 |
* it under the terms of the GNU General Public License as published by |
| 26 |
* the Free Software Foundation; either version 2 of the License, or |
| 27 |
* (at your option) any later version. |
| 28 |
* |
| 29 |
* This program is distributed in the hope that it will be useful, |
| 30 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 31 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 32 |
* GNU General Public License for more details. |
| 33 |
* |
| 34 |
* You should have received a copy of the GNU General Public License |
| 35 |
* along with this program; if not, write to the Free Software |
| 36 |
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 37 |
* ********************************************************************** |
| 38 |
*/ |
| 39 |
|
| 40 |
// don't call the file directly |
| 41 |
if ( !defined( 'ABSPATH' ) ) exit; |
| 42 |
|
| 43 |
/** |
| 44 |
* WeDocs class |
| 45 |
* |
| 46 |
* @class WeDocs The class that holds the entire WeDocs plugin |
| 47 |
*/ |
| 48 |
class WeDocs { |
| 49 |
|
| 50 |
/** |
| 51 |
* Plugin version |
| 52 |
* |
| 53 |
* @var string |
| 54 |
*/ |
| 55 |
public $version = '1.4'; |
| 56 |
|
| 57 |
/** |
| 58 |
* The plugin url |
| 59 |
* |
| 60 |
* @var string |
| 61 |
*/ |
| 62 |
public $plugin_url; |
| 63 |
|
| 64 |
/** |
| 65 |
* The plugin path |
| 66 |
* |
| 67 |
* @var string |
| 68 |
*/ |
| 69 |
public $plugin_path; |
| 70 |
|
| 71 |
/** |
| 72 |
* The theme directory path |
| 73 |
* |
| 74 |
* @var string |
| 75 |
*/ |
| 76 |
public $theme_dir_path; |
| 77 |
|
| 78 |
/** |
| 79 |
* The post type name |
| 80 |
* |
| 81 |
* @var string |
| 82 |
*/ |
| 83 |
private $post_type = 'docs'; |
| 84 |
|
| 85 |
/** |
| 86 |
* Initializes the WeDocs() class |
| 87 |
* |
| 88 |
* Checks for an existing WeDocs() instance |
| 89 |
* and if it doesn't find one, creates it. |
| 90 |
*/ |
| 91 |
public static function init() { |
| 92 |
static $instance = false; |
| 93 |
|
| 94 |
if ( ! $instance ) { |
| 95 |
$instance = new WeDocs(); |
| 96 |
|
| 97 |
$instance->plugin_init(); |
| 98 |
} |
| 99 |
|
| 100 |
return $instance; |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Initialize the plugin |
| 105 |
* |
| 106 |
* @return void |
| 107 |
*/ |
| 108 |
function plugin_init() { |
| 109 |
$this->theme_dir_path = apply_filters( 'wedocs_theme_dir_path', 'wedocs/' ); |
| 110 |
|
| 111 |
$this->file_includes(); |
| 112 |
$this->init_classes(); |
| 113 |
|
| 114 |
// Localize our plugin |
| 115 |
add_action( 'init', array( $this, 'localization_setup' ) ); |
| 116 |
|
| 117 |
// custom post types and taxonomies |
| 118 |
add_action( 'init', array( $this, 'register_post_type' ) ); |
| 119 |
add_action( 'init', array( $this, 'register_taxonomy' ) ); |
| 120 |
|
| 121 |
// filter the search result |
| 122 |
add_action( 'pre_get_posts', array( $this, 'docs_search_filter' ) ); |
| 123 |
|
| 124 |
// registeer our widget |
| 125 |
add_action( 'widgets_init', array( $this, 'register_widget' ) ); |
| 126 |
|
| 127 |
// override the theme template |
| 128 |
add_filter( 'template_include', array( $this, 'template_loader' ), 20 ); |
| 129 |
|
| 130 |
// Loads frontend scripts and styles |
| 131 |
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); |
| 132 |
|
| 133 |
register_activation_hook( __FILE__, array( $this, 'activate' ) ); |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* The plugin activation function |
| 138 |
* |
| 139 |
* @since 1.3 |
| 140 |
* |
| 141 |
* @return void |
| 142 |
*/ |
| 143 |
public function activate() { |
| 144 |
|
| 145 |
$this->maybe_create_docs_page(); |
| 146 |
|
| 147 |
// rewrite rules problem, register and flush |
| 148 |
$this->register_post_type(); |
| 149 |
$this->register_taxonomy(); |
| 150 |
|
| 151 |
flush_rewrite_rules(); |
| 152 |
|
| 153 |
update_option( 'wedocs_installed', time() ); |
| 154 |
update_option( 'wedocs_version', $this->version ); |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* Load the required files |
| 159 |
* |
| 160 |
* @return void |
| 161 |
*/ |
| 162 |
function file_includes() { |
| 163 |
include_once dirname( __FILE__ ) . '/includes/functions.php'; |
| 164 |
include_once dirname( __FILE__ ) . '/includes/class-walker-docs.php'; |
| 165 |
include_once dirname( __FILE__ ) . '/includes/class-search-widget.php'; |
| 166 |
include_once dirname( __FILE__ ) . '/includes/class-theme-support.php'; |
| 167 |
|
| 168 |
if ( is_admin() ) { |
| 169 |
include_once dirname( __FILE__ ) . '/includes/admin/class-admin.php'; |
| 170 |
} else { |
| 171 |
include_once dirname( __FILE__ ) . '/includes/class-shortcode.php'; |
| 172 |
} |
| 173 |
|
| 174 |
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) { |
| 175 |
include_once dirname( __FILE__ ) . '/includes/class-ajax.php'; |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
/** |
| 180 |
* Initialize the classes |
| 181 |
* |
| 182 |
* @since 1.4 |
| 183 |
* |
| 184 |
* @return void |
| 185 |
*/ |
| 186 |
public function init_classes() { |
| 187 |
|
| 188 |
new WeDocs_Theme_Support(); |
| 189 |
|
| 190 |
if ( is_admin() ) { |
| 191 |
new WeDocs_Admin(); |
| 192 |
} else { |
| 193 |
new WeDocs_Shortcode_Handler(); |
| 194 |
} |
| 195 |
|
| 196 |
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) { |
| 197 |
new WeDocs_Ajax(); |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
/** |
| 202 |
* Initialize plugin for localization |
| 203 |
* |
| 204 |
* @uses load_plugin_textdomain() |
| 205 |
*/ |
| 206 |
public function localization_setup() { |
| 207 |
load_plugin_textdomain( 'wedocs', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' ); |
| 208 |
} |
| 209 |
|
| 210 |
/** |
| 211 |
* Enqueue admin scripts |
| 212 |
* |
| 213 |
* Allows plugin assets to be loaded. |
| 214 |
* |
| 215 |
* @uses wp_enqueue_script() |
| 216 |
* @uses wp_localize_script() |
| 217 |
* @uses wp_enqueue_style |
| 218 |
*/ |
| 219 |
public function enqueue_scripts() { |
| 220 |
|
| 221 |
/** |
| 222 |
* All styles goes here |
| 223 |
*/ |
| 224 |
wp_enqueue_style( 'wedocs-styles', plugins_url( 'assets/css/frontend.css', __FILE__ ), array(), $this->version ); |
| 225 |
|
| 226 |
/** |
| 227 |
* All scripts goes here |
| 228 |
*/ |
| 229 |
wp_enqueue_script( 'wedocs-anchorjs', plugins_url( 'assets/js/anchor.min.js', __FILE__ ), array( 'jquery' ), $this->version, true ); |
| 230 |
wp_enqueue_script( 'wedocs-scripts', plugins_url( 'assets/js/frontend.js', __FILE__ ), array( 'jquery', 'wedocs-anchorjs' ), $this->version, true ); |
| 231 |
wp_localize_script( 'wedocs-scripts', 'weDocs_Vars', array( |
| 232 |
'ajaxurl' => admin_url( 'admin-ajax.php' ), |
| 233 |
'nonce' => wp_create_nonce( 'wedocs-ajax' ), |
| 234 |
'style' => plugins_url( 'assets/css/print.css', __FILE__ ), |
| 235 |
'powered' => sprintf( '© %s, %d. %s<br>%s', get_bloginfo( 'name' ), date( 'Y' ), __( 'Powered by weDocs', 'wedocs' ), home_url() ) |
| 236 |
) ); |
| 237 |
} |
| 238 |
|
| 239 |
/** |
| 240 |
* Register the post type |
| 241 |
* |
| 242 |
* @return void |
| 243 |
*/ |
| 244 |
function register_post_type() { |
| 245 |
|
| 246 |
$labels = array( |
| 247 |
'name' => _x( 'Docs', 'Post Type General Name', 'wedocs' ), |
| 248 |
'singular_name' => _x( 'Doc', 'Post Type Singular Name', 'wedocs' ), |
| 249 |
'menu_name' => __( 'Documentation', 'wedocs' ), |
| 250 |
'parent_item_colon' => __( 'Parent Doc', 'wedocs' ), |
| 251 |
'all_items' => __( 'All Documentations', 'wedocs' ), |
| 252 |
'view_item' => __( 'View Documentation', 'wedocs' ), |
| 253 |
'add_new_item' => __( 'Add Documentation', 'wedocs' ), |
| 254 |
'add_new' => __( 'Add New', 'wedocs' ), |
| 255 |
'edit_item' => __( 'Edit Documentation', 'wedocs' ), |
| 256 |
'update_item' => __( 'Update Documentation', 'wedocs' ), |
| 257 |
'search_items' => __( 'Search Documentation', 'wedocs' ), |
| 258 |
'not_found' => __( 'Not documentation found', 'wedocs' ), |
| 259 |
'not_found_in_trash' => __( 'Not found in Trash', 'wedocs' ), |
| 260 |
); |
| 261 |
$rewrite = array( |
| 262 |
'slug' => 'docs', |
| 263 |
'with_front' => true, |
| 264 |
'pages' => true, |
| 265 |
'feeds' => true, |
| 266 |
); |
| 267 |
$args = array( |
| 268 |
'labels' => $labels, |
| 269 |
'supports' => array( 'title', 'editor', 'thumbnail', 'revisions', 'page-attributes' ), |
| 270 |
'hierarchical' => true, |
| 271 |
'public' => true, |
| 272 |
'show_ui' => true, |
| 273 |
'show_in_menu' => false, |
| 274 |
'show_in_nav_menus' => true, |
| 275 |
'show_in_admin_bar' => true, |
| 276 |
'menu_position' => 5, |
| 277 |
'menu_icon' => 'dashicons-portfolio', |
| 278 |
'can_export' => true, |
| 279 |
'has_archive' => false, |
| 280 |
'exclude_from_search' => false, |
| 281 |
'publicly_queryable' => true, |
| 282 |
'show_in_rest' => true, |
| 283 |
'rewrite' => $rewrite, |
| 284 |
'capability_type' => 'post', |
| 285 |
'taxonomies' => array( 'doc_tag' ) |
| 286 |
); |
| 287 |
|
| 288 |
register_post_type( $this->post_type, apply_filters( 'wedocs_post_type', $args ) ); |
| 289 |
} |
| 290 |
|
| 291 |
/** |
| 292 |
* Register doc tags taxonomy |
| 293 |
* |
| 294 |
* @return void |
| 295 |
*/ |
| 296 |
function register_taxonomy() { |
| 297 |
|
| 298 |
$labels = array( |
| 299 |
'name' => _x( 'Tags', 'Taxonomy General Name', 'wedocs' ), |
| 300 |
'singular_name' => _x( 'Tag', 'Taxonomy Singular Name', 'wedocs' ), |
| 301 |
'menu_name' => __( 'Tags', 'wedocs' ), |
| 302 |
'all_items' => __( 'All Tags', 'wedocs' ), |
| 303 |
'parent_item' => __( 'Parent Tag', 'wedocs' ), |
| 304 |
'parent_item_colon' => __( 'Parent Tag:', 'wedocs' ), |
| 305 |
'new_item_name' => __( 'New Tag Tag', 'wedocs' ), |
| 306 |
'add_new_item' => __( 'Add New Item', 'wedocs' ), |
| 307 |
'edit_item' => __( 'Edit Tag', 'wedocs' ), |
| 308 |
'update_item' => __( 'Update Tag', 'wedocs' ), |
| 309 |
'view_item' => __( 'View Tag', 'wedocs' ), |
| 310 |
'separate_items_with_commas' => __( 'Separate items with commas', 'wedocs' ), |
| 311 |
'add_or_remove_items' => __( 'Add or remove items', 'wedocs' ), |
| 312 |
'choose_from_most_used' => __( 'Choose from the most used', 'wedocs' ), |
| 313 |
'popular_items' => __( 'Popular Tags', 'wedocs' ), |
| 314 |
'search_items' => __( 'Search Tags', 'wedocs' ), |
| 315 |
'not_found' => __( 'Not Found', 'wedocs' ), |
| 316 |
'no_terms' => __( 'No items', 'wedocs' ), |
| 317 |
'items_list' => __( 'Tags list', 'wedocs' ), |
| 318 |
'items_list_navigation' => __( 'Tags list navigation', 'wedocs' ), |
| 319 |
); |
| 320 |
|
| 321 |
$rewrite = array( |
| 322 |
'slug' => 'doc-tag', |
| 323 |
'with_front' => true, |
| 324 |
'hierarchical' => false, |
| 325 |
); |
| 326 |
|
| 327 |
$args = array( |
| 328 |
'labels' => $labels, |
| 329 |
'hierarchical' => false, |
| 330 |
'public' => true, |
| 331 |
'show_ui' => true, |
| 332 |
'show_admin_column' => true, |
| 333 |
'show_in_nav_menus' => true, |
| 334 |
'show_tagcloud' => true, |
| 335 |
'show_in_rest' => true, |
| 336 |
'rewrite' => $rewrite |
| 337 |
); |
| 338 |
|
| 339 |
register_taxonomy( 'doc_tag', array( 'docs' ), $args ); |
| 340 |
|
| 341 |
} |
| 342 |
|
| 343 |
/** |
| 344 |
* Register the search widget |
| 345 |
* |
| 346 |
* @return void |
| 347 |
*/ |
| 348 |
public function register_widget() { |
| 349 |
register_widget( 'WeDocs_Search_Widget' ); |
| 350 |
} |
| 351 |
|
| 352 |
/** |
| 353 |
* Get the plugin url. |
| 354 |
* |
| 355 |
* @return string |
| 356 |
*/ |
| 357 |
public function plugin_url() { |
| 358 |
if ( $this->plugin_url ) { |
| 359 |
return $this->plugin_url; |
| 360 |
} |
| 361 |
|
| 362 |
return $this->plugin_url = untrailingslashit( plugins_url( '/', __FILE__ ) ); |
| 363 |
} |
| 364 |
|
| 365 |
|
| 366 |
/** |
| 367 |
* Get the plugin path. |
| 368 |
* |
| 369 |
* @return string |
| 370 |
*/ |
| 371 |
public function plugin_path() { |
| 372 |
if ( $this->plugin_path ) return $this->plugin_path; |
| 373 |
|
| 374 |
return $this->plugin_path = untrailingslashit( plugin_dir_path( __FILE__ ) ); |
| 375 |
} |
| 376 |
|
| 377 |
/** |
| 378 |
* Get the template path. |
| 379 |
* |
| 380 |
* @return string |
| 381 |
*/ |
| 382 |
public function template_path() { |
| 383 |
return $this->plugin_path() . '/templates/'; |
| 384 |
} |
| 385 |
|
| 386 |
/** |
| 387 |
* If the theme doesn't have any single doc handler, load that from |
| 388 |
* the plugin |
| 389 |
* |
| 390 |
* @param string $template |
| 391 |
* |
| 392 |
* @return string |
| 393 |
*/ |
| 394 |
function template_loader( $template ) { |
| 395 |
$find = array( $this->post_type . '.php' ); |
| 396 |
$file = ''; |
| 397 |
|
| 398 |
if ( is_single() && get_post_type() == $this->post_type ) { |
| 399 |
$file = 'single-' . $this->post_type . '.php'; |
| 400 |
$find[] = $file; |
| 401 |
$find[] = $this->theme_dir_path. $file; |
| 402 |
} |
| 403 |
|
| 404 |
if ( $file ) { |
| 405 |
|
| 406 |
$template = locate_template( $find ); |
| 407 |
|
| 408 |
if ( ! $template ) { |
| 409 |
$template = $this->template_path() . $file; |
| 410 |
} |
| 411 |
} |
| 412 |
|
| 413 |
return $template; |
| 414 |
} |
| 415 |
|
| 416 |
/** |
| 417 |
* Handle the search filtering in search page |
| 418 |
* |
| 419 |
* @param \WP_Query $query |
| 420 |
* |
| 421 |
* @return \WP_Query |
| 422 |
*/ |
| 423 |
function docs_search_filter( $query ) { |
| 424 |
|
| 425 |
if ( ! is_admin() && is_search() && $query->is_main_query() ) { |
| 426 |
$param = isset( $_GET['search_in_doc'] ) ? sanitize_text_field( $_GET['search_in_doc'] ) : false; |
| 427 |
|
| 428 |
if ( $param ) { |
| 429 |
|
| 430 |
if ( $param != 'all' ) { |
| 431 |
$parent_doc_id = intval( $param ); |
| 432 |
$post__in = array( $parent_doc_id => $parent_doc_id ); |
| 433 |
$children_docs = wedocs_get_posts_children( $parent_doc_id, 'docs' ); |
| 434 |
|
| 435 |
if ( $children_docs ) { |
| 436 |
$post__in = array_merge( $post__in, wp_list_pluck( $children_docs, 'ID' ) ); |
| 437 |
} |
| 438 |
|
| 439 |
$query->set( 'post__in', $post__in ); |
| 440 |
} |
| 441 |
} |
| 442 |
} |
| 443 |
|
| 444 |
return $query; |
| 445 |
} |
| 446 |
|
| 447 |
/** |
| 448 |
* Maybe create docs page if not found |
| 449 |
* |
| 450 |
* @since 1.3 |
| 451 |
* |
| 452 |
* @return void |
| 453 |
*/ |
| 454 |
public function maybe_create_docs_page() { |
| 455 |
$version = get_option( 'wedocs_version' ); |
| 456 |
|
| 457 |
// seems like it's already installed |
| 458 |
if ( $version ) { |
| 459 |
return; |
| 460 |
} |
| 461 |
|
| 462 |
// skip if there's a page already with [wedocs] shortcode |
| 463 |
$pages_query = new WP_Query( array( |
| 464 |
'post_type' => 'page', |
| 465 |
'posts_per_page' => -1, |
| 466 |
's' => '[wedocs' |
| 467 |
) ); |
| 468 |
|
| 469 |
if ( $pages_query->found_posts ) { |
| 470 |
return; |
| 471 |
} |
| 472 |
|
| 473 |
$docs_page = wp_insert_post( array( |
| 474 |
'post_type' => 'page', |
| 475 |
'post_title' => 'Documentation', |
| 476 |
'post_author' => get_current_user_id(), |
| 477 |
'post_content' => '[wedocs]', |
| 478 |
'post_status' => 'publish', |
| 479 |
'comment_status' => 'closed', |
| 480 |
'ping_status' => 'closed', |
| 481 |
'post_name' => 'docs', |
| 482 |
) ); |
| 483 |
|
| 484 |
if ( ! is_wp_error( $docs_page ) ) { |
| 485 |
$settings = get_option( 'wedocs_settings', array() ); |
| 486 |
$settings['docs_home'] = $docs_page; |
| 487 |
|
| 488 |
update_option( 'wedocs_settings', $settings ); |
| 489 |
} |
| 490 |
} |
| 491 |
|
| 492 |
} // WeDocs |
| 493 |
|
| 494 |
/** |
| 495 |
* Initialize the plugin |
| 496 |
* |
| 497 |
* @return \WeDocs |
| 498 |
*/ |
| 499 |
function wedocs() { |
| 500 |
return WeDocs::init(); |
| 501 |
} |
| 502 |
|
| 503 |
// kick it off |
| 504 |
wedocs(); |
| 505 |
|