Completed
Push — v1.5 ( cd46e3...80949e )
by Maxim
15:19 queued 07:45
created

Bootstrap::log()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 4
nop 0
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * WP-AMD Plugin.
4
 *
5
 */
6
namespace UsabilityDynamics\AMD {
7
8
  use UsabilityDynamics\Settings;
9
10
  if( !class_exists( 'UsabilityDynamics\AMD\Bootstrap' ) ) {
11
12
    class Bootstrap {
13
14
      /**
15
       * Cluster core version.
16
       *
17
       * @static
18
       * @property $version
19
       * @type {Object}
20
       */
21
      public $version = false;
22
23
      /**
24
       * Textdomain String
25
       *
26
       * @public
27
       * @property domain
28
       * @var string
29
       */
30
      public $domain = false;
31
32
      /**
33
       * Singleton Instance Reference.
34
       *
35
       * @public
36
       * @static
37
       * @property $instance
38
       * @type {Object}
39
       */
40
      public static $instance = null;
41
42
      public $option = array();
43
44
      /**
45
       * Instantaite class.
46
       */
47
      private function __construct() {
48
        
49
        $plugin_data = get_file_data( ( dirname( __DIR__ ) . '/wp-amd.php' ), array(
50
          'Name' => 'Plugin Name',
51
          'Version' => 'Version',
52
          'TextDomain' => 'Text Domain',
53
        ), 'plugin' );
54
        
55
        $this->version  = trim( $plugin_data[ 'Version' ] );
0 ignored issues
show
Documentation Bug introduced by
The property $version was declared of type boolean, but trim($plugin_data['Version']) is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
56
        $this->domain   = trim( $plugin_data[ 'TextDomain' ] );
57
58
        // Initialize Settings.
59
        $this->settings = new Settings( array(
0 ignored issues
show
Bug introduced by
The property settings does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
Documentation introduced by
array('key' => 'wp_amd',....min.css')), $this))))) is of type array<string,string|arra...\\\\\\\"}>\\\"}>\"}>"}>, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
60
          'key'  => 'wp_amd',
61
          'data' => array(
62
            'version' => $this->version,
63
            'domain' => $this->domain,
64
            'assets' => array(
65
              'script' => array(
66
                'type'          => 'script',
67
                'disk_cache'    => apply_filters( 'wp-amd:script:disk_cache', false, $this ),
68
                'extension'     => 'js',
69
                'minify'        => false,
70
                'admin_menu'    => true,
71
                'load_in_head'  => false,
72
                'permalink'     => apply_filters( 'wp-amd:script:path', 'assets/wp-amd.js', $this ),
73
                'dependencies'  => apply_filters( 'wp-amd:script:dependencies', array(
74
                  'jquery'  => array(
75
                    'id'            => 'jquery',
76
                    'name'          => 'jQuery',
77
                    'infourl'       => 'http://jquery.com',
78
                    'version'       => '2.1.0',
79
                    'url'           => '//ajax.aspnetcdn.com/ajax/jquery/jquery-2.1.0.min.js',
80
                  ),
81
                  'backbone'  => array(
82
                    'id'            => 'backbone',
83
                    'name'          => 'Backbone.js',
84
                    'infourl'       => 'http://backbonejs.com',
85
                    'url'           => 'http://backbonejs.org/backbone-min.js',
86
                  ),
87
                  'lodash' => array(
88
                    'id'            => 'lodash',
89
                    'name'          => 'Lodash',
90
                    'infourl'       => 'http://lodash.com',
91
                    'version'       => '2.4.1',
92
                    'url'           => 'http://cdnjs.cloudflare.com/ajax/libs/lodash.js/2.4.1/lodash.js',
93
                  ),
94
                  'knockout' => array(
95
                    'id'            => 'knockout',
96
                    'name'          => 'Knockout.js',
97
                    'version'       => '2.2.1',
98
                    'infourl'       => 'http://knockoutjs.com',
99
                    'url'           => 'http://ajax.aspnetcdn.com/ajax/knockout/knockout-2.2.1.js',
100
                  ),
101
                  'udx-requires' => array(
102
                    'id'            => 'udx-requires',
103
                    'name'          => 'Requires.js (UDX)',
104
                    'infourl'       => 'http://cdn.udx.io',
105
                    'version'       => '4.0.0',
106
                    'url'           => '//cdn.udx.io/udx.requires.js',
107
                  ),
108
                  'twitter-bootstrap' => array(
109
                    'id'            => 'twitter-bootstrap',
110
                    'name'          => 'Twitter Bootstrap',
111
                    'infourl'       => 'http://getbootstrap.com/',
112
                    'version'       => '3.1.1',
113
                    'url'           => '//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js'
114
                  ),
115
                  'google-analytics' => array(
116
                    'id'            => 'google-analytics',
117
                    'name'          => 'Google Analytics',
118
                    'infourl'       => 'http://google-analytics.com/',
119
                    'version'       => '3.1.1',
120
                    'url'           => '//www.google-analytics.com/urchin.js'
121
                  )
122
                ), $this ),
123
              ),
124
              'style'  => array(
125
                'type'          => 'style',
126
                'minify'        => false,
127
                'disk_cache'    => apply_filters( 'wp-amd:script:disk_cache', false, $this ),
128
                'permalink'     => apply_filters( 'wp-amd:style:path', 'assets/wp-amd.css', $this ),
129
                'extension'     => 'css',
130
                'admin_menu'    => true,
131
                'load_in_head'  => true,
132
                'dependencies'  => apply_filters( 'wp-amd:style:dependencies', array(
133
                  'twitter-bootstrap' => array(
134
                    'id'            => 'twitter-bootstrap',
135
                    'name'          => 'Twitter Bootstrap',
136
                    'version'       => '3.1.1',
137
                    'infourl'       => 'http://getbootstrap.com/',
138
                    'url'           => '//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css'
139
                  ),
140
                  'normalize' => array(
141
                    'id'            => 'normalize',
142
                    'name'          => 'Normalize',
143
                    'version'       => '3.0.1',
144
                    'infourl'       => 'http://getbootstrap.com/',
145
                    'url'           => '//cdnjs.cloudflare.com/ajax/libs/normalize/3.0.1/normalize.min.css'
146
                  ),
147
                  'fontawesome' => array(
148
                    'id'            => 'fontawesome',
149
                    'name'          => 'Font Awesome',
150
                    'infourl'       => 'http://fontawesome.io/',
151
                    'url'           => '//cdnjs.cloudflare.com/ajax/libs/font-awesome/4.0.3/css/font-awesome.min.css'
152
                  ),
153
                ), $this ),
154
              )
155
            )
156
          )
157
        ));
158
159
        if( !class_exists( 'ChromePhp' ) ) {
160
          include (__DIR__ . '/class-chrome-php.php');
161
        }
162
163
        // Set Dynamics.
164
        $this->set( 'version',  $this->version );
165
        $this->set( 'locale',   $this->domain );
166
167
        // Initialize Style and Script classes.
168
        $this->style    = new Style( $this->get( 'assets.style' ), $this );
0 ignored issues
show
Bug introduced by
The property style does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
169
        $this->script   = new Script( $this->get( 'assets.script' ), $this );
0 ignored issues
show
Bug introduced by
The property script does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
170
171
        // ChromePhp::log('wp-amd: bootstrapping.'.$_SERVER['REQUEST_URI']);
172
        // ChromePhp::log($_SERVER);
173
174
        if( file_exists( dirname( __DIR__ ) . '/lib/class-minit.php' ) ) {
175
          require_once( dirname( __DIR__ ) . '/lib/class-minit.php' );
176
        }
177
178
        // AJAX Update Handler.
179
        add_action( 'wp_ajax_/amd/v1/asset', array( $this, 'ajax_handler' ) );
180
181
        // Handle dynamic URL identification for plugin assets.
182
        add_filter( 'includes_url', array( $this, 'includes_url' ), 20, 2 );
183
        add_filter( 'plugins_url', array( $this, 'plugins_url' ), 20, 2 );
184
185
      }
186
187
      /**
188
       *
189
       */
190
      static public function log() {
191
192
        if( !class_exists( 'ChromePhp' ) ) {
193
          include (__DIR__ . '/class-chrome-php.php');
194
        }
195
196
        if( class_exists( 'UsabilityDynamics\AMD\ChromePhp' ) ) {
197
          call_user_func_array( array('UsabilityDynamics\AMD\ChromePhp', 'log' ), func_get_args() );
198
        }
199
200
      }
201
      
202
      /**
203
       * Modify includes_url() to find valid location for AMD includes assets.
204
       *
205
       * @since 1.1.1
206
       * @author potanin@UD
207
208
       * @param string $url
209
       * @param string $path
210
       *
211
       * @return string
212
       */
213
      public function plugins_url( $url = '', $path = '' ) {
0 ignored issues
show
Unused Code introduced by
The parameter $path is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
214
215
        return $url;
216
217
      }
218
219
      /**
220
       * Modify includes_url() to find valid location for AMD includes assets.
221
       *
222
       * @since 1.1.1
223
       * @author potanin@UD
224
       * @param string $url
225
       * @param string $path
226
       *
227
       * @return string
228
       */
229
      public function includes_url( $url = '', $path = '' ) {
0 ignored issues
show
Unused Code introduced by
The parameter $path is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
230
231
232
233
        return $url;
234
235
      }
236
237
      /**
238
       * Handle Administrative AJAX Actions
239
       *
240
       * @author potanin@UD
241
       * @method ajax_handler
242
       */
243
      public function ajax_handler() {
244
245
        $_data = $_POST[ 'data' ];
246
        $_type = $_POST[ 'type' ];
247
        $_dependency = isset( $_POST[ 'dependency' ] ) ? $_POST[ 'dependency' ] : array();
248
        if( !is_array( $_dependency ) ) {
249
          $_dependency = array();
250
        }
251
252
        if( !$_type || !method_exists( $this->{$_type}, 'save_asset' ) ) {
253
254
          wp_send_json(array(
255
            'ok' => false,
256
            'message' => __( 'Unexpected error occured.', $this->get( 'locale' ) )
257
          ));
258
259
        }
260
261
        if( !is_wp_error( $_revision = $this->{$_type}->save_asset( $_data ) ) ) {
262
263
          update_post_meta( $_revision, 'dependency', $_dependency );
264
265
          wp_send_json(array(
266
            'ok' => false,
267
            'revision' => $_revision,
268
            'dependency' => $_dependency,
269
            'message' => __( 'Asset saved successfully.', $this->get( 'locale' ) )
270
          ));
271
272
        }
273
274
        wp_send_json(array(
275
          'ok' => false,
276
          'message' => __( 'Unable to save asset.', $this->get( 'locale' ) )
277
        ));
278
279
      }
280
281
      /**
282
       * Determine if instance already exists and Return Theme Instance
283
       *
284
       */
285
      public static function get_instance( $args = array() ) {
0 ignored issues
show
Unused Code introduced by
The parameter $args is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
286
        return null === self::$instance ? self::$instance = new self() : self::$instance;
287
      }
288
289
      /**
290
       * @param null $key
291
       * @param null $value
292
       *
293
       * @return \UsabilityDynamics\Settings
294
       */
295
      public function set( $key = null, $value = null ) {
296
        return $this->settings->set( $key, $value );
297
      }
298
299
      /**
300
       * @param null $key
301
       * @param null $default
302
       *
303
       * @return \UsabilityDynamics\type
304
       */
305
      public function get( $key = null, $default = null ) {
306
        return $this->settings->get( $key, $default );
307
      }
308
309
    }
310
311
  }
312
313
}
314