PluginLoader::init()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 14
nc 1
nop 0
dl 0
loc 25
rs 9.7998
c 0
b 0
f 0
1
<?php
2
namespace NirjharLo\WP_Plugin_Framework;
3
4
use NirjharLo\WP_Plugin_Framework\Src\Install as Install;
5
use NirjharLo\WP_Plugin_Framework\Src\Cpt as Cpt;
6
use NirjharLo\WP_Plugin_Framework\Src\Db as Db;
7
use NirjharLo\WP_Plugin_Framework\Src\Settings as Settings;
8
use NirjharLo\WP_Plugin_Framework\Src\Widget as Widget;
9
use NirjharLo\WP_Plugin_Framework\Src\Metabox as Metabox;
10
use NirjharLo\WP_Plugin_Framework\Src\Shortcode as Shortcode;
11
use NirjharLo\WP_Plugin_Framework\Src\RestApi as RestApi;
12
13
use NirjharLo\WP_Plugin_Framework\Lib\Cron as Cron;
14
use NirjharLo\WP_Plugin_Framework\Lib\Script as Script;
15
16
if ( ! defined( 'ABSPATH' ) ) exit;
17
18
/**
19
 * Main plugin object to define the plugin
20
 * Follow: https://codex.wordpress.org/Plugin_API for details
21
 *
22
 * @author     Nirjhar Lo
23
 * @package    wp-plugin-framework
24
 */
25
if ( ! class_exists( 'PluginLoader' ) ) {
26
27
	final class PluginLoader {
28
29
		/**
30
		 * @var String
31
		 */
32
		protected $version = '1.4.3';
33
34
35
		/**
36
		 * Plugin Instance.
37
		 *
38
		 * @var PLUGIN_BUILD the PLUGIN Instance
0 ignored issues
show
Bug introduced by
The type NirjharLo\WP_Plugin_Framework\PLUGIN_BUILD was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
39
		 */
40
		protected static $_instance;
41
42
43
		/**
44
		 * Text domain to be used throughout the plugin
45
		 *
46
		 * @var String
47
		 */
48
		protected static $text_domain = 'textdomain';
49
50
51
		/**
52
		 * Minimum PHP version allowed for the plugin
53
		 *
54
		 * @var String
55
		 */
56
		protected static $php_ver_allowed = '5.3';
57
58
59
		/**
60
		 * DB tabble used in plugin
61
		 *
62
		 * @var String
63
		 */
64
		protected static $plugin_table = 'plugin_db_table_name';
65
66
67
		/**
68
		 * Plugin listing page links, along with Deactivate
69
		 *
70
		 * @var Array
71
		 */
72
		protected static $plugin_page_links = array(
73
			array(
74
				'slug' => '',
75
				'label' => ''
76
			) );
77
78
79
		/**
80
		 * Main Plugin Instance.
81
		 *
82
		 * @return PLUGIN_BUILD
83
		 */
84
		public static function instance() {
85
86
			if ( is_null( self::$_instance ) ) {
87
				self::$_instance = new self();
0 ignored issues
show
Documentation Bug introduced by
It seems like new self() of type NirjharLo\WP_Plugin_Framework\PluginLoader is incompatible with the declared type NirjharLo\WP_Plugin_Framework\PLUGIN_BUILD of property $_instance.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
88
				self::$_instance->init();
89
			}
90
91
			return self::$_instance;
0 ignored issues
show
Bug Best Practice introduced by
The expression return self::_instance also could return the type NirjharLo\WP_Plugin_Framework\PluginLoader which is incompatible with the documented return type NirjharLo\WP_Plugin_Framework\PLUGIN_BUILD.
Loading history...
92
		}
93
94
95
		/**
96
		 * Install plugin setup
97
		 *
98
		 * @return Void
99
		 */
100
		public function installation() {
101
102
			if (class_exists('NirjharLo\\WP_Plugin_Framework\\Src\\Install')) {
103
104
				$install = new Install();
105
				$install->text_domain = self::$text_domain;
106
				$install->php_ver_allowed = self::$php_ver_allowed;
107
				$install->plugin_page_links = self::$plugin_page_links;
108
				$install->execute();
109
			}
110
111
			//If CPT exists, include taht and flush the rewrite rules
112
			if ( class_exists( 'NirjharLo\\WP_Plugin_Framework\\Src\\Cpt' ) ) new Cpt();
113
			flush_rewrite_rules();
0 ignored issues
show
Bug introduced by
The function flush_rewrite_rules was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

113
			/** @scrutinizer ignore-call */ 
114
   flush_rewrite_rules();
Loading history...
114
		}
115
116
117
		/**
118
		 * Custom corn class, register it while activation
119
		 *
120
		 * @return Void
121
		 */
122
		public function cron_activation() {
123
124
			if ( class_exists( 'NirjharLo\\WP_Plugin_Framework\\Lib\\Cron' ) ) {
125
126
				$cron = new Cron();
127
				$schedule = $cron->schedule_task(
0 ignored issues
show
Unused Code introduced by
The assignment to $schedule is dead and can be removed.
Loading history...
128
							array(
129
							'timestamp' => current_time('timestamp'),
0 ignored issues
show
Bug introduced by
The function current_time was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

129
							'timestamp' => /** @scrutinizer ignore-call */ current_time('timestamp'),
Loading history...
130
							//'schedule' can be 'hourly', 'daily', 'weekly' or anything custom as defined in PLUGIN_CRON
131
							'recurrence' => 'schedule',
132
							// Use custom_corn_hook to hook into any cron process, anywhere in the plugin.
133
							'hook' => 'custom_cron_hook'
134
						) );
135
			}
136
137
		}
138
139
140
		/**
141
		 * Install plugin data
142
		 *
143
		 * @return Void
144
		 */
145
		public function db_install() {
146
147
			if ( class_exists( 'NirjharLo\\WP_Plugin_Framework\\Src\\Db' ) ) {
148
149
				$db = new Db();
150
				$db->table = self::$plugin_table;
151
				$db->sql = "ID mediumint(9) NOT NULL AUTO_INCREMENT,
152
							date date NOT NULL,
153
							UNIQUE KEY ID (ID)";
154
				$db->build();
155
			}
156
157
			if (get_option( '_plugin_db_exist') == '0' ) {
0 ignored issues
show
Bug introduced by
The function get_option was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

157
			if (/** @scrutinizer ignore-call */ get_option( '_plugin_db_exist') == '0' ) {
Loading history...
158
				add_action( 'admin_notices', array( $this, 'db_error_msg' ) );
0 ignored issues
show
Bug introduced by
The function add_action was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

158
				/** @scrutinizer ignore-call */ 
159
    add_action( 'admin_notices', array( $this, 'db_error_msg' ) );
Loading history...
159
			}
160
161
			$options = array(
162
				array( 'option_name', '__value__' ),
163
			);
164
			foreach ( $options as $value ) {
165
				update_option( $value[0], $value[1] );
0 ignored issues
show
Bug introduced by
The function update_option was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

165
				/** @scrutinizer ignore-call */ 
166
    update_option( $value[0], $value[1] );
Loading history...
166
			}
167
		}
168
169
170
		/**
171
		 * Notice of DB
172
		 *
173
		 * @return Html
0 ignored issues
show
Bug introduced by
The type NirjharLo\WP_Plugin_Framework\Html was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
174
		 */
175
		public function db_error_msg() { ?>
176
177
			<div class="notice notice-error is-dismissible">
178
				<p><?php _e( 'Database table Not installed correctly.', 'textdomain' ); ?></p>
0 ignored issues
show
Bug introduced by
The function _e was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

178
				<p><?php /** @scrutinizer ignore-call */ _e( 'Database table Not installed correctly.', 'textdomain' ); ?></p>
Loading history...
179
 			</div>
180
			<?php
181
		}
182
183
184
		/**
185
		 * Uninstall plugin data
186
		 *
187
		 * @return Void
188
		 */
189
		public function db_uninstall() {
190
191
			$table_name = self::$plugin_table;
192
193
			global $wpdb;
194
			$wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}$table_name" );
195
196
			$options = array(
197
				'_plugin_db_exist'
198
			);
199
			foreach ( $options as $value ) {
200
				delete_option( $value );
0 ignored issues
show
Bug introduced by
The function delete_option was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

200
				/** @scrutinizer ignore-call */ 
201
    delete_option( $value );
Loading history...
201
			}
202
		}
203
204
205
		/**
206
		 * CRON callback
207
		 *
208
		 * @return Void
209
		 */
210
		public function do_cron_job_function() {
211
212
			//Do cron function
213
		}
214
215
216
		/**
217
		 * Run CRON action
218
		 *
219
		 * @return Void
220
		 */
221
		public function custom_cron_hook_cb() {
222
223
			add_action( 'custom_cron_hook', array( $this, 'do_cron_job_function' ) );
0 ignored issues
show
Bug introduced by
The function add_action was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

223
			/** @scrutinizer ignore-call */ 
224
   add_action( 'custom_cron_hook', array( $this, 'do_cron_job_function' ) );
Loading history...
224
		}
225
226
227
		/**
228
		 * Uninstall CRON hook
229
		 *
230
		 * @return Void
231
		 */
232
		public function cron_uninstall() {
233
234
			wp_clear_scheduled_hook( 'custom_cron_hook' );
0 ignored issues
show
Bug introduced by
The function wp_clear_scheduled_hook was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

234
			/** @scrutinizer ignore-call */ 
235
   wp_clear_scheduled_hook( 'custom_cron_hook' );
Loading history...
235
		}
236
237
238
		/**
239
		 * Install Custom post types
240
		 *
241
		 * @return Void
242
		 */
243
		public function cpt() {
244
245
			if ( class_exists( 'NirjharLo\\WP_Plugin_Framework\\Src\\Cpt' ) ) new Cpt();
246
		}
247
248
249
		/**
250
		 * Include scripts
251
		 *
252
		 * @return Void
253
		 */
254
		public function scripts() {
255
256
			if ( class_exists( 'NirjharLo\\WP_Plugin_Framework\\Lib\\Script' ) ) new Script();
257
		}
258
259
260
		/**
261
		 * Include settings pages
262
		 *
263
		 * @return Void
264
		 */
265
		public function settings() {
266
267
			if ( class_exists( 'NirjharLo\\WP_Plugin_Framework\\Src\\Settings' ) ) new Settings();
268
		}
269
270
271
		/**
272
		 * Include widget classes
273
		 *
274
		 * @return Void
275
		 */
276
		public function widgets() {
277
278
			if ( class_exists( 'NirjharLo\\WP_Plugin_Framework\\Src\\Widget' ) ) new Widget();
279
		}
280
281
282
		/**
283
		 *Include metabox classes
284
		 *
285
		 * @return Void
286
		 */
287
		public function metabox() {
288
289
			if ( class_exists( 'NirjharLo\\WP_Plugin_Framework\\Src\\Metabox' ) ) new Metabox();
290
		}
291
292
293
		/**
294
		 * Include shortcode classes
295
		 *
296
		 * @return Void
297
		 */
298
		public function shortcode() {
299
300
			if ( class_exists( 'NirjharLo\\WP_Plugin_Framework\\Src\\Shortcode' ) ) new Shortcode();
301
		}
302
303
304
		/**
305
		 * Instantiate REST API
306
		 *
307
		 * @return Void
308
		 */
309
		 public function rest_api() {
310
311
			 if ( class_exists( 'NirjharLo\\WP_Plugin_Framework\\Src\\RestApi' ) ) new RestApi();
312
		 }
313
314
315
		 /**
316
 		  * Instantiate REST API
317
 		  *
318
 		  * @return Void
319
 		  */
320
		 public function prevent_unauthorized_rest_access( $result ) {
321
 		    // If a previous authentication check was applied,
322
 		    // pass that result along without modification.
323
 		    if ( true === $result || is_wp_error( $result ) ) {
0 ignored issues
show
Bug introduced by
The function is_wp_error was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

323
 		    if ( true === $result || /** @scrutinizer ignore-call */ is_wp_error( $result ) ) {
Loading history...
324
 		        return $result;
325
 		    }
326
327
 		    // No authentication has been performed yet.
328
 		    // Return an error if user is not logged in.
329
 		    if ( ! is_user_logged_in() ) {
0 ignored issues
show
Bug introduced by
The function is_user_logged_in was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

329
 		    if ( ! /** @scrutinizer ignore-call */ is_user_logged_in() ) {
Loading history...
330
 		        return new WP_Error(
0 ignored issues
show
Bug Best Practice introduced by
The expression return new NirjharLo\WP_...array('status' => 401)) returns the type NirjharLo\WP_Plugin_Framework\WP_Error which is incompatible with the documented return type void.
Loading history...
Bug introduced by
The type NirjharLo\WP_Plugin_Framework\WP_Error was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
331
 		            'rest_not_logged_in',
332
 		            __( 'You are not currently logged in.' ),
0 ignored issues
show
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

332
 		            /** @scrutinizer ignore-call */ 
333
               __( 'You are not currently logged in.' ),
Loading history...
333
 		            array( 'status' => 401 )
334
 		        );
335
 		    }
336
337
 		    return $result;
338
 		}
339
340
341
		/**
342
		 * Instantiate the plugin
343
		 *
344
		 * @return Void
345
		 */
346
		public function init() {
347
348
			register_activation_hook( PLUGIN_FILE, array( $this, 'db_install' ) );
0 ignored issues
show
Bug introduced by
The function register_activation_hook was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

348
			/** @scrutinizer ignore-call */ 
349
   register_activation_hook( PLUGIN_FILE, array( $this, 'db_install' ) );
Loading history...
349
			register_activation_hook( PLUGIN_FILE, array( $this, 'cron_activation' ) );
350
351
			//remove the DB and CORN upon uninstallation
352
			//using $this won't work here.
353
			register_uninstall_hook( PLUGIN_FILE, array( 'NirjharLo\\WP_Plugin_Framework\\PluginLoader', 'db_uninstall' ) );
0 ignored issues
show
Bug introduced by
The function register_uninstall_hook was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

353
			/** @scrutinizer ignore-call */ 
354
   register_uninstall_hook( PLUGIN_FILE, array( 'NirjharLo\\WP_Plugin_Framework\\PluginLoader', 'db_uninstall' ) );
Loading history...
354
			register_uninstall_hook( PLUGIN_FILE, array( 'NirjharLo\\WP_Plugin_Framework\\PluginLoader', 'cron_uninstall' ) );
355
356
			add_filter( 'rest_authentication_errors', array( $this, 'prevent_unauthorized_rest_access' ) );
0 ignored issues
show
Bug introduced by
The function add_filter was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

356
			/** @scrutinizer ignore-call */ 
357
   add_filter( 'rest_authentication_errors', array( $this, 'prevent_unauthorized_rest_access' ) );
Loading history...
357
358
			add_action( 'init', array( $this, 'installation' ) );
0 ignored issues
show
Bug introduced by
The function add_action was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

358
			/** @scrutinizer ignore-call */ 
359
   add_action( 'init', array( $this, 'installation' ) );
Loading history...
359
			add_action( 'init', array( $this, 'cpt' ) );
360
361
			$this->custom_cron_hook_cb();
362
363
			$this->scripts();
364
			$this->widgets();
365
			$this->metabox();
366
			$this->shortcode();
367
			$this->settings();
368
369
			//Alternative method: add_action( 'rest_api_init', array($this, 'rest_api') );
370
			$this->rest_api();
371
		}
372
	}
373
} ?>
374