| 1 |
<?php |
| 2 |
namespace Photonic_Plugin\Add_Ons\WP; |
| 3 |
|
| 4 |
use WP_Widget; |
| 5 |
|
| 6 |
class Widget extends WP_Widget { |
| 7 |
private $empty_shortcode, $invalid_shortcode, $edit_shortcode; |
| 8 |
function __construct() { |
| 9 |
$widget_ops = ['classname' => 'widget-photonic', |
| 10 |
'description' => __("A widget for displaying a Photonic Gallery.", 'photonic') |
| 11 |
]; |
| 12 |
|
| 13 |
$control_ops = []; |
| 14 |
$this->empty_shortcode = esc_html__('Click on the icon to start creating a new gallery.', 'photonic'); |
| 15 |
$this->invalid_shortcode = esc_html__('The current saved data does not correspond to a Photonic gallery. A new one will be created.', 'photonic'); |
| 16 |
$this->edit_shortcode = esc_html__('Click on the icon to edit your gallery.', 'photonic'); |
| 17 |
|
| 18 |
parent::__construct("photonic-widget", __("Photonic Gallery", 'photonic'), $widget_ops, $control_ops); |
| 19 |
} |
| 20 |
|
| 21 |
function widget($args, $instance) { |
| 22 |
extract($args); |
| 23 |
|
| 24 |
$title = empty($instance['title']) ? '' : $instance['title']; |
| 25 |
$shortcode = empty($instance['shortcode']) ? '' : $instance['shortcode']; |
| 26 |
|
| 27 |
echo $before_widget; |
| 28 |
if ($title != '') { |
| 29 |
echo $before_title.$title.$after_title; |
| 30 |
} |
| 31 |
|
| 32 |
$output = do_shortcode($shortcode); |
| 33 |
echo $output; |
| 34 |
|
| 35 |
echo $after_widget; |
| 36 |
} |
| 37 |
|
| 38 |
function update($new_instance, $old_instance) { |
| 39 |
$instance = $old_instance; |
| 40 |
$instance['title'] = esc_attr($new_instance['title']); |
| 41 |
$instance['shortcode'] = $new_instance['shortcode']; |
| 42 |
return $instance; |
| 43 |
} |
| 44 |
|
| 45 |
function form($instance) { |
| 46 |
global $photonic_alternative_shortcode; |
| 47 |
$tag = empty($photonic_alternative_shortcode) ? 'gallery' : $photonic_alternative_shortcode; |
| 48 |
|
| 49 |
$defaults = [ |
| 50 |
'title' => '', |
| 51 |
'custom_class' => '', |
| 52 |
'shortcode' => '' |
| 53 |
]; |
| 54 |
$instance = wp_parse_args((array)$instance, $defaults); |
| 55 |
|
| 56 |
add_thickbox(); |
| 57 |
$url = add_query_arg([ |
| 58 |
'action' => 'photonic_wizard', |
| 59 |
'class' => 'photonic-flow', |
| 60 |
'post_id' => '', |
| 61 |
'width' => '1000', |
| 62 |
'height' => '600', |
| 63 |
'TB_iframe' => 'true', |
| 64 |
], admin_url( 'admin.php' ) ); |
| 65 |
|
| 66 |
$shortcode = $instance['shortcode']; |
| 67 |
$types = ['default', 'wp', 'flickr', 'smugmug', 'picasa', 'google', 'zenfolio', 'instagram']; |
| 68 |
$layouts = ['square', 'circle', 'random', 'masonry', 'mosaic', 'strip-above', 'strip-below', 'strip-right', 'no-strip']; |
| 69 |
|
| 70 |
$pattern = get_shortcode_regex([$tag]); |
| 71 |
preg_match_all('/' . $pattern . '/s', $shortcode, $matches, PREG_OFFSET_CAPTURE); |
| 72 |
$type = 'photonic'; |
| 73 |
|
| 74 |
$message = $this->edit_shortcode; |
| 75 |
if (empty($shortcode)) { |
| 76 |
$message = $this->empty_shortcode; |
| 77 |
} |
| 78 |
else if (!empty($matches) && !empty($matches[0]) && !empty($matches[1]) && !empty($matches[2]) && !empty($matches[3])) { |
| 79 |
foreach ($matches[1] as $instance => $start) { |
| 80 |
if ($start[0] === '') { |
| 81 |
if (!empty($matches[3][$instance])) { |
| 82 |
$shortcode_attr = shortcode_parse_atts($matches[3][$instance][0]); |
| 83 |
if (!empty($shortcode_attr['type']) && in_array($shortcode_attr['type'], $types)) { |
| 84 |
$type = $shortcode_attr['type']; |
| 85 |
} |
| 86 |
else if (empty($shortcode_attr['type']) && !empty($shortcode_attr['style']) && in_array($shortcode_attr['style'], $layouts)) { |
| 87 |
$type = 'wp'; |
| 88 |
} |
| 89 |
else { |
| 90 |
$message = $this->invalid_shortcode; |
| 91 |
$shortcode = ''; |
| 92 |
} |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
else { |
| 98 |
$message = $this->invalid_shortcode; |
| 99 |
$shortcode = ''; |
| 100 |
} |
| 101 |
?> |
| 102 |
<div class="photonic-widget"> |
| 103 |
<p> |
| 104 |
<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title', 'photonic'); ?></label> |
| 105 |
<input id="<?php echo $this->get_field_id('title'); ?>" value="<?php echo $instance['title']; ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" class="widefat" /> |
| 106 |
</p> |
| 107 |
|
| 108 |
<input id="<?php echo $this->get_field_id('shortcode'); ?>" value="<?php echo $shortcode; ?>" type="hidden" name="<?php echo $this->get_field_name('shortcode'); ?>" class="photonic-shortcode"/> |
| 109 |
|
| 110 |
<div class="photonic-source"> |
| 111 |
<a class="photonic-wizard <?php echo $type; ?>" href="<?php echo $url; ?>"></a> |
| 112 |
<p> |
| 113 |
<?php echo $message; ?> |
| 114 |
</p> |
| 115 |
</div> |
| 116 |
|
| 117 |
<div class="photonic-shortcode-display"> |
| 118 |
<?php |
| 119 |
if ($shortcode !== '') { |
| 120 |
?> |
| 121 |
<h4><?php echo esc_html__('Current shortcode', 'photonic'); ?></h4> |
| 122 |
<code><?php echo $shortcode; ?></code> |
| 123 |
<?php |
| 124 |
} |
| 125 |
?> |
| 126 |
</div> |
| 127 |
</div> |
| 128 |
<?php |
| 129 |
} |
| 130 |
|
| 131 |
} |