Completed
Push — master ( 5052d3...d97084 )
by
unknown
02:19
created

lasso::single_activate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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