data
3 years ago
installer
3 months ago
widgets
3 months ago
admin-dashboard.php
2 years ago
admin-layouts.php
10 months ago
admin-widget-dialog.php
1 year ago
admin-widgets-bundle.php
11 months ago
admin.php
1 month ago
compatibility.php
1 month ago
css-builder.php
3 years ago
functions.php
3 years ago
home.php
3 years ago
live-editor.php
2 years ago
post-content-filters.php
2 years ago
renderer-legacy.php
3 months ago
renderer.php
1 month ago
revisions.php
3 years ago
settings.php
3 months ago
sidebars-emulator.php
2 years ago
styles-admin.php
11 months ago
styles.php
11 months ago
widget-shortcode.php
2 years ago
widget-shortcode.php
137 lines
| 1 | <?php |
| 2 | |
| 3 | class SiteOrigin_Panels_Widget_Shortcode { |
| 4 | public static $text_widgets = array( |
| 5 | 'SiteOrigin_Widget_Editor_Widget', |
| 6 | 'SiteOrigin_Panels_Widgets_Layout', |
| 7 | 'WP_Widget_Black_Studio_TinyMCE', |
| 8 | 'WP_Widget_Text', |
| 9 | ); |
| 10 | |
| 11 | public static function init() { |
| 12 | add_shortcode( 'siteorigin_widget', 'SiteOrigin_Panels_Widget_Shortcode::shortcode' ); |
| 13 | } |
| 14 | |
| 15 | public static function add_filters() { |
| 16 | add_filter( 'siteorigin_panels_the_widget_html', 'SiteOrigin_Panels_Widget_Shortcode::widget_html', 10, 4 ); |
| 17 | } |
| 18 | |
| 19 | public static function remove_filters() { |
| 20 | remove_filter( 'siteorigin_panels_the_widget_html', 'SiteOrigin_Panels_Widget_Shortcode::widget_html' ); |
| 21 | } |
| 22 | |
| 23 | /** |
| 24 | * This shortcode just displays a widget based on the given arguments |
| 25 | * |
| 26 | * @return string |
| 27 | */ |
| 28 | public static function shortcode( $attr, $content ) { |
| 29 | $attr = shortcode_atts( array( |
| 30 | 'class' => false, |
| 31 | 'id' => '', |
| 32 | ), $attr, 'siteorigin_widget' ); |
| 33 | |
| 34 | $attr['class'] = html_entity_decode( $attr['class'] ); |
| 35 | $attr['class'] = apply_filters( 'siteorigin_panels_widget_class', $attr['class'] ); |
| 36 | |
| 37 | $the_widget = ! empty( $attr['class'] ) ? SiteOrigin_Panels::get_widget_instance( $attr['class'] ) : null; |
| 38 | |
| 39 | if ( ! empty( $the_widget ) ) { |
| 40 | $data = self::decode_data( $content ); |
| 41 | |
| 42 | $widget_args = ! empty( $data['args'] ) ? $data['args'] : array(); |
| 43 | $widget_instance = ! empty( $data['instance'] ) ? $data['instance'] : array(); |
| 44 | |
| 45 | $widget_args = self::escape_widget_data( $widget_args ); |
| 46 | $widget_instance = self::escape_widget_data( $widget_instance ); |
| 47 | |
| 48 | $widget_args = wp_parse_args( array( |
| 49 | 'before_widget' => '', |
| 50 | 'after_widget' => '', |
| 51 | 'before_title' => '<h3 class="widget-title">', |
| 52 | 'after_title' => '</h3>', |
| 53 | ), $widget_args ); |
| 54 | |
| 55 | ob_start(); |
| 56 | $the_widget->widget( $widget_args, $widget_instance ); |
| 57 | |
| 58 | return ob_get_clean(); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | public static function escape_widget_data( $data ) { |
| 63 | try { |
| 64 | $escaped_data = array(); |
| 65 | foreach ( $data as $key => $value ) { |
| 66 | if ( is_array( $value ) ) { |
| 67 | $escaped_data[ $key ] = self::escape_widget_data( $value ); |
| 68 | } else { |
| 69 | $escaped_data[ $key ] = esc_html( $value ); |
| 70 | } |
| 71 | } |
| 72 | return $escaped_data; |
| 73 | } catch (Exception $e) { |
| 74 | // There was an error, return empty array. |
| 75 | return array(); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Get the shortcode for a specific widget |
| 81 | * |
| 82 | * @return string |
| 83 | */ |
| 84 | public static function get_shortcode( $widget, $args, $instance ) { |
| 85 | unset( $instance['panels_info'] ); |
| 86 | |
| 87 | $data = array( |
| 88 | 'instance' => $instance, |
| 89 | 'args' => $args, |
| 90 | ); |
| 91 | |
| 92 | // This allows other plugins to implement their own shortcode. For example, to work when Page Builder isn't active |
| 93 | $shortcode_name = apply_filters( 'siteorigin_panels_cache_shortcode', 'siteorigin_widget', $widget, $instance, $args ); |
| 94 | |
| 95 | $shortcode = '[' . $shortcode_name . ' '; |
| 96 | $shortcode .= 'class="' . htmlentities( preg_replace( '/\\\\+/', '\\\\\\\\', get_class( $widget ) ) ) . '"]'; |
| 97 | $shortcode .= self::encode_data( $data ); |
| 98 | $shortcode .= '[/' . $shortcode_name . ']'; |
| 99 | |
| 100 | return $shortcode; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * A filter to replace widgets with |
| 105 | */ |
| 106 | public static function widget_html( $html, $widget, $args, $instance ) { |
| 107 | if ( |
| 108 | empty( $GLOBALS['SITEORIGIN_PANELS_POST_CONTENT_RENDER'] ) || |
| 109 | // Don't try create HTML if there already is some |
| 110 | ! empty( $html ) || |
| 111 | ! is_object( $widget ) || |
| 112 | // Skip for known text based widgets |
| 113 | in_array( get_class( $widget ), self::$text_widgets ) |
| 114 | ) { |
| 115 | return $html; |
| 116 | } |
| 117 | |
| 118 | return self::get_shortcode( $widget, $args, $instance ); |
| 119 | } |
| 120 | |
| 121 | public static function encode_data( $data ) { |
| 122 | return '<input type="hidden" value="' . esc_textarea( wp_json_encode( $data, JSON_UNESCAPED_UNICODE ) ) . '" />'; |
| 123 | } |
| 124 | |
| 125 | public static function decode_data( $string ) { |
| 126 | preg_match( '/value="([^"]*)"/', trim( $string ), $matches ); |
| 127 | |
| 128 | if ( ! empty( $matches[1] ) ) { |
| 129 | $data = json_decode( html_entity_decode( $matches[1], ENT_QUOTES ), true ); |
| 130 | |
| 131 | return $data; |
| 132 | } else { |
| 133 | return array(); |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 |