| 1 |
<?php |
| 2 |
|
| 3 |
namespace Photonic_Plugin\Add_Ons\WP; |
| 4 |
|
| 5 |
use Photonic_Plugin\Core\Photonic; |
| 6 |
use WP_Widget; |
| 7 |
|
| 8 |
class Widget extends WP_Widget { |
| 9 |
private string $empty_shortcode; |
| 10 |
public string $invalid_shortcode; |
| 11 |
public string $edit_shortcode; |
| 12 |
|
| 13 |
public function __construct() { |
| 14 |
$widget_ops = [ |
| 15 |
'classname' => 'widget-photonic', |
| 16 |
'description' => __("A widget for displaying a Photonic Gallery.", 'photonic') |
| 17 |
]; |
| 18 |
|
| 19 |
$control_ops = []; |
| 20 |
$this->empty_shortcode = esc_html__('Click on the icon to start creating a new gallery.', 'photonic'); |
| 21 |
$this->invalid_shortcode = esc_html__('The current saved data does not correspond to a Photonic gallery. A new one will be created.', 'photonic'); |
| 22 |
$this->edit_shortcode = esc_html__('Click on the icon to edit your gallery.', 'photonic'); |
| 23 |
|
| 24 |
parent::__construct("photonic-widget", __("Photonic Gallery", 'photonic'), $widget_ops, $control_ops); |
| 25 |
} |
| 26 |
|
| 27 |
public function widget($args, $instance) { |
| 28 |
$title = empty($instance['title']) ? '' : $instance['title']; |
| 29 |
$shortcode = empty($instance['shortcode']) ? '' : $instance['shortcode']; |
| 30 |
|
| 31 |
echo wp_kses_post($args['before_widget']); |
| 32 |
if ('' !== $title) { |
| 33 |
echo wp_kses_post($args['before_title'] . $title . $args['after_title']); |
| 34 |
} |
| 35 |
|
| 36 |
// Input is coming via content saved in a widget, so we have to ensure it is safe. |
| 37 |
// The input is expected to be a Photonic shortcode, so the simplest way is to strip out all instances of the Photonic |
| 38 |
// shortcode and verify that the input is blank. If it is blank, then all that the input had was a Photonic shortcode. |
| 39 |
|
| 40 |
global $photonic_alternative_shortcode; |
| 41 |
$content_without_shortcodes = strip_shortcodes($shortcode); |
| 42 |
$shortcode_tag = esc_attr($photonic_alternative_shortcode ?: 'gallery'); |
| 43 |
|
| 44 |
if (!empty(trim($shortcode)) && has_shortcode($shortcode, $shortcode_tag) && empty(trim($content_without_shortcodes))) { |
| 45 |
// Looks good. Let's proceed. |
| 46 |
$output = do_shortcode($shortcode); |
| 47 |
echo wp_kses($output, Photonic::$safe_tags); |
| 48 |
} |
| 49 |
|
| 50 |
echo wp_kses_post($args['after_widget']); |
| 51 |
} |
| 52 |
|
| 53 |
public function update($new_instance, $old_instance) { |
| 54 |
$instance = $old_instance; |
| 55 |
$instance['title'] = sanitize_text_field($new_instance['title']); |
| 56 |
$instance['shortcode'] = sanitize_text_field($new_instance['shortcode']); |
| 57 |
return $instance; |
| 58 |
} |
| 59 |
|
| 60 |
public function form($instance) { |
| 61 |
global $photonic_alternative_shortcode; |
| 62 |
$tag = $photonic_alternative_shortcode ?: 'gallery'; |
| 63 |
|
| 64 |
$defaults = [ |
| 65 |
'title' => '', |
| 66 |
'custom_class' => '', |
| 67 |
'shortcode' => '' |
| 68 |
]; |
| 69 |
$instance = wp_parse_args( |
| 70 |
(array) $instance, |
| 71 |
$defaults |
| 72 |
); |
| 73 |
|
| 74 |
add_thickbox(); |
| 75 |
$user = get_current_user_id(); |
| 76 |
if (0 === $user) { |
| 77 |
$user = wp_rand(1); |
| 78 |
} |
| 79 |
|
| 80 |
$url = add_query_arg( |
| 81 |
[ |
| 82 |
'action' => 'photonic_wizard', |
| 83 |
'class' => 'photonic-flow', |
| 84 |
'post_id' => '', |
| 85 |
'nonce' => wp_create_nonce('photonic-wizard-' . $user), |
| 86 |
'width' => '1000', |
| 87 |
'height' => '600', |
| 88 |
'TB_iframe' => 'true', |
| 89 |
], |
| 90 |
admin_url('admin.php') |
| 91 |
); |
| 92 |
|
| 93 |
$shortcode = $instance['shortcode']; |
| 94 |
$types = ['default', 'wp', 'flickr', 'smugmug', 'picasa', 'google', 'zenfolio', 'instagram']; |
| 95 |
$layouts = ['square', 'circle', 'random', 'masonry', 'mosaic', 'strip-above', 'strip-below', 'strip-right', 'no-strip']; |
| 96 |
|
| 97 |
$pattern = get_shortcode_regex([$tag]); |
| 98 |
preg_match_all('/' . $pattern . '/s', $shortcode, $matches, PREG_OFFSET_CAPTURE); |
| 99 |
$type = 'photonic'; |
| 100 |
|
| 101 |
$message = $this->edit_shortcode; |
| 102 |
if (empty($shortcode)) { |
| 103 |
$message = $this->empty_shortcode; |
| 104 |
} |
| 105 |
elseif (!empty($matches) && !empty($matches[0]) && !empty($matches[1]) && !empty($matches[2]) && !empty($matches[3])) { |
| 106 |
foreach ($matches[1] as $index => $start) { |
| 107 |
if ('' === $start[0]) { |
| 108 |
if (!empty($matches[3][$index])) { |
| 109 |
$shortcode_attr = shortcode_parse_atts($matches[3][$index][0]); |
| 110 |
if (!empty($shortcode_attr['type']) && in_array($shortcode_attr['type'], $types, true)) { |
| 111 |
$type = $shortcode_attr['type']; |
| 112 |
} |
| 113 |
elseif (empty($shortcode_attr['type']) && !empty($shortcode_attr['style']) && in_array($shortcode_attr['style'], $layouts, true)) { |
| 114 |
$type = 'wp'; |
| 115 |
} |
| 116 |
else { |
| 117 |
$message = $this->invalid_shortcode; |
| 118 |
$shortcode = ''; |
| 119 |
} |
| 120 |
} |
| 121 |
} |
| 122 |
} |
| 123 |
} |
| 124 |
else { |
| 125 |
$message = $this->invalid_shortcode; |
| 126 |
$shortcode = ''; |
| 127 |
} |
| 128 |
?> |
| 129 |
<div class="photonic-widget"> |
| 130 |
<p> |
| 131 |
<label for="<?php echo esc_attr($this->get_field_id('title')); ?>"><?php esc_attr_e('Title', 'photonic'); ?></label> |
| 132 |
<input id="<?php echo esc_attr($this->get_field_id('title')); ?>" value="<?php echo esc_attr($instance['title']); ?>" |
| 133 |
name="<?php echo esc_attr($this->get_field_name('title')); ?>" type="text" class="widefat"/> |
| 134 |
</p> |
| 135 |
|
| 136 |
<input id="<?php echo esc_attr($this->get_field_id('shortcode')); ?>" value="<?php echo esc_attr($shortcode); ?>" type="hidden" |
| 137 |
name="<?php echo esc_attr($this->get_field_name('shortcode')); ?>" class="photonic-shortcode"/> |
| 138 |
|
| 139 |
<div class="photonic-source"> |
| 140 |
<a class="photonic-wizard <?php echo esc_attr($type); ?>" href="<?php echo esc_url($url); ?>"></a> |
| 141 |
<p> |
| 142 |
<?php echo wp_kses_post($message); ?> |
| 143 |
</p> |
| 144 |
</div> |
| 145 |
|
| 146 |
<div class="photonic-shortcode-display"> |
| 147 |
<?php |
| 148 |
if ('' !== $shortcode) { |
| 149 |
?> |
| 150 |
<h4><?php echo esc_html__('Current shortcode', 'photonic'); ?></h4> |
| 151 |
<code><?php echo wp_kses_post($shortcode); ?></code> |
| 152 |
<?php |
| 153 |
} |
| 154 |
?> |
| 155 |
</div> |
| 156 |
</div> |
| 157 |
<?php |
| 158 |
} |
| 159 |
|
| 160 |
} |
| 161 |
|