Completed
Push — master ( 0b5bcf...1a45d6 )
by Julien
03:23
created

BetterOptin::display_error()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 23
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 23
rs 8.7972
cc 4
eloc 16
nc 3
nop 0
1
<?php
2
/**
3
 * Better Optin.
4
 *
5
 * @package   BetterOptin
6
 * @author    ThemeAvenue <[email protected]>
7
 * @license   GPL-2.0+
8
 * @link      http://themeavenue.net
9
 * @copyright 2014 ThemeAvenue
10
 *
11
 * @wordpress-plugin
12
 * Plugin Name:       BetterOptin
13
 * Plugin URI:        https://betteropt.in/
14
 * Description:       BetterOptin helps you convert your visitors in subscribers and fill up your mailing lists.
15
 * Version:           2.0.0
16
 * Author:            ThemeAvenue
17
 * Author URI:        https://themeavenue.net
18
 * Text Domain:       betteroptin
19
 * License:           GPL-2.0+
20
 * License URI:       http://www.gnu.org/licenses/gpl-2.0.txt
21
 * Domain Path:       /languages
22
 */
23
24
// If this file is called directly, abort.
25
if ( ! defined( 'WPINC' ) ) {
26
	die;
27
}
28
29
if ( ! class_exists( 'BetterOptin' ) ):
30
31
	/**
32
	 * Main BetterOptin class
33
	 *
34
	 * This class is the one and only instance of the plugin. It is used
35
	 * to load the core and all its components.
36
	 *
37
	 * @since 2.0
38
	 */
39
	final class BetterOptin {
40
41
		/**
42
		 * @var BetterOptin Holds the unique instance of BetterOptin
43
		 * @since 2.0
44
		 */
45
		private static $instance;
46
47
		/**
48
		 * Possible error message.
49
		 *
50
		 * @since 2.0
51
		 * @var null|WP_Error
52
		 */
53
		protected $error = null;
54
55
		/**
56
		 * Instantiate and return the unique BetterOptin object
57
		 *
58
		 * @since     2.0
59
		 * @return object BetterOptin Unique instance of BetterOptin
60
		 */
61
		public static function instance() {
62
63
			if ( ! isset( self::$instance ) && ! ( self::$instance instanceof BetterOptin ) ) {
64
65
				// Instantiate
66
				self::$instance = new BetterOptin;
67
				self::$instance->init();
68
69
70
			}
71
72
			return self::$instance;
73
74
		}
75
76
		/**
77
		 * Instantiate the plugin
78
		 *
79
		 * @since 2.0
80
		 * @return void
81
		 */
82
		private function init() {
83
84
			self::$instance->setup_constants();
85
86
			// Make sure the WordPress version is recent enough
87
			if ( ! self::$instance->are_dependencies_loaded() ) {
88
				self::$instance->add_error( wp_kses_post( sprintf( __( 'BetterOptin cannot load because its dependencies are missing. If you don&#039;t know what this means, please purchase the pro version on %s', 'betteroptin' ), '<a href="https://betteropt.in/?utm_source=plugin&utm_medium=dependencies_nag&utm_campaign=upsell" target="_blank">https://betteropt.in</a>' ) ) );
89
			}
90
91
			// If we have any error, don't load the plugin
92
			if ( is_a( self::$instance->error, 'WP_Error' ) ) {
93
				add_action( 'admin_notices', array( self::$instance, 'display_error' ), 10, 0 );
94
				return;
95
			}
96
97
			self::$instance->setup_database_constants();
98
			self::$instance->includes();
99
			self::$instance->load_providers();
100
101
			if ( is_admin() ) {
102
				self::$instance->includes_admin();
103
			}
104
105
			add_action( 'plugins_loaded', array( self::$instance, 'load_plugin_textdomain' ) );
106
			add_action( 'init', array( self::$instance, 'license_notification' ) );
107
			add_action( 'init', array( self::$instance, 'check_provider_ready' ) );
108
		}
109
110
		/**
111
		 * Throw error on object clone
112
		 *
113
		 * The whole idea of the singleton design pattern is that there is a single
114
		 * object therefore, we don't want the object to be cloned.
115
		 *
116
		 * @since 2.0
117
		 * @return void
118
		 */
119
		public function __clone() {
120
			// Cloning instances of the class is forbidden
121
			_doing_it_wrong( __FUNCTION__, __( 'Cheatin&#8217; huh?', 'betteroptin' ), '2.0' );
122
		}
123
124
		/**
125
		 * Disable unserializing of the class
126
		 *
127
		 * @since 2.0
128
		 * @return void
129
		 */
130
		public function __wakeup() {
131
			// Unserializing instances of the class is forbidden
132
			_doing_it_wrong( __FUNCTION__, __( 'Cheatin&#8217; huh?', 'betteroptin' ), '2.0' );
133
		}
134
135
		/**
136
		 * Setup all plugin constants
137
		 *
138
		 * @since 2.0
139
		 * @return void
140
		 */
141
		private function setup_constants() {
142
			define( 'WPBO_URL', trailingslashit( plugin_dir_url( __FILE__ ) ) );
143
			define( 'WPBO_PATH', trailingslashit( plugin_dir_path( __FILE__ ) ) );
144
			define( 'WPBO_BASENAME', plugin_basename( __FILE__ ) );
145
			define( 'WPBO_PLUGIN_FILE', __FILE__ );
146
			define( 'WPBO_VERSION', '2.0.0' );
147
			define( 'WPBO_DB_VERSION', '1' );
148
		}
149
150
		private function are_dependencies_loaded() {
151
			return is_dir( WPBO_PATH . 'vendor' );
152
		}
153
154
		/**
155
		 * Add error.
156
		 *
157
		 * Add a new error to the WP_Error object
158
		 * and create the object if it doesn't exist yet.
159
		 *
160
		 * @since  2.0
161
		 *
162
		 * @param string $message Error message to add
163
		 *
164
		 * @return void
165
		 */
166
		private function add_error( $message ) {
167
168
			if ( ! is_object( $this->error ) || ! is_a( $this->error, 'WP_Error' ) ) {
169
				$this->error = new WP_Error();
170
			}
171
172
			$this->error->add( 'addon_error', $message );
173
174
		}
175
176
		/**
177
		 * Display error.
178
		 *
179
		 * Get all the error messages and display them
180
		 * in the admin notices.
181
		 *
182
		 * @since  2.0
183
		 * @return void
184
		 */
185
		public function display_error() {
186
			if ( ! is_a( $this->error, 'WP_Error' ) ) {
187
				return;
188
			}
189
			$message = self::$instance->error->get_error_messages(); ?>
190
191
			<div class="error">
192
				<p>
193
					<?php
194
					if ( count( $message ) > 1 ) {
195
						echo '<ul>';
196
						foreach ( $message as $msg ) {
197
							echo "<li>$msg</li>";
198
						}
199
						echo '</li>';
200
					} else {
201
						echo $message[0];
202
					}
203
					?>
204
				</p>
205
			</div>
206
			<?php
207
		}
208
209
		/**
210
		 * Setup the custom database table constants
211
		 *
212
		 * @since 2.0
213
		 * @return void
214
		 */
215
		private function setup_database_constants() {
216
217
			global $wpdb;
218
219
			define( 'wpbo_analytics_table_name', 'wpbo_analytics' );
220
			define( 'wpbo_failsafe_table_name', 'wpbo_failsafe' );
221
			define( 'wpbo_analytics_table', $wpdb->prefix . wpbo_analytics_table_name );
222
			define( 'wpbo_failsafe_table', $wpdb->prefix . wpbo_failsafe_table_name );
223
224
		}
225
226
		/**
227
		 * Include all files used sitewide
228
		 *
229
		 * @since 2.0
230
		 * @return void
231
		 */
232
		private function includes() {
233
234
			if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX ) {
235
236
				require( WPBO_PATH . 'includes/scripts.php' );
237
				require( WPBO_PATH . 'includes/shortcode.php' );
238
				require( WPBO_PATH . 'includes/functions-templating.php' );
239
				require( WPBO_PATH . 'includes/functions-dummy.php' );
240
241
			}
242
243
			require( WPBO_PATH . 'includes/class-popup.php' );
244
			require( WPBO_PATH . 'includes/functions-post-type.php' );
245
			require( WPBO_PATH . 'includes/functions-analytics.php' );
246
			require( WPBO_PATH . 'includes/functions-failsafe.php' );
247
			require( WPBO_PATH . 'includes/functions-popup.php' );
248
			require( WPBO_PATH . 'includes/functions-misc.php' );
249
			require( WPBO_PATH . 'includes/functions-ajax.php' );
250
251
		}
252
253
		/**
254
		 * Include all files used in admin only
255
		 *
256
		 * @since 2.0
257
		 * @return void
258
		 */
259
		private function includes_admin() {
260
261
			require( WPBO_PATH . 'includes/admin/functions-misc.php' );
262
			require( WPBO_PATH . 'vendor/julien731/wp-dismissible-notices-handler/handler.php' );
263
264
			if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX ) {
265
				require( WPBO_PATH . 'includes/admin/class-titan-framework.php' );
266
				require( WPBO_PATH . 'includes/admin/settings/settings-general.php' );
267
				require( WPBO_PATH . 'includes/admin/functions-menu.php' );
268
				require( WPBO_PATH . 'includes/admin/functions-metabox.php' );
269
				require( WPBO_PATH . 'includes/admin/functions-list-table.php' );
270
				require( WPBO_PATH . 'includes/install.php' );
271
			}
272
273
		}
274
275
		/**
276
		 * Load all the providers from the providers directory
277
		 *
278
		 * @since 2.0
279
		 * @return void
280
		 */
281
		private function load_providers() {
282
			require( WPBO_PATH . 'includes/providers/wordpress/load.php' );
283
			require( WPBO_PATH . 'includes/providers/mailchimp/load.php' );
284
			require( WPBO_PATH . 'includes/providers/mailpoet/load.php' );
285
			require( WPBO_PATH . 'includes/providers/aweber/load.php' );
286
		}
287
288
		/**
289
		 * Load the plugin text domain for translation.
290
		 *
291
		 * @since    1.0.0
292
		 */
293
		public function load_plugin_textdomain() {
294
295
			apply_filters( 'plugin_locale', get_locale(), 'betteroptin' );
296
297
			load_plugin_textdomain( 'betteroptin', false, basename( plugin_dir_path( dirname( __FILE__ ) ) ) . '/languages/' );
298
299
		}
300
301
		/**
302
		 * Display an admin notice if license key is missing
303
		 *
304
		 * @since 2.0
305
		 * @return void
306
		 */
307
		public function license_notification() {
308
309
			if ( ! function_exists( 'DNH' ) ) {
310
				return;
311
			}
312
313
			$license = wpbo_get_option( 'license_key', '' );
314
315
			/**
316
			 * Do not show the notice if the license key has already been entered.
317
			 */
318
			if ( ! empty( $license ) ) {
319
				return;
320
			}
321
322
			$license_page = wpbo_get_settings_page_link();
323
324
			dnh_register_notice( 'wpbo_no_license', 'error', sprintf( __( 'You haven&#039;t entered your BetterOptin license key. This means that you will not get automatic updates and you will not get technical support. <a %s>Click here to enter your license key</a>.', 'betteroptin' ), "href='$license_page'" ), array( 'cap' => 'administrator' ) );
325
326
		}
327
328
		/**
329
		 * Make sure a provider is selected for the lead collection
330
		 *
331
		 * @since 2.0
332
		 * @return void
333
		 */
334
		public function check_provider_ready() {
335
336
			if ( ! function_exists( 'DNH' ) ) {
337
				return;
338
			}
339
340
			if ( false !== wpbo_is_provider_ready() ) {
341
				return;
342
			}
343
344
			$license_page = wpbo_get_settings_page_link();
345
346
			dnh_register_notice( 'wpbo_no_provider', 'error', sprintf( __( 'You haven&#039;t selected your provider for catching leads. <strong>BetterOptin will not work until you do so!</strong> <a %s>Click here to select your provider</a>.', 'betteroptin' ), "href='$license_page'" ) );
347
348
		}
349
350
	}
351
352
endif;
353
354
/**
355
 * The main function responsible for returning the unique BetterOptin instance
356
 *
357
 * Use this function like you would a global variable, except without needing
358
 * to declare the global.
359
 *
360
 * @since 2.0
361
 * @return object BetterOptin
362
 */
363
function BO() {
364
	return BetterOptin::instance();
365
}
366
367
// Get BetterOptin Running
368
BO();