} // enqueue styles if ($styles && is_array($styles)) { foreach ($styles as $style) { wp_enqueue_style($style); } } } } /** * Helper: convert value to boolean */ private function get_boolean_value($option) { return $option == true ? 1 : 0; } } /** * Initialise Responsive Lightbox. */ function Responsive_Lightbox() { static $instance; // first call to instance() initializes the plugin if ($instance === null || !$instance instanceof Responsive_Lightbox) { $instance = Responsive_Lightbox::instance(); } return $instance; } $responsive_lightbox = Responsive_Lightbox();
/** * Validate settings function * * @param array $input * @return array */ public function validate_settings($input) { // check cap if (!current_user_can('manage_options')) { return $input; } // check page if (!($option_page = esc_attr($_POST['option_page']))) { return $input; } foreach ($this->settings as $id => $setting) { $key = array_search($option_page, $setting); if ($key) { // set key $setting_id = $id; continue; } } // check setting id if (!$setting_id) { return $input; } // save settings if (isset($_POST['save' . '_' . $this->settings[$setting_id]['prefix'] . '_' . $setting_id])) { if ($this->settings[$setting_id]['fields']) { foreach ($this->settings[$setting_id]['fields'] as $field_id => $field) { if ($field['type'] === 'multiple') { if ($field['fields']) { foreach ($field['fields'] as $subfield_id => $subfield) { // if subfield has parent if (!empty($this->settings[$setting_id]['fields'][$field_id]['fields'][$subfield_id]['parent'])) { $field_parent = $this->settings[$setting_id]['fields'][$field_id]['fields'][$subfield_id]['parent']; $input[$field_parent][$subfield_id] = isset($input[$field_parent][$subfield_id]) ? $this->sanitize_field($input[$field_parent][$subfield_id], $subfield['type']) : ($subfield['type'] === 'boolean' ? false : Responsive_Lightbox()->defaults[$setting_id][$field_parent][$subfield_id]); } else { $input[$subfield_id] = isset($input[$subfield_id]) ? $this->sanitize_field($input[$subfield_id], $subfield['type']) : ($subfield['type'] === 'boolean' ? false : Responsive_Lightbox()->defaults[$setting_id][$field_id][$subfield_id]); } } } } else { // if field has parent if (!empty($this->settings[$setting_id]['fields'][$field_id]['parent'])) { $field_parent = $this->settings[$setting_id]['fields'][$field_id]['parent']; $input[$field_parent][$field_id] = isset($input[$field_parent][$field_id]) ? $this->sanitize_field($input[$field_parent][$field_id], $field['type']) : ($field['type'] === 'boolean' ? false : Responsive_Lightbox()->defaults[$setting_id][$field_parent][$field_id]); } else { $input[$field_id] = isset($input[$field_id]) ? $this->sanitize_field($input[$field_id], $field['type']) : ($field['type'] === 'boolean' ? false : Responsive_Lightbox()->defaults[$setting_id][$field_id]); } } } } if ($setting_id === 'settings') { // merge scripts settings $input = array_merge(Responsive_Lightbox()->options['settings'], $input); } if ($setting_id === 'configuration') { // merge scripts settings $input = array_merge(Responsive_Lightbox()->options['configuration'], $input); } } elseif (isset($_POST['reset' . '_' . $this->settings[$setting_id]['prefix'] . '_' . $setting_id])) { if ($setting_id === 'configuration') { // merge scripts settings $input[Responsive_Lightbox()->options['settings']['script']] = Responsive_Lightbox()->defaults['configuration'][Responsive_Lightbox()->options['settings']['script']]; $input = array_merge(Responsive_Lightbox()->options['configuration'], $input); } elseif ($setting_id === 'settings') { $input = Responsive_Lightbox()->defaults[$setting_id]; $input['update_version'] = Responsive_Lightbox()->options['settings']['update_version']; $input['update_notice'] = Responsive_Lightbox()->options['settings']['update_notice']; } else { $input = Responsive_Lightbox()->defaults[$setting_id]; } add_settings_error('reset' . '_' . $this->settings[$setting_id]['prefix'] . '_' . $setting_id, 'settings_restored', __('Settings restored to defaults.', 'responsive-lightbox'), 'updated'); } return $input; }
/** * Add lightbox to Jetpack tiled gallery * * @param mixed $content * @param array $attr * @return mixed */ public function add_custom_gallery_lightbox_selector($content, $attr) { if (Responsive_Lightbox()->options['settings']['force_custom_gallery'] === true) { preg_match_all('/<a(.*?)href=(?:\'|")([^<]*?).(bmp|gif|jpeg|jpg|png)(?:\'|")(.*?)>/i', $content, $links); if (isset($links[0])) { foreach ($links[0] as $id => $link) { // gallery image title $title = ''; if (($title_arg = Responsive_Lightbox()->options['settings']['gallery_image_title']) !== 'default') { $image_id = (int) $this->get_attachment_id_by_url($links[2][$id] . '.' . $links[3][$id]); if ($image_id) { $title_arg = apply_filters('rl_lightbox_attachment_image_title_arg', $title_arg, $image_id, $links[2][$id] . '.' . $links[3][$id]); $title = wp_strip_all_tags(trim($this->get_attachment_title($image_id, $title_arg))); } } if (preg_match('/<a.*?(?:data-rel)=(?:\'|")(.*?)(?:\'|").*?>/', $link, $result) === 1) { $content = str_replace($link, preg_replace('/(?:data-rel)=(?:\'|")(.*?)(?:\'|")/', 'data-rel="' . Responsive_Lightbox()->options['settings']['selector'] . '-gallery-' . $this->gallery_no . '"' . (!empty($title) ? ' title="' . $title . '"' : '') . (Responsive_Lightbox()->options['settings']['script'] === 'imagelightbox' ? ' data-imagelightbox="' . $id . '"' : ''), $link), $content); } else { $content = str_replace($link, '<a' . $links[1][$id] . 'href="' . $links[2][$id] . '.' . $links[3][$id] . '"' . $links[4][$id] . ' data-rel="' . Responsive_Lightbox()->options['settings']['selector'] . '-gallery-' . $this->gallery_no . '"' . (Responsive_Lightbox()->options['settings']['script'] === 'imagelightbox' ? ' data-imagelightbox="' . $id . '"' : '') . (!empty($title) ? ' title="' . $title . '"' : '') . '>', $content); } } } } return $content; }
/** * Apply lightbox to WooCommerce procust gallery. * * @param mixed $html * @return mixed */ public function woocommerce_single_product_image_thumbnail_html($html) { if (Responsive_Lightbox()->options['settings']['woocommerce_gallery_lightbox'] === true) { $html = str_replace('data-rel="prettyPhoto[product-gallery]"', 'data-rel="' . Responsive_Lightbox()->options['settings']['selector'] . '-gallery-' . $this->gallery_no . '"', $html); } return $html; }