Completed
Push — master ( 76c660...cbd312 )
by
unknown
01:39
created

lasso::editus_unlock_post()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
1
<?php
2
/**
3
 * AH Editor
4
 *
5
 * @package   Lasso
6
 * @author    Nick Haskins <[email protected]>
7
 * @license   GPL-2.0+
8
 * @link      http://aesopinteractive.com
9
 * @copyright 2015-2017 Aesopinteractive 
10
 */
11
namespace lasso_public_facing;
12
/**
13
 *
14
 *
15
 * @package Lasso
16
 * @author  Nick Haskins <[email protected]>
17
 */
18
class lasso {
19
20
	/**
21
	 *
22
	 *
23
	 * @since    0.0.1
24
	 *
25
	 * @var      string
26
	 */
27
	protected $plugin_slug = 'lasso';
28
29
	/**
30
	 * Instance of this class.
31
	 *
32
	 * @since    0.0.1
33
	 *
34
	 * @var      object
35
	 */
36
	protected static $instance = null;
37
38
	/**
39
	 *
40
	 *
41
	 * @since     0.0.1
42
	 */
43
	private function __construct() {
44
45
		require_once LASSO_DIR.'/public/includes/underscore-templates.php';
46
47
		require_once LASSO_DIR.'/public/includes/editor-modules.php';
48
		require_once LASSO_DIR.'/public/includes/helpers.php';
49
		require_once LASSO_DIR.'/public/includes/editor-modules--gallery.php';
50
		require_once LASSO_DIR.'/public/includes/components.php';
51
		require_once LASSO_DIR.'/public/includes/option-engine.php';
52
		require_once LASSO_DIR.'/public/includes/wrap-shortcodes.php';
53
54
		// Activate plugin when new blog is added
55
		add_action( 'wpmu_new_blog', array( $this, 'activate_new_site' ) );
56
57
		// Load plugin text domain
58
		add_action( 'init', array( $this, 'load_plugin_textdomain' ) );
59
		
60
		add_action( 'wp_ajax_get_aesop_component',     array( $this, 'get_aesop_component' ) );
61
		add_action( 'wp_ajax_editus_do_shortcode',     array( $this, 'editus_do_shortcode' ) );
62
		add_action( 'wp_ajax_editus_lock_post',     array( $this, 'editus_lock_post' ) );
63
		add_action( 'wp_ajax_editus_unlock_post',     array( $this, 'editus_unlock_post' ) );
64
		add_action( 'wp_ajax_editus_hide_tour',     array( $this, 'editus_hide_tour' ) );
65
		add_action( 'wp_ajax_editus_set_post_setting',     array( $this, 'editus_set_post_setting' ) );
66
		add_action( 'wp_ajax_editus_get_ase_options',     array( $this, 'get_ase_options' ) );
67
		add_action( 'wp_ajax_editus_delete_post',     array( $this, 'delete_post' ) );
68
		add_action( 'wp_ajax_editus_featured_img',     array( $this, 'set_featured_img' ) );
69
		add_action( 'wp_ajax_editus_del_featured_img',     array( $this, 'del_featured_img' ) );
70
71
		// enable saving custom fields through REST API
72
		self::enable_metasave('post');
73
		self::enable_metasave('page');
74
		//enqueue assets
75
		new assets();
76
77
	}
78
79
	/**
80
	 * Return the plugin slug.
81
	 *
82
	 * @since    0.0.1
83
	 *
84
	 * @return    Plugin slug variable.
85
	 */
86
	public function get_plugin_slug() {
87
		return $this->plugin_slug;
88
	}
89
90
	/**
91
	 * Return an instance of this class.
92
	 *
93
	 * @since     0.0.1
94
	 *
95
	 * @return    object    A single instance of this class.
96
	 */
97
	public static function get_instance() {
98
99
		// If the single instance hasn't been set, set it now.
100
		if ( null == self::$instance ) {
101
			self::$instance = new self;
102
		}
103
104
		return self::$instance;
105
	}
106
107
	/**
108
	 * Fired when the plugin is activated.
109
	 *
110
	 * @since    0.0.1
111
	 *
112
	 * @param boolean $network_wide True if WPMU superadmin uses
113
	 *                                       "Network Activate" action, false if
114
	 *                                       WPMU is disabled or plugin is
115
	 *                                       activated on an individual blog.
116
	 */
117 View Code Duplication
	public static function activate( $network_wide ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
118
119
		if ( function_exists( 'is_multisite' ) && is_multisite() ) {
120
121
			if ( $network_wide  ) {
122
123
				// Get all blog ids
124
				$blog_ids = self::get_blog_ids();
125
126
				foreach ( $blog_ids as $blog_id ) {
0 ignored issues
show
Bug introduced by
The expression $blog_ids of type array|false is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
127
128
					switch_to_blog( $blog_id );
129
					self::single_activate();
130
				}
131
132
				restore_current_blog();
133
134
			} else {
135
				self::single_activate();
136
			}
137
138
		} else {
139
			self::single_activate();
140
		}
141
142
	}
143
144
	/**
145
	 * Fired when the plugin is deactivated.
146
	 *
147
	 * @since    0.0.1
148
	 *
149
	 * @param boolean $network_wide True if WPMU superadmin uses
150
	 *                                       "Network Deactivate" action, false if
151
	 *                                       WPMU is disabled or plugin is
152
	 *                                       deactivated on an individual blog.
153
	 */
154 View Code Duplication
	public static function deactivate( $network_wide ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
155
156
		if ( function_exists( 'is_multisite' ) && is_multisite() ) {
157
158
			if ( $network_wide ) {
159
160
				// Get all blog ids
161
				$blog_ids = self::get_blog_ids();
162
163
				foreach ( $blog_ids as $blog_id ) {
0 ignored issues
show
Bug introduced by
The expression $blog_ids of type array|false is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
164
165
					switch_to_blog( $blog_id );
166
					self::single_deactivate();
0 ignored issues
show
Unused Code introduced by
The call to the method lasso_public_facing\lasso::single_deactivate() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
167
168
				}
169
170
				restore_current_blog();
171
172
			} else {
173
				self::single_deactivate();
0 ignored issues
show
Unused Code introduced by
The call to the method lasso_public_facing\lasso::single_deactivate() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
174
			}
175
176
		} else {
177
			self::single_deactivate();
0 ignored issues
show
Unused Code introduced by
The call to the method lasso_public_facing\lasso::single_deactivate() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
178
		}
179
180
	}
181
182
	/**
183
	 * Fired when a new site is activated with a WPMU environment.
184
	 *
185
	 * @since    0.0.1
186
	 *
187
	 * @param int     $blog_id ID of the new blog.
188
	 */
189
	public function activate_new_site( $blog_id ) {
190
191
		if ( 1 !== did_action( 'wpmu_new_blog' ) ) {
192
			return;
193
		}
194
195
		switch_to_blog( $blog_id );
196
		self::single_activate();
197
		restore_current_blog();
198
199
	}
200
201
	/**
202
	 * Get all blog ids of blogs in the current network that are:
203
	 * - not archived
204
	 * - not spam
205
	 * - not deleted
206
	 *
207
	 * @since    0.0.1
208
	 *
209
	 * @return   array|false    The blog ids, false if no matches.
210
	 */
211
	private static function get_blog_ids() {
212
213
		global $wpdb;
214
215
		// get an array of blog ids
216
		$sql = "SELECT blog_id FROM $wpdb->blogs
217
			WHERE archived = '0' AND spam = '0'
218
			AND deleted = '0'";
219
220
		return $wpdb->get_col( $sql );
221
222
	}
223
224
	/**
225
	 * Fired for each blog when the plugin is activated.
226
	 *
227
	 * @since    0.0.1
228
	 */
229
	private static function single_activate() {
230
231
		$curr_version = get_option( 'lasso_version' );
232
233
		// update upgraded from
234
		if ( $curr_version ) {
235
			update_option( 'lasso_updated_from', $curr_version );
236
		}
237
238
		// update lasso version option
239
		update_option( 'lasso_version', LASSO_VERSION );
240
241
		// set transietn for activation welcome
242
		set_transient( '_lasso_welcome_redirect', true, 30 );
243
244
245
	}
246
247
	/**
248
	 * Fired for each blog when the plugin is deactivated.
249
	 *
250
	 * @since    0.0.1
251
	 */
252
	private static function single_deactivate() {
253
		// @TODO: Define deactivation functionality here
254
	}
255
256
	/**
257
	 * Load the plugin text domain for translation.
258
	 *
259
	 * @since    1.0.0
260
	 */
261
	public function load_plugin_textdomain() {
262
263
		$domain = $this->plugin_slug;
264
		$locale = apply_filters( 'plugin_locale', get_locale(), $domain );
265
266
		$out = load_textdomain( $domain, trailingslashit( LASSO_DIR ). 'languages/' . $domain . '-' . $locale . '.mo' );
0 ignored issues
show
Unused Code introduced by
$out 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...
267
	}
268
	
269
    // new ajax function to lock post for editing
270
	public function editus_lock_post()
271
	{
272
		$post_id= $_POST["postid"];
273
		$locked = wp_check_post_lock($post_id);
274
		
275
		if (!$locked) {
276
		    wp_set_post_lock($post_id);
277
			echo "true";
278
		} else {
279
			$user_info = get_userdata($locked);
280
			echo "Post opened by ".$user_info->first_name .  " " . $user_info->last_name;
281
		}
282
		exit;
283
	}
284
	
285
	public function editus_unlock_post()
286
	{
287
		$post_id= $_POST["postid"];
288
		$locked = wp_check_post_lock($post_id);
0 ignored issues
show
Unused Code introduced by
$locked 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...
289
		delete_post_meta( $post_id, '_edit_lock');
290
		echo "true";
291
		
292
		exit;
293
	}
294
	
295
	// new ajax function to update tour setting
296
	public function editus_hide_tour()
297
	{
298
		$user_id = get_current_user_ID();
299
				
300
		update_user_meta( $user_id, 'lasso_hide_tour', true );
301
		exit;
302
	}
303
	
304
	public function editus_set_post_setting()
305
	{
306
		
307
		
308
		$data = array();
309
		parse_str($_POST['data'], $data);
310
		
311
		if (!wp_verify_nonce( $data[ 'nonce' ], 'lasso-update-post-settings' )) {
312
			wp_send_json_error();
313
			exit;
314
		}
315
		
316
		$status = isset( $data['status'] ) ? $data['status'] : false;
317
		$postid = isset( $data['postid'] ) ? $data['postid'] : false;
318
		$slug   = isset( $data['story_slug'] ) ? $data['story_slug'] : false;
319
	
320
321
		$args = array(
322
			'ID'   			=> (int) $postid,
323
			'post_name'  	=> $slug,
324
			'post_status' 	=> $status
325
		);
326
		
327
		
328
329
		wp_update_post( apply_filters( 'lasso_object_status_update_args', $args ) );
330
		
331
		// update categories
332
		$cats  = isset( $data['story_cats'] ) ? $data['story_cats'] : false;
333
		
334
		self::set_post_terms( $postid, $cats, 'category' );
335
		
336
		// update tags
337
		$tags = isset( $data['story_tags'] ) ? $data['story_tags'] : false;
338
		self::set_post_terms( $postid, $tags, 'post_tag' );
339
		
340
		//update date
341
		$date  = isset( $data['post_date'] ) ? $data['post_date'] : false;
342
		self::set_date( $postid, $date );
343
		
344
		do_action( 'lasso_post_updated', $postid, $slug, $status, get_current_user_ID() );
345
		$response= array(
346
			'link'   => get_permalink($postid). (($status=='publish') ? '' : '&preview=true')
347
		);
348
		wp_send_json_success($response);
349
		exit;
350
	}
351
	
352
	public static function enable_metasave($type)
353
	{
354
		register_rest_field( $type, 'metadata', array(
355
			'get_callback' => function ( $data ) {
356
				return get_post_meta( $data['id']);//, '', '' );
357
			}, 
358
			'update_callback' => function( $data, $post ) {
359
				foreach ($data as $key => $value) {
360
					update_post_meta($post->ID, $key, $value);
361
				}
362
				return true;
363
			}
364
		));
365
	}
366
	
367
	public function editus_do_shortcode()
368
	{
369
		
370
		$code= $_POST["code"];
371
		$code = str_replace('\"', '"', $code);
372
		
373
		$code_wrapped = lasso_wrap_shortcodes( $code);
374
		$out =  do_shortcode($code);
375
		if ($out != '') {
376
			$out =  do_shortcode($code_wrapped);
377
			echo $out;
378
			exit;
379
		}
380
		
381
		// do_shortcode didn't work. Try again using wp_embed
382
383
		/** @var \WP_Embed $wp_embed */
384
		global $wp_embed;
385
		$wp_embed->post_ID = $_POST["ID"];
386
		$out =$wp_embed->run_shortcode( $code_wrapped );
387
		
388
		echo $out;
389
		exit;
390
	}
391
	
392
	public function get_aesop_component()
393
	{
394
		
395
		
396
		$code= $_POST["code"];
397
		$atts = array(
398
		 );
399
		foreach ($_POST as $key => $value) {
400
			if ($key !="code" && $key !="action") {
401
			    //$shortcode = $shortcode.$key.'="'.$value.'" ';
402
				$atts[$key] = $value;
403
			}
404
		}
405
		if ($code == "aesop_video") {
406
		    require_once( ABSPATH . '/wp-content/plugins/aesop-story-engine/public/includes/components/component-video.php');
407
		    echo aesop_video_shortcode($atts);
408
		}
409
		else if ($code == "aesop_image") {
410
		    require_once( ABSPATH . '/wp-content/plugins/aesop-story-engine/public/includes/components/component-image.php');
411
		    echo aesop_image_shortcode($atts);
412
		}
413
		else if ($code == "aesop_quote") {
414
		    require_once( ABSPATH . '/wp-content/plugins/aesop-story-engine/public/includes/components/component-quote.php');
415
		    echo aesop_quote_shortcode($atts);
416
		}
417
		else if ($code == "aesop_parallax") {
418
		    require_once( ABSPATH . '/wp-content/plugins/aesop-story-engine/public/includes/components/component-parallax.php');
419
		    echo aesop_parallax_shortcode($atts);
420
		}
421
		else if ($code == "aesop_character") {
422
		    require_once( ABSPATH . '/wp-content/plugins/aesop-story-engine/public/includes/components/component-character.php');
423
		    echo aesop_character_shortcode($atts);
424
		}
425
		else if ($code == "aesop_collection") {
426
		    require_once( ABSPATH . '/wp-content/plugins/aesop-story-engine/public/includes/components/component-collections.php');
427
		    echo aesop_collection_shortcode($atts);
428
		}
429
		else if ($code == "aesop_chapter") {
430
		    require_once( ABSPATH . '/wp-content/plugins/aesop-story-engine/public/includes/components/component-heading.php');
431
		    echo aesop_chapter_shortcode($atts);
432
		}
433
		else if ($code == "aesop_content") {
434
		    require_once( ABSPATH . '/wp-content/plugins/aesop-story-engine/public/includes/components/component-cbox.php');
435
		    echo aesop_content_shortcode($atts, $atts['content_data']);
436
		}
437
		else if ($code == "aesop_gallery") {
438
		    require_once( ABSPATH . '/wp-content/plugins/aesop-story-engine/public/includes/components/component-gallery.php');
439
		    echo do_shortcode( '[aesop_gallery id="'.$atts["id"].'"]');
440
		}
441
		else if ($code == "aesop_audio") {
442
		    require_once( ABSPATH . '/wp-content/plugins/aesop-story-engine/public/includes/components/component-audio.php');
443
		    echo aesop_audio_shortcode($atts);
444
		}
445
		else {
446
			$code = '['.$code.' ';
447
			foreach ($atts as $key => $value) {
448
			    $code = ''.$key.'="'.$value.'" ';
449
			}
450
			$code = $code.']';
451
			echo do_shortcode($code);
452
		    //require_once( ABSPATH . '/wp-content/plugins/aesop-events/public/includes/shortcode.php');
453
		    //echo aesop_audio_shortcode($atts);
454
		}
455
		
456
		exit; 
457
	}
458
	
459
	
460
	public function get_ase_options()
461
	{
462
		$blob = lasso_editor_options_blob();
463
		$code= $_POST["component"];
464
		echo $blob[$code];
465
		exit; 
466
	}
467
	
468
	public function delete_post( ) {
469
470
		$postid = isset( $_POST['postid'] ) ? $_POST['postid'] : false;
471
472
		// bail out if teh current user can't publish posts
473
		if ( !lasso_user_can( 'delete_post', $postid ) )
474
			return;
475
		
476
		if (!wp_verify_nonce( $_POST[ 'nonce' ], 'lasso_delete_post' )) {
477
			wp_send_json_error();
478
			exit;
479
		}
480
481
		$args = array(
482
			'ID'   			=> (int) $postid,
483
			'post_status' 	=> 'trash'
484
		);
485
486
		wp_update_post( apply_filters( 'lasso_object_deleted_args', $args ) );
487
488
		do_action( 'lasso_object_deleted', $postid, get_current_user_ID() );
489
490
		exit;
491
	}
492
	
493
	public function set_featured_img( ) {
494
495
		$postid  	= isset( $_POST['postid'] ) ? $_POST['postid'] : false;
496
		$image_id  	= isset( $_POST['image_id'] ) ? absint( $_POST['image_id'] ) : false;
497
		if (!wp_verify_nonce( $_POST[ 'nonce' ], 'lasso_gallery' )) {
498
			wp_send_json_error();
499
			exit;
500
		}	
501
502
		set_post_thumbnail( $postid, $image_id );
503
504
		do_action( 'lasso_featured_image_set', $postid, $image_id, get_current_user_ID() );
505
506
		exit;
507
	}
508
	
509
	public function del_featured_img( ) {
510
511
		$postid  = isset( $_POST['postid'] ) ? $_POST['postid'] : false;
512
		if (!wp_verify_nonce( $_POST[ 'nonce' ], 'lasso_gallery' )) {
513
			wp_send_json_error();
514
			exit;
515
		}	
516
517
		delete_post_thumbnail( $postid );
518
519
		do_action( 'lasso_featured_image_deleted', $postid, get_current_user_ID() );
520
521
		exit;
522
	}
523
	
524
	/*public function revision_get( ) {
525
		$args = array();
526
		if ( isset( $_POST[ 'limit' ] ) ) {
527
			$args[ 'posts_per_page' ] = $data[ 'limit' ];
528
		}else{
529
			$args[ 'posts_per_page' ] = 6; // we start at revision 0
530
		}
531
532
		$revisions = wp_get_post_revisions( $_POST[ 'postid' ], $args  );
533
		if ( is_array( $revisions )  && ! empty( $revisions )  ) {
534
			self::set_revisions( $data[ 'postid' ], $revisions );
535
		}
536
537
		return self::$revisions;
538
	}*/
539
	
540
	public function set_post_terms( $postid, $value, $taxonomy ) {
541
		if( $value ) {
542
			$value = explode( ',', $value );
543
			$allow_new_category = lasso_editor_get_option( 'allow_new_category', 'lasso_editor' );
544
			
545
			if ($taxonomy =='category') {
546
                // convert from names to category ids
547
				$cats = array();
548
				foreach ($value as $cat) {
549
					$cat_id = get_cat_ID($cat);
550
					if ($cat_id !=0) {
551
						$cats [] = $cat_id;
552
					} else if ($allow_new_category) {
553
					    $cats [] = wp_create_category($cat);
554
					}
555
				}
556
				$value = $cats;
557
			}
558
	
559
			$result = wp_set_object_terms( $postid, $value, $taxonomy );
560
		}
561
		else  {
562
			//remove all terms from post
563
			$result = wp_set_object_terms( $postid, null, $taxonomy );
564
		}
565
566
		if ( ! is_wp_error( $result ) ) {
567
			return true;
568
		}else{
569
			return false;
570
		}
571
	}
572
	
573
	function getEnglishMonthName($foreignMonthName){
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
574
575
		  setlocale(LC_ALL, 'en_US');
576
577
		  $month_numbers = range(1,12);
578
579
		  foreach($month_numbers as $month)
580
			$english_months[] = strftime('%B',mktime(0,0,0,$month,1,2011));
0 ignored issues
show
Coding Style Comprehensibility introduced by
$english_months was never initialized. Although not strictly required by PHP, it is generally a good practice to add $english_months = 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...
581
582
		  setlocale(LC_ALL, get_locale());
583
584
		  foreach($month_numbers as $month)
585
			$foreign_months[] = utf8_encode(strftime('%B',mktime(0,0,0,$month,1,2011)));
0 ignored issues
show
Coding Style Comprehensibility introduced by
$foreign_months was never initialized. Although not strictly required by PHP, it is generally a good practice to add $foreign_months = 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...
586
587
		  return str_replace($foreign_months, $english_months, $foreignMonthName);
0 ignored issues
show
Bug introduced by
The variable $foreign_months does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
Bug introduced by
The variable $english_months does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
588
	}
589
590
591
	
592
	public function set_date( $postid, $value) {
593
		if( $value ) {
594
			$value = self::getEnglishMonthName($value)." ".date("H:i:s", current_time( 'timestamp', 1 ));
595
            wp_update_post(
596
				array (
597
					'ID'            => $postid, // ID of the post to update
598
					'post_date'     => date( 'Y-m-d H:i:s',  strtotime($value) ),
599
					'post_date_gmt'     => gmdate( 'Y-m-d H:i:s',  strtotime($value) ),
600
				)
601
			);
602
		}
603
	}
604
}
605