Completed
Push — master ( 516a8e...498f6d )
by
unknown
02:18
created

lasso::get_aesop_component()   C

Complexity

Conditions 8
Paths 48

Size

Total Lines 39
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

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