Completed
Push — master ( 13652e...8e57b5 )
by
unknown
01:51
created

lasso   B

Complexity

Total Complexity 41

Size/Duplication

Total Lines 352
Duplicated Lines 15.06 %

Coupling/Cohesion

Components 3
Dependencies 1

Importance

Changes 0
Metric Value
dl 53
loc 352
rs 8.2769
c 0
b 0
f 0
wmc 41
lcom 3
cbo 1

14 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 28 1
A get_plugin_slug() 0 3 1
A get_instance() 0 9 2
B activate() 26 26 5
B 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 enable_metasave() 0 14 2
A editus_do_shortcode() 0 9 1
C get_aesop_component() 0 66 15

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 Aesopinteractive LLC
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
64
		// enable saving custom fields through REST API
65
		self::enable_metasave('post');
66
		self::enable_metasave('page');
67
		//enqueue assets
68
		new assets();
69
70
	}
71
72
	/**
73
	 * Return the plugin slug.
74
	 *
75
	 * @since    0.0.1
76
	 *
77
	 * @return    Plugin slug variable.
78
	 */
79
	public function get_plugin_slug() {
80
		return $this->plugin_slug;
81
	}
82
83
	/**
84
	 * Return an instance of this class.
85
	 *
86
	 * @since     0.0.1
87
	 *
88
	 * @return    object    A single instance of this class.
89
	 */
90
	public static function get_instance() {
91
92
		// If the single instance hasn't been set, set it now.
93
		if ( null == self::$instance ) {
94
			self::$instance = new self;
95
		}
96
97
		return self::$instance;
98
	}
99
100
	/**
101
	 * Fired when the plugin is activated.
102
	 *
103
	 * @since    0.0.1
104
	 *
105
	 * @param boolean $network_wide True if WPMU superadmin uses
106
	 *                                       "Network Activate" action, false if
107
	 *                                       WPMU is disabled or plugin is
108
	 *                                       activated on an individual blog.
109
	 */
110 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...
111
112
		if ( function_exists( 'is_multisite' ) && is_multisite() ) {
113
114
			if ( $network_wide  ) {
115
116
				// Get all blog ids
117
				$blog_ids = self::get_blog_ids();
118
119
				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...
120
121
					switch_to_blog( $blog_id );
122
					self::single_activate();
123
				}
124
125
				restore_current_blog();
126
127
			} else {
128
				self::single_activate();
129
			}
130
131
		} else {
132
			self::single_activate();
133
		}
134
135
	}
136
137
	/**
138
	 * Fired when the plugin is deactivated.
139
	 *
140
	 * @since    0.0.1
141
	 *
142
	 * @param boolean $network_wide True if WPMU superadmin uses
143
	 *                                       "Network Deactivate" action, false if
144
	 *                                       WPMU is disabled or plugin is
145
	 *                                       deactivated on an individual blog.
146
	 */
147 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...
148
149
		if ( function_exists( 'is_multisite' ) && is_multisite() ) {
150
151
			if ( $network_wide ) {
152
153
				// Get all blog ids
154
				$blog_ids = self::get_blog_ids();
155
156
				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...
157
158
					switch_to_blog( $blog_id );
159
					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...
160
161
				}
162
163
				restore_current_blog();
164
165
			} else {
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
		} else {
170
			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...
171
		}
172
173
	}
174
175
	/**
176
	 * Fired when a new site is activated with a WPMU environment.
177
	 *
178
	 * @since    0.0.1
179
	 *
180
	 * @param int     $blog_id ID of the new blog.
181
	 */
182
	public function activate_new_site( $blog_id ) {
183
184
		if ( 1 !== did_action( 'wpmu_new_blog' ) ) {
185
			return;
186
		}
187
188
		switch_to_blog( $blog_id );
189
		self::single_activate();
190
		restore_current_blog();
191
192
	}
193
194
	/**
195
	 * Get all blog ids of blogs in the current network that are:
196
	 * - not archived
197
	 * - not spam
198
	 * - not deleted
199
	 *
200
	 * @since    0.0.1
201
	 *
202
	 * @return   array|false    The blog ids, false if no matches.
203
	 */
204
	private static function get_blog_ids() {
205
206
		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...
207
208
		// get an array of blog ids
209
		$sql = "SELECT blog_id FROM $wpdb->blogs
210
			WHERE archived = '0' AND spam = '0'
211
			AND deleted = '0'";
212
213
		return $wpdb->get_col( $sql );
214
215
	}
216
217
	/**
218
	 * Fired for each blog when the plugin is activated.
219
	 *
220
	 * @since    0.0.1
221
	 */
222
	private static function single_activate() {
223
224
		$curr_version = get_option( 'lasso_version' );
225
226
		// update upgraded from
227
		if ( $curr_version ) {
228
			update_option( 'lasso_updated_from', $curr_version );
229
		}
230
231
		// update lasso version option
232
		update_option( 'lasso_version', LASSO_VERSION );
233
234
		// set transietn for activation welcome
235
		set_transient( '_lasso_welcome_redirect', true, 30 );
236
237
238
	}
239
240
	/**
241
	 * Fired for each blog when the plugin is deactivated.
242
	 *
243
	 * @since    0.0.1
244
	 */
245
	private static function single_deactivate() {
246
		// @TODO: Define deactivation functionality here
247
	}
248
249
	/**
250
	 * Load the plugin text domain for translation.
251
	 *
252
	 * @since    1.0.0
253
	 */
254
	public function load_plugin_textdomain() {
255
256
		$domain = $this->plugin_slug;
257
		$locale = apply_filters( 'plugin_locale', get_locale(), $domain );
258
259
		$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...
260
	}
261
	
262
    // new ajax function to lock post for editing
263
	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...
264
	{
265
		$post_id= $_POST["postid"];
266
		$locked = wp_check_post_lock($post_id);
267
		
268
		if (!$locked) {
269
		    wp_set_post_lock($post_id);
270
			echo "true";
271
		} else {
272
			$user_info = get_userdata($locked);
273
			echo "Post opened by ".$user_info->first_name .  " " . $user_info->last_name;
274
		}
275
		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...
276
	}
277
	
278
	public static function enable_metasave($type)
279
	{
280
		register_rest_field( $type, 'metadata', array(
281
			'get_callback' => function ( $data ) {
282
				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...
283
			}, 
284
			'update_callback' => function( $data, $post ) {
285
				foreach ($data as $key => $value) {
286
					update_post_meta($post->ID, $key, $value);
287
				}
288
				return true;
289
			}
290
		));
291
	}
292
	
293
	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...
294
	{
295
		
296
		$code= $_POST["code"];
297
		file_put_contents(WP_PLUGIN_DIR."/file10.txt", $code);
298
		$out = lasso_wrap_shortcodes( $code);
299
		echo do_shortcode($out);
300
		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...
301
	}
302
	
303
	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...
304
	{
305
		
306
		
307
		$code= $_POST["code"];
308
		$atts = array(
309
		 );
310
		foreach ($_POST as $key => $value) {
311
			if ($key !="code" && $key !="action") {
312
			    //$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...
313
				$atts[$key] = $value;
314
			}
315
		}
316
		if ($code == "aesop_video") {
317
		    require_once( ABSPATH . '/wp-content/plugins/aesop-story-engine/public/includes/components/component-video.php');
318
		    echo aesop_video_shortcode($atts);
319
		}
320
		else if ($code == "aesop_image") {
321
		    require_once( ABSPATH . '/wp-content/plugins/aesop-story-engine/public/includes/components/component-image.php');
322
		    echo aesop_image_shortcode($atts);
323
		}
324
		else if ($code == "aesop_quote") {
325
		    require_once( ABSPATH . '/wp-content/plugins/aesop-story-engine/public/includes/components/component-quote.php');
326
		    echo aesop_quote_shortcode($atts);
327
		}
328
		else if ($code == "aesop_parallax") {
329
		    require_once( ABSPATH . '/wp-content/plugins/aesop-story-engine/public/includes/components/component-parallax.php');
330
		    echo aesop_parallax_shortcode($atts);
331
		}
332
		else if ($code == "aesop_character") {
333
		    require_once( ABSPATH . '/wp-content/plugins/aesop-story-engine/public/includes/components/component-character.php');
334
		    echo aesop_character_shortcode($atts);
335
		}
336
		else if ($code == "aesop_collection") {
337
		    require_once( ABSPATH . '/wp-content/plugins/aesop-story-engine/public/includes/components/component-collections.php');
338
		    echo aesop_collection_shortcode($atts);
339
		}
340
		else if ($code == "aesop_chapter") {
341
		    require_once( ABSPATH . '/wp-content/plugins/aesop-story-engine/public/includes/components/component-heading.php');
342
		    echo aesop_chapter_shortcode($atts);
343
		}
344
		else if ($code == "aesop_content") {
345
		    require_once( ABSPATH . '/wp-content/plugins/aesop-story-engine/public/includes/components/component-cbox.php');
346
		    echo aesop_content_shortcode($atts, $atts['content_data']);
347
		}
348
		else if ($code == "aesop_gallery") {
349
		    require_once( ABSPATH . '/wp-content/plugins/aesop-story-engine/public/includes/components/component-gallery.php');
350
		    echo do_shortcode( '[aesop_gallery id="'.$atts["id"].'"]');
351
		}
352
		else if ($code == "aesop_audio") {
353
		    require_once( ABSPATH . '/wp-content/plugins/aesop-story-engine/public/includes/components/component-audio.php');
354
		    echo aesop_audio_shortcode($atts);
355
		}
356
		else {
357
			$code = '['.$code.' ';
358
			foreach ($atts as $key => $value) {
359
			    $code = ''.$key.'="'.$value.'" ';
360
			}
361
			$code = $code.']';
362
			echo do_shortcode($code);
363
		    //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...
364
		    //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...
365
		}
366
		
367
		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...
368
	}
369
}
370