GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 36eb24...b3d82b )
by Chris
04:20
created

post-promoter-pro.php (5 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/*
3
Plugin Name: Post Promoter Pro
4
Plugin URI: https://postpromoterpro.com
5
Description: Maximize your social media presence on Twitter, Facebook, and LinkedIn.
6
Version: 2.3.20
7
Author: Post Promoter Pro
8
Author URI: https://postpromoterpro.com
9
License: GPLv2
10
*/
11
12
define( 'PPP_PATH', plugin_dir_path( __FILE__ ) );
13
define( 'PPP_VERSION', '2.3.20' );
14
define( 'PPP_FILE', plugin_basename( __FILE__ ) );
15
define( 'PPP_URL', plugins_url( '/', PPP_FILE ) );
16
17
define( 'PPP_STORE_URL', 'https://postpromoterpro.com' );
18
define( 'PPP_PLUGIN_NAME', 'Post Promoter Pro' );
19
if( !class_exists( 'EDD_SL_Plugin_Updater' ) ) {
20
	// load our custom updater
21
	include( PPP_PATH . '/includes/EDD_SL_Plugin_Updater.php' );
22
}
23
24
class PostPromoterPro {
25
	private static $ppp_instance;
26
27
	private function __construct() {
28
		add_action( 'init', array( $this, 'ppp_loaddomain' ), 1 );
29
30
		if ( ! is_callable( 'curl_init' ) ) {
31
			add_action( 'admin_notices', array( $this, 'no_curl' ) );
32
		} else {
33
			global $ppp_options, $ppp_social_settings, $ppp_share_settings;
34
35
			include PPP_PATH . '/includes/general-functions.php';
36
			include PPP_PATH . '/includes/share-functions.php';
37
			include PPP_PATH . '/includes/cron-functions.php';
38
			include PPP_PATH . '/includes/filters.php';
39
			include PPP_PATH . '/includes/libs/social-loader.php';
40
41
			if( ! class_exists( 'WP_Logging' ) ) {
42
				include PPP_PATH . '/includes/libs/class-wp-logging.php';
43
			}
44
45
			if ( is_admin() ) {
46
				include PPP_PATH . '/includes/admin/upgrades.php';
47
				include PPP_PATH . '/includes/admin/do-upgrades.php';
48
				include PPP_PATH . '/includes/admin/actions.php';
49
				include PPP_PATH . '/includes/admin/admin-pages.php';
50
				include PPP_PATH . '/includes/admin/admin-ajax.php';
51
				include PPP_PATH . '/includes/admin/meta-boxes.php';
52
				include PPP_PATH . '/includes/admin/welcome.php';
53
				include PPP_PATH . '/includes/admin/dashboard.php';
54
			}
55
56
			$ppp_options         = get_option( 'ppp_options' );
57
			$ppp_social_settings = get_option( 'ppp_social_settings' );
58
			$ppp_share_settings  = get_option( 'ppp_share_settings' );
59
60
			// Do some leg work on the social settings for Issue #257
61
			if ( is_array( $ppp_share_settings ) && ! array_key_exists( 'share_on_publish', $ppp_share_settings ) ) {
62
				$tw_share_on_publish = ! empty( $ppp_share_settings['twitter']['share_on_publish'] ) ? true : false;
63
				$fb_share_on_publish = ! empty( $ppp_share_settings['facebook']['share_on_publish'] ) ? true : false;
64
				$li_share_on_publish = ! empty( $ppp_share_settings['linkedin']['share_on_publish'] ) ? true : false;
65
66
				unset(
67
					$ppp_share_settings['twitter']['share_on_publish'],
68
					$ppp_share_settings['facebook']['share_on_publish'],
69
					$ppp_share_settings['linkedin']['share_on_publish']
70
				);
71
72
				$post_types = ppp_supported_post_types();
73
				foreach ( $post_types as $key => $post_type ) {
74
					$ppp_share_settings['share_on_publish'][ $key ]['twitter'] = $tw_share_on_publish;
75
					$ppp_share_settings['share_on_publish'][ $key ]['facebook'] = $fb_share_on_publish;
76
					$ppp_share_settings['share_on_publish'][ $key ]['linkedin'] = $li_share_on_publish;
77
				}
78
79
				update_option( 'ppp_share_settings', $ppp_share_settings );
80
			}
81
82
			$this->hooks();
83
		}
84
85
	}
86
87
	/**
88
	 * Get the singleton instance of our plugin
89
	 * @return class The Instance
90
	 * @access public
91
	 */
92 1
	public static function getInstance() {
93 1
		if ( !self::$ppp_instance ) {
94
			self::$ppp_instance = new PostPromoterPro();
95
		}
96
97 1
		return self::$ppp_instance;
98
	}
99
100
	/**
101
	 * Nag if cURL is disabled
102
	 * @return void
103
	 */
104
	public function no_curl() {
105
		?>
106
		<div class="no-curl">
107
			<p><?php _e( 'Post Promoter Pro requires cURL to be enabled. Please enable it to continue using the plugin.', 'ppp-txt' ); ?></p>
108
		</div>
109
		<?php
110
	}
111
112
	private function hooks() {
113
		if ( is_admin() ) {
114
			add_action( 'admin_init', array( $this, 'ppp_register_settings' ) );
115
			add_action( 'admin_init', 'ppp_upgrade_plugin', 1 );
116
117
			// Handle licenses
118
			add_action( 'admin_init', array( $this, 'plugin_updater' ) );
119
			add_action( 'admin_init', array( $this, 'activate_license' ) );
120
			add_action( 'admin_init', array( $this, 'deactivate_license' ) );
121
122
			add_action( 'admin_menu', array( $this, 'ppp_setup_admin_menu' ), 1000, 0 );
123
			add_filter( 'plugin_action_links', array( $this, 'plugin_settings_links' ), 10, 2 );
124
			add_action( 'admin_enqueue_scripts', array( $this, 'load_custom_scripts' ), 99 );
125
			add_action( 'admin_enqueue_scripts', array( $this, 'load_styles' ), 99999 );
126
			add_action( 'wp_trash_post', 'ppp_remove_scheduled_shares', 10, 1 );
127
128
			if ( ppp_is_dev_or_staging() ) {
129
				add_action( 'admin_notices', array( $this, 'local_site_nag' ) );
130
			}
131
		}
132
133
		add_action( 'init', array( $this, 'get_actions' ) );
134
		add_action( 'wp_insert_post', 'ppp_schedule_share', 99, 2);
135
		add_action( 'transition_post_status', 'ppp_share_on_publish', 99, 3);
136
		add_action( 'init', 'ppp_add_image_sizes' );
137
		add_filter( 'wp_log_types', array( $this, 'register_log_type' ), 10, 1 );
138
	}
139
140
	/**
141
	 * Queue up the JavaScript file for the admin page, only on our admin page
142
	 * @param  string $hook The current page in the admin
143
	 * @return void
144
	 * @access public
145
	 */
146
	public function load_custom_scripts( $hook ) {
147
148
		$allowed_pages = array(
149
			'toplevel_page_ppp-options',
150
			'post-promoter_page_ppp-social-settings',
151
			'post-new.php',
152
			'post.php',
153
			'post-promoter_page_ppp-schedule-info',
154
			'profile.php',
155
			'user-edit.php',
156
		);
157
158
		$allowed_pages = apply_filters( 'ppp_admin_scripts_pages', $allowed_pages, $hook );
159
160
		if ( ! in_array( $hook, $allowed_pages ) ) {
161
			return;
162
		}
163
164
		wp_enqueue_script( 'jquery-ui-core' );
165
		wp_enqueue_script( 'jquery-ui-datepicker' );
166
167
		$jquery_ui_timepicker_path = PPP_URL . 'includes/scripts/libs/jquery-ui-timepicker-addon.js';
168
		wp_enqueue_script( 'ppp_timepicker_js', $jquery_ui_timepicker_path , array( 'jquery', 'jquery-ui-core' ), PPP_VERSION, true );
169
		wp_enqueue_script( 'ppp_core_custom_js', PPP_URL.'includes/scripts/js/ppp_custom.js', 'jquery', PPP_VERSION, true );
170
171
	}
172
173
	public function load_styles() {
174
175
		global $wp_styles;
176
177
		// List of people who make it impossible to override their jQuery UI as it's in their core CSS...so only
178
		// load ours if they don't exist
179
		if ( ! wp_style_is( 'ot-admin-css' ) && ! wp_style_is( 'jquery-ui-css' ) ) {
180
			wp_enqueue_style( 'jquery-ui-css', '//ajax.googleapis.com/ajax/libs/jqueryui/1.8.2/themes/flick/jquery-ui.css' );
181
		}
182
183
		wp_register_style( 'ppp_admin_css', PPP_URL . 'includes/scripts/css/admin-style.css', false, PPP_VERSION );
184
		wp_enqueue_style( 'ppp_admin_css' );
185
186
		$sources = array_map( 'basename', (array) wp_list_pluck( $wp_styles->registered, 'src' ) );
187
		if ( ! in_array( 'font-awesome.css', $sources ) || in_array( 'font-awesome.min.css', $sources )  ) {
188
			wp_register_style( 'font-awesome', '//maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css', false, null );
189
			wp_enqueue_style( 'font-awesome' );
190
		}
191
192
	}
193
194
	/**
195
	 * Adds the Settings and Post Promoter Pro Link to the Settings page list
196
	 * @param  array $links The current list of links
197
	 * @param  string $file The plugin file
198
	 * @return array        The new list of links, with our additional ones added
199
	 * @access public
200
	 */
201
	public function plugin_settings_links( $links, $file ) {
202
		if ( $file != PPP_FILE ) {
203
			return $links;
204
		}
205
206
		$settings_link = sprintf( '<a href="%s">%s</a>', admin_url( 'admin.php?page=ppp-options' ), __( 'Settings', 'ppp-txt' ) );
207
208
		array_unshift( $links, $settings_link );
209
210
		return $links;
211
	}
212
213
	/**
214
	 * Returns the capability (or role) required to manage the plugin.
215
	 *
216
	 * @return string A WordPress capability or role name.
217
	 */
218 1
	public static function get_manage_capability() {
219 1
		return apply_filters( 'ppp_manage_role', 'manage_options' );
220
	}
221
222
	/**
223
	 * Add the Pushover Notifications item to the Settings menu
224
	 * @return void
225
	 * @access public
226
	 */
227
	public function ppp_setup_admin_menu() {
228
		$capability = self::get_manage_capability();
229
230
		add_menu_page(
231
			__( 'Post Promoter', 'ppp-txt' ),
232
			__( 'Post Promoter', 'ppp-txt' ),
233
			$capability,
234
			'ppp-options',
235
			'ppp_admin_page'
236
		);
237
238
		add_submenu_page(
239
			'ppp-options',
240
			__( 'Social Settings', 'ppp-txt' ),
241
			__( 'Social Settings', 'ppp-txt' ),
242
			$capability,
243
			'ppp-social-settings',
244
			'ppp_display_social'
245
		);
246
247
		add_submenu_page(
248
			'ppp-options',
249
			__( 'Schedule', 'ppp-txt' ),
250
			__( 'Schedule', 'ppp-txt' ),
251
			$capability,
252
			'ppp-schedule-info',
253
			'ppp_display_schedule'
254
		);
255
256
		add_submenu_page(
257
			'ppp-options',
258
			__( 'System Info', 'ppp-txt' ),
259
			__( 'System Info', 'ppp-txt' ),
260
			$capability,
261
			'ppp-system-info',
262
			'ppp_display_sysinfo'
263
		);
264
265
		add_submenu_page(
266
			null,
267
			__( 'PPP Upgrades', 'ppp-txt' ),
268
			__( 'PPP Upgrades', 'ppp-txt' ),
269
			$capability,
270
			'ppp-upgrades',
271
			'ppp_upgrades_screen'
272
		);
273
274
	}
275
276
	/**
277
	 * Register/Whitelist our settings on the settings page, allow extensions and other plugins to hook into this
278
	 * @return void
279
	 * @access public
280
	 */
281
	public function ppp_register_settings() {
282
		register_setting( 'ppp-options', 'ppp_options' );
283
		register_setting( 'ppp-options', '_ppp_license_key', array( $this, 'ppp_sanitize_license' ) );
284
285
		register_setting( 'ppp-social-settings', 'ppp_social_settings' );
286
		register_setting( 'ppp-share-settings', 'ppp_share_settings' );
287
		do_action( 'ppp_register_additional_settings' );
288
	}
289
290
	/**
291
	 * Load the Text Domain for i18n
292
	 * @return void
293
	 * @access public
294
	 */
295
	public function ppp_loaddomain() {
296
		load_plugin_textdomain( 'ppp-txt', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
297
	}
298
299
	/**
300
	 * Sets up the EDD SL Plugin updated class
301
	 * @return void
302
	 */
303
	public function plugin_updater() {
304
		global $ppp_options;
305
		if ( defined( 'NO_AUTO_UPDATE' ) && true === NO_AUTO_UPDATE ) {
306
			return;
307
		}
308
309
		$license_key = trim( get_option( '_ppp_license_key' ) );
310
311
		if ( empty( $license_key ) ) {
312
			add_action( 'admin_notices', array( $this, 'no_license_nag' ) );
313
			return;
314
		}
315
316
		// setup the updater
317
		$edd_updater = new EDD_SL_Plugin_Updater( PPP_STORE_URL, __FILE__, array(
0 ignored issues
show
$edd_updater is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
318
				'version'   => PPP_VERSION,         // current version number
319
				'license'   => $license_key,        // license key (used get_option above to retrieve from DB)
320
				'item_name' => PPP_PLUGIN_NAME,     // name of this plugin
321
				'author'    => 'Post Promoter Pro',  // author of this plugin
322
				'beta'      => ! empty( $ppp_options['enable_betas'] ) ? true : false, // If we should install beta versions
323
			)
324
		);
325
	}
326
327
	/**
328
	 * If no license key is saved, show a notice
329
	 * @return void
330
	 */
331
	public function no_license_nag() {
332
		?>
333
		<div class="updated">
334
			<p>
335
				<?php printf(
336
					 __( 'Post Promoter Pro requires your license key to work, please <a href="%s">enter it now</a>.', 'ppp-txt' ),
337
						  admin_url( 'admin.php?page=ppp-options' )
338
					 );
339
				?>
340
			</p>
341
		</div>
342
		<?php
343
	}
344
345
	/**
346
	 * If site is detected as local, show notice
347
	 * @return void
348
	 */
349
	public function local_site_nag() {
350
		?>
351
		<div class="updated dismissible">
352
			<p>
353
				<?php
354
					_e( 'Post Promoter Pro has detected a development or staging site. To prevent unintended social media posts, sharing has been disabled.', 'ppp-txt' );
355
				?>
356
			</p>
357
		</div>
358
		<?php
359
	}
360
361
	/**
362
	 * Deactivates the license key
363
	 * @return void
364
	 */
365
	public function deactivate_license() {
366
		// listen for our activate button to be clicked
367
		if( isset( $_POST['ppp_license_deactivate'] ) ) {
368
369
			// run a quick security check
370
			if( ! check_admin_referer( 'ppp_deactivate_nonce', 'ppp_deactivate_nonce' ) ) {
371
				return;
372
			}
373
			// get out if we didn't click the Activate button
374
375
			// retrieve the license from the database
376
			$license = trim( get_option( '_ppp_license_key' ) );
377
378
379
			// data to send in our API request
380
			$api_params = array(
381
				'edd_action'=> 'deactivate_license',
382
				'license' 	=> $license,
383
				'item_name' => urlencode( PPP_PLUGIN_NAME ) // the name of our product in EDD
384
			);
385
386
			// Call the custom API.
387
			$response = wp_remote_get( add_query_arg( $api_params, PPP_STORE_URL ), array( 'timeout' => 15, 'sslverify' => false ) );
388
389
			// make sure the response came back okay
390
			if ( is_wp_error( $response ) ) {
391
				return false;
392
			}
393
394
			// decode the license data
395
			$license_data = json_decode( wp_remote_retrieve_body( $response ) );
396
397
			// $license_data->license will be either "deactivated" or "failed"
398
			if( $license_data->license == 'deactivated' ) {
399
				delete_option( '_ppp_license_key_status' );
400
			}
401
402
		}
403
	}
404
405
	/**
406
	 * Activates the license key provided
407
	 * @return void
408
	 */
409
	public function activate_license() {
410
		// listen for our activate button to be clicked
411
		if( isset( $_POST['ppp_license_activate'] ) ) {
412
413
			// run a quick security check
414
			if( ! check_admin_referer( 'ppp_activate_nonce', 'ppp_activate_nonce' ) ) {
415
				return;
416
			}
417
			// get out if we didn't click the Activate button
418
419
			// retrieve the license from the database
420
			$license = trim( get_option( '_ppp_license_key' ) );
421
422
423
			// data to send in our API request
424
			$api_params = array(
425
				'edd_action' => 'activate_license',
426
				'license'    => $license,
427
				'item_name'  => urlencode( PPP_PLUGIN_NAME ),
428
			);
429
430
			// Call the custom API.
431
			$response = wp_remote_get( add_query_arg( $api_params, PPP_STORE_URL ), array( 'timeout' => 15, 'sslverify' => false ) );
432
433
			// make sure the response came back okay
434
			if ( is_wp_error( $response ) ) {
435
				return false;
436
			}
437
438
			// decode the license data
439
			$license_data = json_decode( wp_remote_retrieve_body( $response ) );
440
441
			// $license_data->license will be either "active" or "inactive"
442
443
			update_option( '_ppp_license_key_status', $license_data->license );
444
445
		}
446
	}
447
448
	/**
449
	 * Sanatize the liscense key being provided
450
	 * @param  string $new The License key provided
451
	 * @return string      Sanitized license key
452
	 */
453
	public function ppp_sanitize_license( $new ) {
454
		$old = get_option( '_ppp_license_key' );
455
		if( $old && $old != $new ) {
456
			delete_option( '_ppp_license_key_status' ); // new license has been entered, so must reactivate
457
		}
458
		return $new;
459
	}
460
461
	/**
462
	 * Hook to listen for our actions
463
	 *
464
	 * @return void
465
	 */
466
	public function get_actions() {
467
		if ( isset( $_GET['ppp_action'] ) ) {
468
			do_action( 'ppp_' . $_GET['ppp_action'], $_GET );
469
		}
470
	}
471
472
	/**
473
	 * Register our log type for when items are shared
474
	 *
475
	 * @since  2.3
476
	 * @param  array $log_types Array of log types
477
	 * @return array
478
	 */
479
	public function register_log_type( $log_types ) {
0 ignored issues
show
The parameter $log_types is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
480
		$types[] = 'ppp_share';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$types was never initialized. Although not strictly required by PHP, it is generally a good practice to add $types = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
481
		return $types;
482
	}
483
484
}
485
486
/**
487
 * Load and access the one true instance of Post Promoter Pro
488
 *
489
 * @return object The Post_Promoter_Pro instance
490
 */
491
function post_promoter_pro() {
492 1
	return PostPromoterPro::getInstance();
493
}
494
add_action( 'plugins_loaded', 'post_promoter_pro' );
495
496
/**
497
 * On activation, setup the default options
498
 * @return void
499
 */
500
function post_promoter_pro_activation_setup() {
501
	// If the settings already exist, don't do this
502
	if ( get_option( 'ppp_options' ) ) {
503
		return;
504
	}
505
506
	$default_settings['post_types']['post'] = '1';
0 ignored issues
show
Coding Style Comprehensibility introduced by
$default_settings was never initialized. Although not strictly required by PHP, it is generally a good practice to add $default_settings = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
507
	update_option( 'ppp_options', $default_settings );
508
509
	$default_share_settings['share_on_publish'] = array(
0 ignored issues
show
Coding Style Comprehensibility introduced by
$default_share_settings was never initialized. Although not strictly required by PHP, it is generally a good practice to add $default_share_settings = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
510
		'post' => array(
511
			'twitter'  => 1,
512
			'facebook' => 1,
513
			'linkedin' => 1,
514
		),
515
	);
516
	update_option( 'ppp_share_settings', $default_share_settings );
517
518
	update_option( 'ppp_completed_upgrades', array( 'upgrade_post_meta', 'fix_scheduled_shares_2319' ) );
519
}
520
register_activation_hook( PPP_FILE, 'post_promoter_pro_activation_setup' );