Completed
Push — master ( c34fb4...51d4c2 )
by
unknown
01:31
created

lasso::publish_post()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 8
rs 10
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
        add_action( 'wp_ajax_editus_publish_post',     array( $this, 'publish_post' ) );
72
73
		// enable saving custom fields through REST API
74
		self::enable_metasave('post');
75
		self::enable_metasave('page');
76
		//enqueue assets
77
		new assets();
78
79
	}
80
81
	/**
82
	 * Return the plugin slug.
83
	 *
84
	 * @since    0.0.1
85
	 *
86
	 * @return    Plugin slug variable.
87
	 */
88
	public function get_plugin_slug() {
89
		return $this->plugin_slug;
90
	}
91
92
	/**
93
	 * Return an instance of this class.
94
	 *
95
	 * @since     0.0.1
96
	 *
97
	 * @return    object    A single instance of this class.
98
	 */
99
	public static function get_instance() {
100
101
		// If the single instance hasn't been set, set it now.
102
		if ( null == self::$instance ) {
103
			self::$instance = new self;
104
		}
105
106
		return self::$instance;
107
	}
108
109
	/**
110
	 * Fired when the plugin is activated.
111
	 *
112
	 * @since    0.0.1
113
	 *
114
	 * @param boolean $network_wide True if WPMU superadmin uses
115
	 *                                       "Network Activate" action, false if
116
	 *                                       WPMU is disabled or plugin is
117
	 *                                       activated on an individual blog.
118
	 */
119 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...
120
121
		if ( function_exists( 'is_multisite' ) && is_multisite() ) {
122
123
			if ( $network_wide  ) {
124
125
				// Get all blog ids
126
				$blog_ids = self::get_blog_ids();
127
128
				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...
129
130
					switch_to_blog( $blog_id );
131
					self::single_activate();
132
				}
133
134
				restore_current_blog();
135
136
			} else {
137
				self::single_activate();
138
			}
139
140
		} else {
141
			self::single_activate();
142
		}
143
144
	}
145
146
	/**
147
	 * Fired when the plugin is deactivated.
148
	 *
149
	 * @since    0.0.1
150
	 *
151
	 * @param boolean $network_wide True if WPMU superadmin uses
152
	 *                                       "Network Deactivate" action, false if
153
	 *                                       WPMU is disabled or plugin is
154
	 *                                       deactivated on an individual blog.
155
	 */
156 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...
157
158
		if ( function_exists( 'is_multisite' ) && is_multisite() ) {
159
160
			if ( $network_wide ) {
161
162
				// Get all blog ids
163
				$blog_ids = self::get_blog_ids();
164
165
				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...
166
167
					switch_to_blog( $blog_id );
168
					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...
169
170
				}
171
172
				restore_current_blog();
173
174
			} else {
175
				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...
176
			}
177
178
		} else {
179
			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...
180
		}
181
182
	}
183
184
	/**
185
	 * Fired when a new site is activated with a WPMU environment.
186
	 *
187
	 * @since    0.0.1
188
	 *
189
	 * @param int     $blog_id ID of the new blog.
190
	 */
191
	public function activate_new_site( $blog_id ) {
192
193
		if ( 1 !== did_action( 'wpmu_new_blog' ) ) {
194
			return;
195
		}
196
197
		switch_to_blog( $blog_id );
198
		self::single_activate();
199
		restore_current_blog();
200
201
	}
202
203
	/**
204
	 * Get all blog ids of blogs in the current network that are:
205
	 * - not archived
206
	 * - not spam
207
	 * - not deleted
208
	 *
209
	 * @since    0.0.1
210
	 *
211
	 * @return   array|false    The blog ids, false if no matches.
212
	 */
213
	private static function get_blog_ids() {
214
215
		global $wpdb;
216
217
		// get an array of blog ids
218
		$sql = "SELECT blog_id FROM $wpdb->blogs
219
			WHERE archived = '0' AND spam = '0'
220
			AND deleted = '0'";
221
222
		return $wpdb->get_col( $sql );
223
224
	}
225
226
	/**
227
	 * Fired for each blog when the plugin is activated.
228
	 *
229
	 * @since    0.0.1
230
	 */
231
	private static function single_activate() {
232
233
		$curr_version = get_option( 'lasso_version' );
234
235
		// update upgraded from
236
		if ( $curr_version ) {
237
			update_option( 'lasso_updated_from', $curr_version );
238
		}
239
240
		// update lasso version option
241
		update_option( 'lasso_version', LASSO_VERSION );
242
243
		// set transietn for activation welcome
244
		set_transient( '_lasso_welcome_redirect', true, 30 );
245
246
247
	}
248
249
	/**
250
	 * Fired for each blog when the plugin is deactivated.
251
	 *
252
	 * @since    0.0.1
253
	 */
254
	private static function single_deactivate() {
255
		// @TODO: Define deactivation functionality here
256
	}
257
258
	/**
259
	 * Load the plugin text domain for translation.
260
	 *
261
	 * @since    1.0.0
262
	 */
263
	public function load_plugin_textdomain() {
264
265
		$domain = $this->plugin_slug;
266
		$locale = apply_filters( 'plugin_locale', get_locale(), $domain );
267
268
		$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...
269
	}
270
	
271
    // new ajax function to lock post for editing
272
	public function editus_lock_post()
273
	{
274
		$post_id= $_POST["postid"];
275
		$locked = wp_check_post_lock($post_id);
276
		
277
		if (!$locked) {
278
		    wp_set_post_lock($post_id);
279
			echo "true";
280
		} else {
281
			$user_info = get_userdata($locked);
282
			echo "Post opened by ".$user_info->first_name .  " " . $user_info->last_name;
283
		}
284
		exit;
285
	}
286
	
287
	public function editus_unlock_post()
288
	{
289
		$post_id= $_POST["postid"];
290
		$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...
291
		delete_post_meta( $post_id, '_edit_lock');
292
		echo "true";
293
		
294
		exit;
295
	}
296
	
297
	// new ajax function to update tour setting
298
	public function editus_hide_tour()
299
	{
300
		$user_id = get_current_user_ID();
301
				
302
		update_user_meta( $user_id, 'lasso_hide_tour', true );
303
		exit;
304
	}
305
	
306
	public function editus_set_post_setting()
307
	{
308
		
309
		
310
		$data = array();
311
		parse_str($_POST['data'], $data);
312
		
313
		if (!wp_verify_nonce( $data[ 'nonce' ], 'lasso-update-post-settings' )) {
314
			wp_send_json_error();
315
			exit;
316
		}
317
		
318
		$status = isset( $data['status'] ) ? $data['status'] : false;
319
		$postid = isset( $data['postid'] ) ? $data['postid'] : false;
320
		$slug   = isset( $data['story_slug'] ) ? $data['story_slug'] : false;
321
	
322
323
		$args = array(
324
			'ID'   			=> (int) $postid,
325
			'post_name'  	=> $slug,
326
			'post_status' 	=> $status
327
		);
328
		
329
		
330
331
		wp_update_post( apply_filters( 'lasso_object_status_update_args', $args ) );
332
		
333
		// update categories
334
		$cats  = isset( $data['story_cats'] ) ? $data['story_cats'] : false;
335
		
336
		self::set_post_terms( $postid, $cats, 'category' );
337
		
338
		// update tags
339
		$tags = isset( $data['story_tags'] ) ? $data['story_tags'] : false;
340
		self::set_post_terms( $postid, $tags, 'post_tag' );
341
		
342
		//update date
343
		$date  = isset( $data['post_date'] ) ? $data['post_date'] : false;
344
		self::set_date( $postid, $date );
345
		
346
		do_action( 'lasso_post_updated', $postid, $slug, $status, get_current_user_ID() );
347
		$response= array(
348
			'link'   => get_permalink($postid). (($status=='publish') ? '' : '&preview=true')
349
		);
350
		wp_send_json_success($response);
351
		exit;
352
	}
353
	
354
	public static function enable_metasave($type)
355
	{
356
		register_rest_field( $type, 'metadata', array(
357
			'get_callback' => function ( $data ) {
358
				return get_post_meta( $data['id']);//, '', '' );
359
			}, 
360
			'update_callback' => function( $data, $post ) {
361
				foreach ($data as $key => $value) {
362
					update_post_meta($post->ID, $key, $value);
363
				}
364
				return true;
365
			}
366
		));
367
	}
368
	
369
	public function editus_do_shortcode()
370
	{
371
		
372
		$code= $_POST["code"];
373
		$code = str_replace('\"', '"', $code);
374
		
375
		$code_wrapped = lasso_wrap_shortcodes( $code);
376
		$out =  do_shortcode($code);
377
		if ($out != '') {
378
			$out =  do_shortcode($code_wrapped);
379
			echo $out;
380
			exit;
381
		}
382
		
383
		// do_shortcode didn't work. Try again using wp_embed
384
385
		/** @var \WP_Embed $wp_embed */
386
		global $wp_embed;
387
		$wp_embed->post_ID = $_POST["ID"];
388
		$out =$wp_embed->run_shortcode( $code_wrapped );
389
		
390
		echo $out;
391
		exit;
392
	}
393
	
394
	public function get_aesop_component()
395
	{
396
		
397
		
398
		$code= $_POST["code"];
399
		$atts = array(
400
		 );
401
		foreach ($_POST as $key => $value) {
402
			if ($key !="code" && $key !="action") {
403
			    //$shortcode = $shortcode.$key.'="'.$value.'" ';
404
				$atts[$key] = $value;
405
			}
406
		}
407
		if ($code == "aesop_video") {
408
		    require_once( ABSPATH . '/wp-content/plugins/aesop-story-engine/public/includes/components/component-video.php');
409
		    echo aesop_video_shortcode($atts);
410
		}
411
		else if ($code == "aesop_image") {
412
		    require_once( ABSPATH . '/wp-content/plugins/aesop-story-engine/public/includes/components/component-image.php');
413
		    echo aesop_image_shortcode($atts);
414
		}
415
		else if ($code == "aesop_quote") {
416
		    require_once( ABSPATH . '/wp-content/plugins/aesop-story-engine/public/includes/components/component-quote.php');
417
		    echo aesop_quote_shortcode($atts);
418
		}
419
		else if ($code == "aesop_parallax") {
420
		    require_once( ABSPATH . '/wp-content/plugins/aesop-story-engine/public/includes/components/component-parallax.php');
421
		    echo aesop_parallax_shortcode($atts);
422
		}
423
		else if ($code == "aesop_character") {
424
		    require_once( ABSPATH . '/wp-content/plugins/aesop-story-engine/public/includes/components/component-character.php');
425
		    echo aesop_character_shortcode($atts);
426
		}
427
		else if ($code == "aesop_collection") {
428
		    require_once( ABSPATH . '/wp-content/plugins/aesop-story-engine/public/includes/components/component-collections.php');
429
		    echo aesop_collection_shortcode($atts);
430
		}
431
		else if ($code == "aesop_chapter") {
432
		    require_once( ABSPATH . '/wp-content/plugins/aesop-story-engine/public/includes/components/component-heading.php');
433
		    echo aesop_chapter_shortcode($atts);
434
		}
435
		else if ($code == "aesop_content") {
436
		    require_once( ABSPATH . '/wp-content/plugins/aesop-story-engine/public/includes/components/component-cbox.php');
437
		    echo aesop_content_shortcode($atts, $atts['content_data']);
438
		}
439
		else if ($code == "aesop_gallery") {
440
		    require_once( ABSPATH . '/wp-content/plugins/aesop-story-engine/public/includes/components/component-gallery.php');
441
		    echo do_shortcode( '[aesop_gallery id="'.$atts["id"].'"]');
442
		}
443
		else if ($code == "aesop_audio") {
444
		    require_once( ABSPATH . '/wp-content/plugins/aesop-story-engine/public/includes/components/component-audio.php');
445
		    echo aesop_audio_shortcode($atts);
446
		}
447
		else {
448
			$code = '['.$code.' ';
449
			foreach ($atts as $key => $value) {
450
			    $code = ''.$key.'="'.$value.'" ';
451
			}
452
			$code = $code.']';
453
			echo do_shortcode($code);
454
		    //require_once( ABSPATH . '/wp-content/plugins/aesop-events/public/includes/shortcode.php');
455
		    //echo aesop_audio_shortcode($atts);
456
		}
457
		
458
		exit; 
459
	}
460
	
461
	
462
	public function get_ase_options()
463
	{
464
		$blob = lasso_editor_options_blob();
465
		$code= $_POST["component"];
466
		echo $blob[$code];
467
		exit; 
468
	}
469
	
470
	public function delete_post( ) {
471
472
		$postid = isset( $_POST['postid'] ) ? $_POST['postid'] : false;
473
474
		// bail out if the current user can't publish posts
475
		if ( !lasso_user_can( 'delete_post', $postid ) )
476
			return;
477
		
478
		if (!wp_verify_nonce( $_POST[ 'nonce' ], 'lasso_delete_post' )) {
479
			wp_send_json_error();
480
			exit;
481
		}
482
483
		$args = array(
484
			'ID'   			=> (int) $postid,
485
			'post_status' 	=> 'trash'
486
		);
487
488
		wp_update_post( apply_filters( 'lasso_object_deleted_args', $args ) );
489
490
		do_action( 'lasso_object_deleted', $postid, get_current_user_ID() );
491
492
		exit;
493
	}
494
    
495
    /* This function doesn't actually publish post, but should be called when a post is published */
496
    public function publish_post( ) {
497
498
		$post_id = isset( $_POST['postid'] ) ? $_POST['postid'] : false;
499
        
500
        do_action( 'transition_post_status', 'publish', 'draft', get_post( $post_id ) );
501
502
		exit;
503
	}
504
	
505
	public function set_featured_img( ) {
506
507
		$postid  	= isset( $_POST['postid'] ) ? $_POST['postid'] : false;
508
		$image_id  	= isset( $_POST['image_id'] ) ? absint( $_POST['image_id'] ) : false;
509
		if (!wp_verify_nonce( $_POST[ 'nonce' ], 'lasso_gallery' )) {
510
			wp_send_json_error();
511
			exit;
512
		}	
513
514
		set_post_thumbnail( $postid, $image_id );
515
516
		do_action( 'lasso_featured_image_set', $postid, $image_id, get_current_user_ID() );
517
518
		exit;
519
	}
520
	
521
	public function del_featured_img( ) {
522
523
		$postid  = isset( $_POST['postid'] ) ? $_POST['postid'] : false;
524
		if (!wp_verify_nonce( $_POST[ 'nonce' ], 'lasso_gallery' )) {
525
			wp_send_json_error();
526
			exit;
527
		}	
528
529
		delete_post_thumbnail( $postid );
530
531
		do_action( 'lasso_featured_image_deleted', $postid, get_current_user_ID() );
532
533
		exit;
534
	}
535
	
536
	/*public function revision_get( ) {
537
		$args = array();
538
		if ( isset( $_POST[ 'limit' ] ) ) {
539
			$args[ 'posts_per_page' ] = $data[ 'limit' ];
540
		}else{
541
			$args[ 'posts_per_page' ] = 6; // we start at revision 0
542
		}
543
544
		$revisions = wp_get_post_revisions( $_POST[ 'postid' ], $args  );
545
		if ( is_array( $revisions )  && ! empty( $revisions )  ) {
546
			self::set_revisions( $data[ 'postid' ], $revisions );
547
		}
548
549
		return self::$revisions;
550
	}*/
551
	
552
	public function set_post_terms( $postid, $value, $taxonomy ) {
553
		if( $value ) {
554
			$value = explode( ',', $value );
555
			$allow_new_category = lasso_editor_get_option( 'allow_new_category', 'lasso_editor' );
556
			
557
			if ($taxonomy =='category') {
558
                // convert from names to category ids
559
				$cats = array();
560
				foreach ($value as $cat) {
561
					$cat_id = get_cat_ID($cat);
562
					if ($cat_id !=0) {
563
						$cats [] = $cat_id;
564
					} else if ($allow_new_category) {
565
					    $cats [] = wp_create_category($cat);
566
					}
567
				}
568
				$value = $cats;
569
			}
570
	
571
			$result = wp_set_object_terms( $postid, $value, $taxonomy );
572
		}
573
		else  {
574
			//remove all terms from post
575
			$result = wp_set_object_terms( $postid, null, $taxonomy );
576
		}
577
578
		if ( ! is_wp_error( $result ) ) {
579
			return true;
580
		}else{
581
			return false;
582
		}
583
	}
584
	
585
	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...
586
587
		  setlocale(LC_ALL, 'en_US');
588
589
		  $month_numbers = range(1,12);
590
591
		  foreach($month_numbers as $month)
592
			$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...
593
594
		  setlocale(LC_ALL, get_locale());
595
596
		  foreach($month_numbers as $month)
597
			$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...
598
599
		  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...
600
	}
601
602
603
	
604
	public function set_date( $postid, $value) {
605
		if( $value ) {
606
			$value = self::getEnglishMonthName($value)." ".date("H:i:s", current_time( 'timestamp', 1 ));
607
            wp_update_post(
608
				array (
609
					'ID'            => $postid, // ID of the post to update
610
					'post_date'     => date( 'Y-m-d H:i:s',  strtotime($value) ),
611
					'post_date_gmt'     => gmdate( 'Y-m-d H:i:s',  strtotime($value) ),
612
				)
613
			);
614
		}
615
	}
616
}
617