Completed
Push — master ( 8dfca6...dd8fb7 )
by
unknown
02:32
created

lasso   F

Complexity

Total Complexity 60

Size/Duplication

Total Lines 466
Duplicated Lines 13.73 %

Coupling/Cohesion

Components 3
Dependencies 1

Importance

Changes 0
Metric Value
dl 64
loc 466
rs 3.6
c 0
b 0
f 0
wmc 60
lcom 3
cbo 1

18 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 30 1
A get_plugin_slug() 0 3 1
A get_instance() 0 9 2
A activate() 26 26 5
A deactivate() 27 27 5
A activate_new_site() 0 11 2
A get_blog_ids() 0 12 1
A single_activate() 0 17 2
A single_deactivate() 0 3 1
A load_plugin_textdomain() 0 7 1
A editus_lock_post() 0 14 2
A editus_hide_tour() 0 7 1
B editus_set_post_setting() 0 44 8
A enable_metasave() 0 14 2
A editus_do_shortcode() 0 23 2
C get_aesop_component() 0 66 15
B set_post_terms() 0 32 7
A set_date() 11 11 2

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like lasso often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use lasso, and based on these observations, apply Extract Interface, too.

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_hide_tour',     array( $this, 'editus_hide_tour' ) );
64
		add_action( 'wp_ajax_editus_set_post_setting',     array( $this, 'editus_set_post_setting' ) );
65
66
		// enable saving custom fields through REST API
67
		self::enable_metasave('post');
68
		self::enable_metasave('page');
69
		//enqueue assets
70
		new assets();
71
72
	}
73
74
	/**
75
	 * Return the plugin slug.
76
	 *
77
	 * @since    0.0.1
78
	 *
79
	 * @return    Plugin slug variable.
80
	 */
81
	public function get_plugin_slug() {
82
		return $this->plugin_slug;
83
	}
84
85
	/**
86
	 * Return an instance of this class.
87
	 *
88
	 * @since     0.0.1
89
	 *
90
	 * @return    object    A single instance of this class.
91
	 */
92
	public static function get_instance() {
93
94
		// If the single instance hasn't been set, set it now.
95
		if ( null == self::$instance ) {
96
			self::$instance = new self;
97
		}
98
99
		return self::$instance;
100
	}
101
102
	/**
103
	 * Fired when the plugin is activated.
104
	 *
105
	 * @since    0.0.1
106
	 *
107
	 * @param boolean $network_wide True if WPMU superadmin uses
108
	 *                                       "Network Activate" action, false if
109
	 *                                       WPMU is disabled or plugin is
110
	 *                                       activated on an individual blog.
111
	 */
112 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...
113
114
		if ( function_exists( 'is_multisite' ) && is_multisite() ) {
115
116
			if ( $network_wide  ) {
117
118
				// Get all blog ids
119
				$blog_ids = self::get_blog_ids();
120
121
				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...
122
123
					switch_to_blog( $blog_id );
124
					self::single_activate();
125
				}
126
127
				restore_current_blog();
128
129
			} else {
130
				self::single_activate();
131
			}
132
133
		} else {
134
			self::single_activate();
135
		}
136
137
	}
138
139
	/**
140
	 * Fired when the plugin is deactivated.
141
	 *
142
	 * @since    0.0.1
143
	 *
144
	 * @param boolean $network_wide True if WPMU superadmin uses
145
	 *                                       "Network Deactivate" action, false if
146
	 *                                       WPMU is disabled or plugin is
147
	 *                                       deactivated on an individual blog.
148
	 */
149 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...
150
151
		if ( function_exists( 'is_multisite' ) && is_multisite() ) {
152
153
			if ( $network_wide ) {
154
155
				// Get all blog ids
156
				$blog_ids = self::get_blog_ids();
157
158
				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...
159
160
					switch_to_blog( $blog_id );
161
					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...
162
163
				}
164
165
				restore_current_blog();
166
167
			} else {
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
		} else {
172
			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...
173
		}
174
175
	}
176
177
	/**
178
	 * Fired when a new site is activated with a WPMU environment.
179
	 *
180
	 * @since    0.0.1
181
	 *
182
	 * @param int     $blog_id ID of the new blog.
183
	 */
184
	public function activate_new_site( $blog_id ) {
185
186
		if ( 1 !== did_action( 'wpmu_new_blog' ) ) {
187
			return;
188
		}
189
190
		switch_to_blog( $blog_id );
191
		self::single_activate();
192
		restore_current_blog();
193
194
	}
195
196
	/**
197
	 * Get all blog ids of blogs in the current network that are:
198
	 * - not archived
199
	 * - not spam
200
	 * - not deleted
201
	 *
202
	 * @since    0.0.1
203
	 *
204
	 * @return   array|false    The blog ids, false if no matches.
205
	 */
206
	private static function get_blog_ids() {
207
208
		global $wpdb;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
209
210
		// get an array of blog ids
211
		$sql = "SELECT blog_id FROM $wpdb->blogs
212
			WHERE archived = '0' AND spam = '0'
213
			AND deleted = '0'";
214
215
		return $wpdb->get_col( $sql );
216
217
	}
218
219
	/**
220
	 * Fired for each blog when the plugin is activated.
221
	 *
222
	 * @since    0.0.1
223
	 */
224
	private static function single_activate() {
225
226
		$curr_version = get_option( 'lasso_version' );
227
228
		// update upgraded from
229
		if ( $curr_version ) {
230
			update_option( 'lasso_updated_from', $curr_version );
231
		}
232
233
		// update lasso version option
234
		update_option( 'lasso_version', LASSO_VERSION );
235
236
		// set transietn for activation welcome
237
		set_transient( '_lasso_welcome_redirect', true, 30 );
238
239
240
	}
241
242
	/**
243
	 * Fired for each blog when the plugin is deactivated.
244
	 *
245
	 * @since    0.0.1
246
	 */
247
	private static function single_deactivate() {
248
		// @TODO: Define deactivation functionality here
249
	}
250
251
	/**
252
	 * Load the plugin text domain for translation.
253
	 *
254
	 * @since    1.0.0
255
	 */
256
	public function load_plugin_textdomain() {
257
258
		$domain = $this->plugin_slug;
259
		$locale = apply_filters( 'plugin_locale', get_locale(), $domain );
260
261
		$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...
262
	}
263
	
264
    // new ajax function to lock post for editing
265
	public function editus_lock_post()
0 ignored issues
show
Coding Style introduced by
editus_lock_post uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
266
	{
267
		$post_id= $_POST["postid"];
268
		$locked = wp_check_post_lock($post_id);
269
		
270
		if (!$locked) {
271
		    wp_set_post_lock($post_id);
272
			echo "true";
273
		} else {
274
			$user_info = get_userdata($locked);
275
			echo "Post opened by ".$user_info->first_name .  " " . $user_info->last_name;
276
		}
277
		exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The method editus_lock_post() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
278
	}
279
	
280
	// new ajax function to update tour setting
281
	public function editus_hide_tour()
282
	{
283
		$user_id = get_current_user_ID();
284
				
285
		update_user_meta( $user_id, 'lasso_hide_tour', true );
286
		exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The method editus_hide_tour() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
287
	}
288
	
289
	public function editus_set_post_setting()
0 ignored issues
show
Coding Style introduced by
editus_set_post_setting uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
290
	{
291
		
292
		
293
		$data = array();
294
		parse_str($_POST['data'], $data);
295
		
296
		if (!wp_verify_nonce( $data[ 'nonce' ], 'lasso-update-post-settings' )) {
297
			wp_send_json_error();
298
			exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The method editus_set_post_setting() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
299
		}
300
		
301
		$status = isset( $data['status'] ) ? $data['status'] : false;
302
		$postid = isset( $data['postid'] ) ? $data['postid'] : false;
303
		$slug   = isset( $data['story_slug'] ) ? $data['story_slug'] : false;
304
	
305
306
		$args = array(
307
			'ID'   			=> (int) $postid,
308
			'post_name'  	=> $slug,
309
			'post_status' 	=> $status
310
		);
311
		
312
		
313
314
		wp_update_post( apply_filters( 'lasso_object_status_update_args', $args ) );
315
		
316
		// update categories
317
		$cats  = isset( $data['story_cats'] ) ? $data['story_cats'] : false;
318
		
319
		self::set_post_terms( $postid, $cats, 'category' );
320
		
321
		// update tags
322
		$tags = isset( $data['story_tags'] ) ? $data['story_tags'] : false;
323
		self::set_post_terms( $postid, $tags, 'post_tag' );
324
		
325
		//update date
326
		$date  = isset( $data['post_date'] ) ? $data['post_date'] : false;
327
		self::set_date( $postid, $date );
328
		
329
		do_action( 'lasso_post_updated', $postid, $slug, $status, get_current_user_ID() );
330
		wp_send_json_success();
331
		exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The method editus_set_post_setting() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
332
	}
333
	
334
	public static function enable_metasave($type)
335
	{
336
		register_rest_field( $type, 'metadata', array(
337
			'get_callback' => function ( $data ) {
338
				return get_post_meta( $data['id']);//, '', '' );
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
339
			}, 
340
			'update_callback' => function( $data, $post ) {
341
				foreach ($data as $key => $value) {
342
					update_post_meta($post->ID, $key, $value);
343
				}
344
				return true;
345
			}
346
		));
347
	}
348
	
349
	public function editus_do_shortcode()
0 ignored issues
show
Coding Style introduced by
editus_do_shortcode uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
350
	{
351
		
352
		$code= $_POST["code"];
353
		$code = str_replace('\"', '"', $code);
354
		
355
		$code_wrapped = lasso_wrap_shortcodes( $code);
356
		$out =  do_shortcode($code_wrapped);
357
		if ($out != '') {
358
			echo $out;
359
			exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The method editus_do_shortcode() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
360
		}
361
		
362
		// do_shortcode didn't work. Try again using wp_embed
363
364
		/** @var \WP_Embed $wp_embed */
365
		global $wp_embed;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
366
		$wp_embed->post_ID = $_POST["ID"];
367
		$out =$wp_embed->run_shortcode( $code );
368
		
369
		echo $out;
370
		exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The method editus_do_shortcode() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
371
	}
372
	
373
	public function get_aesop_component()
0 ignored issues
show
Coding Style introduced by
get_aesop_component uses the super-global variable $_POST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
374
	{
375
		
376
		
377
		$code= $_POST["code"];
378
		$atts = array(
379
		 );
380
		foreach ($_POST as $key => $value) {
381
			if ($key !="code" && $key !="action") {
382
			    //$shortcode = $shortcode.$key.'="'.$value.'" ';
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
383
				$atts[$key] = $value;
384
			}
385
		}
386
		if ($code == "aesop_video") {
387
		    require_once( ABSPATH . '/wp-content/plugins/aesop-story-engine/public/includes/components/component-video.php');
388
		    echo aesop_video_shortcode($atts);
389
		}
390
		else if ($code == "aesop_image") {
391
		    require_once( ABSPATH . '/wp-content/plugins/aesop-story-engine/public/includes/components/component-image.php');
392
		    echo aesop_image_shortcode($atts);
393
		}
394
		else if ($code == "aesop_quote") {
395
		    require_once( ABSPATH . '/wp-content/plugins/aesop-story-engine/public/includes/components/component-quote.php');
396
		    echo aesop_quote_shortcode($atts);
397
		}
398
		else if ($code == "aesop_parallax") {
399
		    require_once( ABSPATH . '/wp-content/plugins/aesop-story-engine/public/includes/components/component-parallax.php');
400
		    echo aesop_parallax_shortcode($atts);
401
		}
402
		else if ($code == "aesop_character") {
403
		    require_once( ABSPATH . '/wp-content/plugins/aesop-story-engine/public/includes/components/component-character.php');
404
		    echo aesop_character_shortcode($atts);
405
		}
406
		else if ($code == "aesop_collection") {
407
		    require_once( ABSPATH . '/wp-content/plugins/aesop-story-engine/public/includes/components/component-collections.php');
408
		    echo aesop_collection_shortcode($atts);
409
		}
410
		else if ($code == "aesop_chapter") {
411
		    require_once( ABSPATH . '/wp-content/plugins/aesop-story-engine/public/includes/components/component-heading.php');
412
		    echo aesop_chapter_shortcode($atts);
413
		}
414
		else if ($code == "aesop_content") {
415
		    require_once( ABSPATH . '/wp-content/plugins/aesop-story-engine/public/includes/components/component-cbox.php');
416
		    echo aesop_content_shortcode($atts, $atts['content_data']);
417
		}
418
		else if ($code == "aesop_gallery") {
419
		    require_once( ABSPATH . '/wp-content/plugins/aesop-story-engine/public/includes/components/component-gallery.php');
420
		    echo do_shortcode( '[aesop_gallery id="'.$atts["id"].'"]');
421
		}
422
		else if ($code == "aesop_audio") {
423
		    require_once( ABSPATH . '/wp-content/plugins/aesop-story-engine/public/includes/components/component-audio.php');
424
		    echo aesop_audio_shortcode($atts);
425
		}
426
		else {
427
			$code = '['.$code.' ';
428
			foreach ($atts as $key => $value) {
429
			    $code = ''.$key.'="'.$value.'" ';
430
			}
431
			$code = $code.']';
432
			echo do_shortcode($code);
433
		    //require_once( ABSPATH . '/wp-content/plugins/aesop-events/public/includes/shortcode.php');
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
434
		    //echo aesop_audio_shortcode($atts);
0 ignored issues
show
Unused Code Comprehensibility introduced by
72% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
435
		}
436
		
437
		exit; 
0 ignored issues
show
Coding Style Compatibility introduced by
The method get_aesop_component() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
438
	}
439
	public function set_post_terms( $postid, $value, $taxonomy ) {
440
		if( $value ) {
441
			$value = explode( ',', $value );
442
			$allow_new_category = lasso_editor_get_option( 'allow_new_category', 'lasso_editor' );
443
			
444
			if ($taxonomy =='category') {
445
                // convert from names to category ids
446
				$cats = array();
447
				foreach ($value as $cat) {
448
					$cat_id = get_cat_ID($cat);
449
					if ($cat_id !=0) {
450
						$cats [] = $cat_id;
451
					} else if ($allow_new_category) {
452
					    $cats [] = wp_create_category($cat);
453
					}
454
				}
455
				$value = $cats;
456
			}
457
	
458
			$result = wp_set_object_terms( $postid, $value, $taxonomy );
459
		}
460
		else  {
461
			//remove all terms from post
462
			$result = wp_set_object_terms( $postid, null, $taxonomy );
463
		}
464
465
		if ( ! is_wp_error( $result ) ) {
466
			return true;
467
		}else{
468
			return false;
469
		}
470
	}
471
	
472 View Code Duplication
	public function set_date( $postid, $value) {
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...
473
		if( $value ) {
474
            wp_update_post(
475
				array (
476
					'ID'            => $postid, // ID of the post to update
477
					'post_date'     => date( 'Y-m-d H:i:s',  strtotime($value) ),
478
					'post_date_gmt'     => gmdate( 'Y-m-d H:i:s',  strtotime($value) ),
479
				)
480
			);
481
		}
482
	}
483
}
484