Completed
Push — master ( 72613d...069c91 )
by Michael
02:16
created

MetaSlide::slide_exists_in_slideshow()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Slide class represting a single slide. This is extended by type specific
5
 * slides (eg, MetaImageSlide, MetaYoutubeSlide (pro only), etc)
6
 */
7
class MetaSlide
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
8
{
9
    public $slide    = 0;
10
    public $slider   = 0;
11
    public $settings = array(); // slideshow settings
12
13
    /**
14
     * Set the slide
15
     * @param $id
16
     */
17
    public function set_slide($id)
18
    {
19
        $this->slide = get_post($id);
20
    }
21
22
    /**
23
     * Set the slide (that this slide belongs to)
24
     * @param $id
25
     */
26
    public function set_slider($id)
27
    {
28
        $this->slider   = get_post($id);
29
        $this->settings = get_post_meta($id, 'ml-slider_settings', true);
30
    }
31
32
    /**
33
     * Return the HTML for the slide
34
     *
35
     * @param $slide_id
36
     * @param $slider_id
37
     * @return array complete array of slides
0 ignored issues
show
Documentation introduced by
Should the return type not be string?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
38
     */
39
    public function get_slide($slide_id, $slider_id)
40
    {
41
        $this->set_slider($slider_id);
42
        $this->set_slide($slide_id);
43
44
        return $this->get_slide_html();
45
    }
46
47
    /**
48
     * Save the slide
49
     * @param $slide_id
50
     * @param $slider_id
51
     * @param $fields
52
     */
53
    public function save_slide($slide_id, $slider_id, $fields)
54
    {
55
        $this->set_slider($slider_id);
56
        $this->set_slide($slide_id);
57
        $this->save($fields);
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class MetaSlide as the method save() does only exist in the following sub-classes of MetaSlide: MetaImageSlide. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
58
    }
59
60
    /**
61
     * Return the correct slide HTML based on whether we're viewing the slides in the
62
     * admin panel or on the front end.
63
     *
64
     * @return string slide html
65
     */
66
    public function get_slide_html()
0 ignored issues
show
Coding Style introduced by
get_slide_html uses the super-global variable $_GET 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...
67
    {
68
        if (is_admin() && isset($_GET['page']) && 'metaslider-theme-editor' === $_GET['page']) {
69
            return $this->get_public_slide();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class MetaSlide as the method get_public_slide() does only exist in the following sub-classes of MetaSlide: MetaImageSlide. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
70
        }
71
72
        if (is_admin() && !isset($_GET['slider_id'])) {
73
            return $this->get_admin_slide();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class MetaSlide as the method get_admin_slide() does only exist in the following sub-classes of MetaSlide: MetaImageSlide. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
74
        }
75
76
        return $this->get_public_slide();
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class MetaSlide as the method get_public_slide() does only exist in the following sub-classes of MetaSlide: MetaImageSlide. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
77
    }
78
79
    /**
80
     * @param $slider_id
81
     * @param $slide_id
82
     * @return
83
     */
84
    public function slide_exists_in_slideshow($slider_id, $slide_id)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
85
    {
86
        return has_term("{$slider_id}", 'ml-slider', $slide_id);
87
    }
88
89
    /**
90
     * @param $slider_id
91
     * @param $slide_id
92
     * @return bool
93
     */
94
    public function slide_is_unassigned_or_image_slide($slider_id, $slide_id)
0 ignored issues
show
Unused Code introduced by
The parameter $slider_id 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...
95
    {
96
        $type = get_post_meta($slide_id, 'ml-slider_type', true);
97
98
        return !strlen($type) || 'image' === $type;
99
    }
100
101
    /**
102
     * Build image HTML
103
     *
104
     * @param  array $attributes
105
     * @return string image HTML
106
     */
107 View Code Duplication
    public function build_image_tag($attributes)
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...
108
    {
109
        $html = '<img';
110
111
        foreach ($attributes as $att => $val) {
112
            if (strlen($val)) {
113
                $html .= ' ' . $att . '="' . $val . '"';
114
            }
115
        }
116
117
        $html .= ' >';
118
119
        return $html;
120
    }
121
122
    /**
123
     * Build image HTML
124
     *
125
     * @param  array $attributes
126
     * @param        $content
127
     * @return string image HTML
128
     */
129 View Code Duplication
    public function build_anchor_tag($attributes, $content)
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...
130
    {
131
        $html = '<a';
132
133
        foreach ($attributes as $att => $val) {
134
            if (strlen($val)) {
135
                $html .= ' ' . $att . '="' . $val . '"';
136
            }
137
        }
138
139
        $html .= '>' . $content . '</a>';
140
141
        return $html;
142
    }
143
144
    /**
145
     * Tag the slide attachment to the slider tax category
146
     */
147
    public function tag_slide_to_slider()
148
    {
149
        if (!term_exists($this->slider->ID, 'ml-slider')) {
150
            // create the taxonomy term, the term is the ID of the slider itself
151
            wp_insert_term($this->slider->ID, 'ml-slider');
152
        }
153
154
        // get the term thats name is the same as the ID of the slider
155
        $term = get_term_by('name', $this->slider->ID, 'ml-slider');
156
        // tag this slide to the taxonomy term
157
        wp_set_post_terms($this->slide->ID, $term->term_id, 'ml-slider', true);
158
159
        $this->update_menu_order();
160
    }
161
162
    /**
163
     * Ensure slides are added to the slideshow in the correct order.
164
     *
165
     * Find the highest slide menu_order in the slideshow, increment, then
166
     * update the new slides menu_order.
167
     */
168
    public function update_menu_order()
169
    {
170
        $menu_order = 0;
171
172
        // get the slide with the highest menu_order so far
173
        $args = array(
174
            'force_no_custom_order' => true,
175
            'orderby'               => 'menu_order',
176
            'order'                 => 'DESC',
177
            'post_type'             => 'attachment',
178
            'post_status'           => 'inherit',
179
            'lang'                  => '', // polylang, ingore language filter
180
            'suppress_filters'      => 1, // wpml, ignore language filter
181
            'posts_per_page'        => 1,
182
            'tax_query'             => array(
183
                array(
184
                    'taxonomy' => 'ml-slider',
185
                    'field'    => 'slug',
186
                    'terms'    => $this->slider->ID
187
                )
188
            )
189
        );
190
191
        $query = new WP_Query($args);
192
193
        while ($query->have_posts()) {
194
            $query->next_post();
195
            $menu_order = $query->post->menu_order;
196
        }
197
198
        wp_reset_query();
199
200
        // increment
201
        +$menu_order;
202
203
        // update the slide
204
        wp_update_post(array(
205
                           'ID'         => $this->slide->ID,
206
                           'menu_order' => $menu_order
207
                       ));
208
    }
209
210
    /**
211
     * If the meta doesn't exist, add it
212
     * If the meta exists, but the value is empty, delete it
213
     * If the meta exists, update it
214
     * @param $post_id
215
     * @param $name
216
     * @param $value
217
     */
218
    public function add_or_update_or_delete_meta($post_id, $name, $value)
219
    {
220
        $key = 'ml-slider_' . $name;
221
222
        if ('false' === $value || '' === $value || !$value) {
223
            if (get_post_meta($post_id, $key)) {
224
                delete_post_meta($post_id, $key);
225
            }
226
        } else {
227
            if (get_post_meta($post_id, $key)) {
228
                update_post_meta($post_id, $key, $value);
229
            } else {
230
                add_post_meta($post_id, $key, $value, true);
231
            }
232
        }
233
    }
234
235
    /**
236
     * Get the thumbnail for the slide
237
     */
238
    public function get_thumb()
239
    {
240
        $imageHelper = new MetaSliderImageHelper($this->slide->ID, 150, 150, 'false');
241
242
        return $imageHelper->get_image_url();
243
    }
244
}
245