Passed
Push — master ( 93534b...206518 )
by Nirjhar
08:40
created

PLUGIN_BUILD::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 15
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 25
rs 9.7666
1
<?php
2
if ( ! defined( 'ABSPATH' ) ) exit;
3
4
/**
5
 * Main plugin object to define the plugin
6
 * Follow: https://codex.wordpress.org/Plugin_API for details
7
 *
8
 * @author     Nirjhar Lo
9
 * @package    wp-plugin-framework
10
 */
11
if ( ! class_exists( 'PLUGIN_BUILD' ) ) {
12
13
	final class PLUGIN_BUILD {
14
15
		/**
16
		 * @var String
17
		 */
18
		protected $version = '1.3';
19
20
21
		/**
22
		 * Plugin Instance.
23
		 *
24
		 * @var PLUGIN_BUILD the PLUGIN Instance
25
		 */
26
		protected static $_instance;
27
28
29
		/**
30
		 * Text domain to be used throughout the plugin
31
		 *
32
		 * @var String
33
		 */
34
		protected static $text_domain = 'textdomain';
35
36
37
		/**
38
		 * Minimum PHP version allowed for the plugin
39
		 *
40
		 * @var String
41
		 */
42
		protected static $php_ver_allowed = '5.3';
43
44
45
		/**
46
		 * DB tabble used in plugin
47
		 *
48
		 * @var String
49
		 */
50
		protected static $plugin_table = 'plugin_db_table_name';
51
52
53
		/**
54
		 * Plugin listing page links, along with Deactivate
55
		 *
56
		 * @var Array
57
		 */
58
		protected static $plugin_page_links = array(
59
			array(
60
				'slug' => '',
61
				'label' => ''
62
			) );
63
64
65
		/**
66
		 * Main Plugin Instance.
67
		 *
68
		 * @return PLUGIN_BUILD
69
		 */
70
		public static function instance() {
71
72
			if ( is_null( self::$_instance ) ) {
73
				self::$_instance = new self();
74
				self::$_instance->init();
75
			}
76
77
			return self::$_instance;
78
		}
79
80
81
		/**
82
		 * Install plugin setup
83
		 *
84
		 * @return Void
85
		 */
86
		public function installation() {
87
88
			if (class_exists('PLUGIN_INSTALL')) {
89
90
				$install = new PLUGIN_INSTALL();
91
				$install->text_domain = self::$text_domain;
92
				$install->php_ver_allowed = self::$php_ver_allowed;
93
				$install->plugin_page_links = self::$plugin_page_links;
94
				$install->execute();
95
			}
96
97
			//If CPT exists, include taht and flush the rewrite rules
98
			if ( class_exists( 'PLUGIN_CPT' ) ) new PLUGIN_CPT();
99
			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

99
			/** @scrutinizer ignore-call */ 
100
   flush_rewrite_rules();
Loading history...
100
		}
101
102
103
		/**
104
		 * Custom corn class, register it while activation
105
		 *
106
		 * @return Void
107
		 */
108
		public function cron_activation() {
109
110
			if ( class_exists( 'PLUGIN_CRON' ) ) {
111
112
				$cron = new PLUGIN_CRON();
113
				$schedule = $cron->schedule_task(
0 ignored issues
show
Unused Code introduced by
The assignment to $schedule is dead and can be removed.
Loading history...
114
							array(
115
							'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

115
							'timestamp' => /** @scrutinizer ignore-call */ current_time('timestamp'),
Loading history...
116
							//'schedule' can be 'hourly', 'daily', 'weekly' or anything custom as defined in PLUGIN_CRON
117
							'recurrence' => 'schedule',
118
							// Use custom_corn_hook to hook into any cron process, anywhere in the plugin.
119
							'hook' => 'custom_cron_hook'
120
						) );
121
			}
122
123
		}
124
125
126
		/**
127
		 * Install plugin data
128
		 *
129
		 * @return Void
130
		 */
131
		public function db_install() {
132
133
			if ( class_exists( 'PLUGIN_DB' ) ) {
134
135
				$db = new PLUGIN_DB();
136
				$db->table = self::$plugin_table;
137
				$db->sql = "ID mediumint(9) NOT NULL AUTO_INCREMENT,
138
							date date NOT NULL,
139
							UNIQUE KEY ID (ID)";
140
				$db->build();
141
			}
142
143
			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

143
			if (/** @scrutinizer ignore-call */ get_option( '_plugin_db_exist') == '0' ) {
Loading history...
144
				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

144
				/** @scrutinizer ignore-call */ 
145
    add_action( 'admin_notices', array( $this, 'db_error_msg' ) );
Loading history...
145
			}
146
147
			$options = array(
148
				array( 'option_name', '__value__' ),
149
			);
150
			foreach ( $options as $value ) {
151
				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

151
				/** @scrutinizer ignore-call */ 
152
    update_option( $value[0], $value[1] );
Loading history...
152
			}
153
		}
154
155
156
		/**
157
		 * Notice of DB
158
		 *
159
		 * @return Html
0 ignored issues
show
Bug introduced by
The type 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...
160
		 */
161
		public function db_error_msg() { ?>
162
163
			<div class="notice notice-error is-dismissible">
164
				<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

164
				<p><?php /** @scrutinizer ignore-call */ _e( 'Database table Not installed correctly.', 'textdomain' ); ?></p>
Loading history...
165
 			</div>
166
			<?php
167
		}
168
169
170
		/**
171
		 * Uninstall plugin data
172
		 *
173
		 * @return Void
174
		 */
175
		public function db_uninstall() {
176
177
			$table_name = self::$plugin_table;
178
179
			global $wpdb;
180
			$wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}$table_name" );
181
182
			$options = array(
183
				'_plugin_db_exist'
184
			);
185
			foreach ( $options as $value ) {
186
				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

186
				/** @scrutinizer ignore-call */ 
187
    delete_option( $value );
Loading history...
187
			}
188
		}
189
190
191
		/**
192
		 * CRON callback
193
		 *
194
		 * @return Void
195
		 */
196
		public function do_cron_job_function() {
197
198
			//Do cron function
199
		}
200
201
202
		/**
203
		 * Run CRON action
204
		 *
205
		 * @return Void
206
		 */
207
		public function custom_cron_hook_cb() {
208
209
			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

209
			/** @scrutinizer ignore-call */ 
210
   add_action( 'custom_cron_hook', array( $this, 'do_cron_job_function' ) );
Loading history...
210
		}
211
212
213
		/**
214
		 * Uninstall CRON hook
215
		 *
216
		 * @return Void
217
		 */
218
		public function cron_uninstall() {
219
220
			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

220
			/** @scrutinizer ignore-call */ 
221
   wp_clear_scheduled_hook( 'custom_cron_hook' );
Loading history...
221
		}
222
223
224
		/**
225
		 * Install Custom post types
226
		 *
227
		 * @return Void
228
		 */
229
		public function cpt() {
230
231
			if ( class_exists( 'PLUGIN_CPT' ) ) new PLUGIN_CPT();
232
		}
233
234
235
		/**
236
		 * Include scripts
237
		 *
238
		 * @return Void
239
		 */
240
		public function scripts() {
241
242
			if ( class_exists( 'PLUGIN_SCRIPT' ) ) new PLUGIN_SCRIPT();
243
		}
244
245
246
		/**
247
		 * Include settings pages
248
		 *
249
		 * @return Void
250
		 */
251
		public function settings() {
252
253
			if ( class_exists( 'PLUGIN_SETTINGS' ) ) new PLUGIN_SETTINGS();
254
		}
255
256
257
		/**
258
		 * Include widget classes
259
		 *
260
		 * @return Void
261
		 */
262
		public function widgets() {
263
264
			if ( class_exists( 'PLUGIN_WIDGET' ) ) new PLUGIN_WIDGET();
265
		}
266
267
268
		/**
269
		 *Include metabox classes
270
		 *
271
		 * @return Void
272
		 */
273
		public function metabox() {
274
275
			if ( class_exists( 'PLUGIN_METABOX' ) ) new PLUGIN_METABOX();
276
		}
277
278
279
		/**
280
		 * Include shortcode classes
281
		 *
282
		 * @return Void
283
		 */
284
		public function shortcode() {
285
286
			if ( class_exists( 'PLUGIN_SHORTCODE' ) ) new PLUGIN_SHORTCODE();
287
		}
288
289
290
		/**
291
		 * Instantiate REST API
292
		 *
293
		 * @return Void
294
		 */
295
		 public function rest_api() {
296
297
			 if ( class_exists( 'PLUGIN_CUSTOM_ROUTE' ) ) new PLUGIN_CUSTOM_ROUTE();
0 ignored issues
show
Bug introduced by
The type PLUGIN_CUSTOM_ROUTE 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...
298
		 }
299
300
301
		/**
302
		 * Add the functionality files
303
		 * Available classes: PLUGIN_INSTALL, PLUGIN_DB, PLUGIN_METABOX, PLUGIN_QUERY, PLUGIN_SETTINGS, PLUGIN_SHORTCODE, PLUGIN_WIDGET
304
		 *
305
		 * @return Void
306
		 */
307
		public function functionality() {
308
309
			require_once( 'src/class-install.php' );
310
			require_once( 'src/class-db.php' );
311
			require_once( 'src/class-query.php' );
312
			require_once( 'src/class-settings.php' );
313
			require_once( 'src/class-widget.php' );
314
			require_once( 'src/class-metabox.php' );
315
			require_once( 'src/class-shortcode.php' );
316
			require_once( 'src/class-cpt.php' );
317
			require_once( 'src/class-rest.php' );
318
		}
319
320
321
		/**
322
		 * Call the dependency files
323
		 * Available classes: PLUGIN_CORN, PLUGIN_API, PLUGIN_TABLE, PLUGIN_AJAX, PLUGIN_UPLOAD, PLUGIN_SCRIPT
324
		 *
325
		 * @return Void
326
		 */
327
		public function helpers() {
328
329
			require_once( 'lib/class-cron.php' );
330
			require_once( 'lib/class-api.php' );
331
			require_once( 'lib/class-table.php' );
332
			require_once( 'lib/class-ajax.php' );
333
			require_once( 'lib/class-upload.php' );
334
			require_once( 'lib/class-script.php' );
335
		}
336
337
338
		/**
339
		 * Instantiate the plugin
340
		 *
341
		 * @return Void
342
		 */
343
		public function init() {
344
345
			$this->helpers();
346
			$this->functionality();
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( 'PLUGIN_BUILD', '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( 'PLUGIN_BUILD', 'db_uninstall' ) );
Loading history...
354
			register_uninstall_hook( PLUGIN_FILE, array( 'PLUGIN_BUILD', 'cron_uninstall' ) );
355
356
			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

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