Completed
Push — master ( ded167...50d14d )
by
unknown
02:50
created

lasso::enable_metasave()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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