mambax7 /
extgallery
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 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 |
||
| 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 |
||
| 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); |
||
| 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
|
|||
| 67 | { |
||
| 68 | if (is_admin() && isset($_GET['page']) && 'metaslider-theme-editor' === $_GET['page']) { |
||
| 69 | return $this->get_public_slide(); |
||
| 70 | } |
||
| 71 | |||
| 72 | if (is_admin() && !isset($_GET['slider_id'])) { |
||
| 73 | return $this->get_admin_slide(); |
||
| 74 | } |
||
| 75 | |||
| 76 | return $this->get_public_slide(); |
||
| 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) |
||
| 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) |
||
| 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) |
|
| 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) |
|
| 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 |
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: