forked from weDevsOfficial/wedocs-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwedocs.php
More file actions
427 lines (369 loc) · 11.7 KB
/
wedocs.php
File metadata and controls
427 lines (369 loc) · 11.7 KB
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
<?php
/*
Plugin Name: weDocs
Plugin URI: https://wedocs.co/
Description: A documentation plugin for WordPress
Version: 2.2.1
Author: weDevs
Author URI: https://wedocs.co/?utm_source=wporg&utm_medium=banner&utm_campaign=author-uri
License: GPL2
Text Domain: wedocs
Domain Path: /languages
*/
/*
* Copyright (c) 2021 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
use Appsero\Client;
use WeDevs\WeDocs\Capability;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
require_once __DIR__ . '/vendor/autoload.php';
require_once plugin_dir_path(__FILE__) . 'assets/build/blocks/helpers/block-styles.php';
// require_once __DIR__ . '/includes/captcha-config.php'; // todo add later
/**
* WeDocs class.
*
* @class WeDocs The class that holds the entire WeDocs plugin
*/
final class WeDocs {
/**
* Plugin version.
*
* @var string
*/
const VERSION = '2.2.1';
/**
* The plugin url.
*
* @var string
*/
public $plugin_url;
/**
* The plugin path.
*
* @var string
*/
public $plugin_path;
/**
* The theme directory path.
*
* @var string
*/
public $theme_dir_path;
/**
* The post type name.
*
* @var string
*/
private $post_type = 'docs';
/**
* Holds various class instances
*
* @var array
*/
private $container = [];
/**
* Class constructor.
*/
private function __construct() {
$this->define_constants();
$this->init_actions();
register_activation_hook( __FILE__, [ $this, 'activate' ] );
add_action( 'after_setup_theme', [ $this, 'init_classes' ] );
$this->init_action_scheduler();
}
/**
* 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();
}
return $instance;
}
/**
* Define the required plugin constants
*
* @return void
*/
public function define_constants() {
define( 'WEDOCS_VERSION', self::VERSION );
define( 'WEDOCS_FILE', __FILE__ );
define( 'WEDOCS_PATH', __DIR__ );
define( 'WEDOCS_URL', plugins_url( '', WEDOCS_FILE ) );
define( 'WEDOCS_ASSETS', WEDOCS_URL . '/assets' );
define( 'WEDOCS_PROMO_KEY', 'wedocs_promo_notices' );
define( 'WEDOCS_PROMO_URL', 'https://raw.githubusercontent.com/weDevsOfficial/wedocs-util/refs/heads/master/promotion.json' );
define( 'WEDOCS_LOGO_URL', WEDOCS_URL . '/assets/img/wedocs-icon-128x128.png' );
}
/**
* Magic getter to bypass referencing plugin.
*
* @param $prop
*
* @return mixed
*/
public function __get( $prop ) {
if ( array_key_exists( $prop, $this->container ) ) {
return $this->container[ $prop ];
}
return $this->{$prop};
}
/**
* Initialize the plugin.
*
* @return void
*/
public function init_actions() {
$this->theme_dir_path = apply_filters( 'wedocs_theme_dir_path', 'wedocs/' );
// Localize our plugin
add_action( 'init', [ $this, 'localization_setup' ] );
add_action( 'init', [ $this, 'register_blocks' ] );
// add_action('init', [$this, 'register_blocks']);
add_filter('block_categories_all', [$this, 'register_block_category']);
add_filter( 'render_block', [ $this, 'gate_pro_blocks' ], 10, 2 );
// registeer our widget
add_action( 'widgets_init', [ $this, 'register_widget' ] );
}
public function register_blocks() {
// Enqueue admin script early to make weDocsAdminScriptVars available for blocks
wp_enqueue_script( 'wedocs-admin-script' );
// Modern WordPress block registration using block.json files
$block_directories = [
// Free blocks
'assets/build/blocks/DocsGrid',
'assets/build/blocks/Breadcrumb',
'assets/build/blocks/HelpfulFeedback',
'assets/build/blocks/QuickSearch',
'assets/build/blocks/PrintButton',
'assets/build/blocks/DocNavigation',
'assets/build/blocks/Sidebar',
'assets/build/blocks/Search',
'assets/build/blocks/HelpfulModal',
'assets/build/blocks/LastUpdated',
// Pro blocks — always registered so they appear in the inserter;
// JS handles the pro gate and shows the PRO badge when pro is inactive.
'assets/build/blocks/TableOfContents',
// 'assets/build/blocks/AdvanceContributors',
'assets/build/blocks/Contributors',
'assets/build/blocks/SocialShare',
'assets/build/blocks/AISummary',
'assets/build/blocks/DocActions',
'assets/build/blocks/ReadingProgress',
'assets/build/blocks/FontSizeSwitcher',
];
foreach ( $block_directories as $block_dir ) {
$block_path = plugin_dir_path(__FILE__) . $block_dir;
if ( file_exists( $block_path . '/block.json' ) ) {
// Register block using block.json (modern approach - render callback is handled by block.json)
register_block_type( $block_path );
}
}
}
/**
* Prevent pro-only blocks from rendering on the frontend when weDocs Pro is inactive.
* Covers blocks that use a save() function (static HTML stored in DB).
*
* @param string $block_content Rendered block HTML.
* @param array $block Block data.
* @return string
*/
public function gate_pro_blocks( $block_content, $block ) {
static $pro_active = null;
if ( $pro_active === null ) {
$pro_active = function_exists( 'wedocs_is_pro_active' ) && wedocs_is_pro_active();
}
if ( $pro_active ) {
return $block_content;
}
$pro_blocks = [
'wedocs/reading-progress',
'wedocs/ai-summary',
'wedocs/doc-actions',
'wedocs/font-size-switcher',
];
if ( isset( $block['blockName'] ) && in_array( $block['blockName'], $pro_blocks, true ) ) {
return '';
}
return $block_content;
}
/**
* Register the weDocs block category.
*
* @param array $categories Existing block categories.
* @return array Modified block categories.
*/
public function register_block_category($categories) {
// Check if weDocs category already exists
foreach ($categories as $category) {
if ($category['slug'] === 'wedocs') {
return $categories;
}
}
// Add weDocs category at the beginning
return array_merge(
[
[
'slug' => 'wedocs',
'title' => __('weDocs', 'wedocs'),
'icon' => null
]
],
$categories
);
}
/**
* The plugin activation function.
*
* @since 1.3
*
* @return void
*/
public function activate() {
$installer = new WeDevs\WeDocs\Installer();
$installer->run();
// Set the redirect option to true when the plugin is activated.
update_option( 'wedocs_activation_redirect', true );
}
/**
* Initialize the classes.
*
* @since 1.4
*
* @return void
*/
public function init_classes() {
$this->container['post_type'] = new WeDevs\WeDocs\Post_Types();
if ( is_admin() ) {
$this->container['admin'] = new WeDevs\WeDocs\Admin();
} else {
$this->container['frontend'] = new WeDevs\WeDocs\Frontend();
}
if ( wp_doing_ajax() ) {
$this->container['ajax'] = new WeDevs\WeDocs\Ajax();
}
$this->container['api'] = new WeDevs\WeDocs\API();
$this->container['assets'] = new WeDevs\WeDocs\Assets();
$this->container['migrate'] = new WeDevs\WeDocs\Admin\Migrate();
$this->container['upgrader'] = new WeDevs\WeDocs\Upgrader\Upgrader();
$this->container['capability'] = new Capability();
$this->container['templates'] = new WeDevs\WeDocs\Templates\TemplateManager();
// Initialize Elementor integration if Elementor is active
if ( did_action( 'elementor/loaded' ) ) {
$this->container['elementor'] = new WeDevs\WeDocs\Elementor();
}
}
/**
* Initialize plugin for localization.
*
* @uses load_plugin_textdomain()
*/
public function localization_setup() {
load_plugin_textdomain( 'wedocs', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
}
/**
* Register the search widget.
*
* @return void
*/
public function register_widget() {
register_widget( 'WeDevs\WeDocs\Widget' );
}
/**
* Get the plugin url.
*
* @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.
*
* @return string
*/
public function plugin_path() {
if ( $this->plugin_path ) {
return $this->plugin_path;
}
return $this->plugin_path = untrailingslashit( plugin_dir_path( __FILE__ ) );
}
/**
* Initialize action scheduler.
*
* @since 2.0.0
*
* @return void
*/
public function init_action_scheduler() {
require_once( __DIR__ . '/vendor/woocommerce/action-scheduler/action-scheduler.php' );
}
/**
* Get the template path.
*
* @return string
*/
public function template_path() {
return $this->plugin_path() . '/templates/';
}
/**
* Get the theme directory path.
*
* @return string
*/
public function theme_dir_path() {
return $this->theme_dir_path;
}
} // WeDocs
/**
* Initialize the plugin.
*
* @return \WeDocs
*/
function wedocs() {
return WeDocs::init();
}
// kick it off
wedocs();
/**
* Dequeue wedocs-pro-frontend-css on non-docs post types
*/
function dequeue_wedocs_pro_frontend_css() {
// Check if we're not on a docs post type
if ( ! is_singular( 'docs' ) && ! is_post_type_archive( 'docs' ) && ! is_tax( array( 'doc_category', 'doc_tag' ) ) ) {
wp_dequeue_style( 'wedocs-pro-frontend-css' );
wp_deregister_style( 'wedocs-pro-frontend-css' );
}
}
add_action( 'wp_enqueue_scripts', 'dequeue_wedocs_pro_frontend_css', 20 );