siteorigin.php
181 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Handle compatibility with Pagebuilder by SiteOrigin Plugin |
| 5 | * |
| 6 | * Process AJAX actions. |
| 7 | * |
| 8 | * @copyright Copyright (c) 2016, Jeffrey Carandang |
| 9 | * @since 3.0 |
| 10 | */ |
| 11 | // Exit if accessed directly |
| 12 | if (! defined('ABSPATH')) exit; |
| 13 | |
| 14 | if (!function_exists('widgetopts_siteorigin_panels_data')) { |
| 15 | add_filter('siteorigin_panels_data', 'widgetopts_siteorigin_panels_data', 10, 4); |
| 16 | function widgetopts_siteorigin_panels_data($panels_data, $post_id) |
| 17 | { |
| 18 | global $widget_options; |
| 19 | if (!is_admin()) { |
| 20 | if (isset($panels_data['widgets']) && !empty($panels_data['widgets']) && is_array($panels_data['widgets'])) { |
| 21 | |
| 22 | global $current_user; |
| 23 | |
| 24 | foreach ($panels_data['widgets'] as $key => $widgets) { |
| 25 | if (isset($widgets['extended_widget_opts']) && !empty($widgets['extended_widget_opts'])) { |
| 26 | |
| 27 | if (isset($panels_data['widgets'][$key]) && 'activate' == $widget_options['logic']) { |
| 28 | // New snippet-based system |
| 29 | if (isset($widgets['extended_widget_opts']['class']['logic_snippet_id']) && !empty($widgets['extended_widget_opts']['class']['logic_snippet_id'])) { |
| 30 | $snippet_id = $widgets['extended_widget_opts']['class']['logic_snippet_id']; |
| 31 | if (class_exists('WidgetOpts_Snippets_API')) { |
| 32 | $result = WidgetOpts_Snippets_API::execute_snippet($snippet_id); |
| 33 | if ($result === false) { |
| 34 | unset($panels_data['widgets'][$key]); |
| 35 | } |
| 36 | } |
| 37 | } |
| 38 | // Legacy support for old inline logic |
| 39 | elseif (isset($widgets['extended_widget_opts']['class']['logic']) && !empty($widgets['extended_widget_opts']['class']['logic'])) { |
| 40 | // Flag that legacy migration is needed |
| 41 | if (!get_option('wopts_display_logic_migration_required', false)) { |
| 42 | update_option('wopts_display_logic_migration_required', true); |
| 43 | } |
| 44 | |
| 45 | $display_logic = stripslashes(trim($widgets['extended_widget_opts']['class']['logic'])); |
| 46 | $display_logic = apply_filters('widget_options_logic_override', $display_logic); |
| 47 | if ($display_logic === false) { |
| 48 | unset($panels_data['widgets'][$key]); |
| 49 | } |
| 50 | if ($display_logic === true) { |
| 51 | // return true; |
| 52 | } |
| 53 | $display_logic = htmlspecialchars_decode($display_logic, ENT_QUOTES); |
| 54 | try { |
| 55 | if (!widgetopts_safe_eval($display_logic)) { |
| 56 | unset($panels_data['widgets'][$key]); |
| 57 | } |
| 58 | } catch (ParseError $e) { |
| 59 | unset($panels_data['widgets'][$key]); |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | return $panels_data; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Protect legacy display logic from modification/injection on SiteOrigin save. |
| 73 | * Intercepts panels_data meta save, restores old logic values from DB. |
| 74 | * |
| 75 | * @since 5.1 |
| 76 | */ |
| 77 | if (!function_exists('widgetopts_siteorigin_protect_logic_on_save')) { |
| 78 | add_filter('update_post_metadata', 'widgetopts_siteorigin_protect_logic_on_save', 10, 5); |
| 79 | function widgetopts_siteorigin_protect_logic_on_save($check, $object_id, $meta_key, $meta_value, $prev_value) { |
| 80 | if ($meta_key !== 'panels_data') return $check; |
| 81 | |
| 82 | static $processing = false; |
| 83 | if ($processing) return $check; |
| 84 | |
| 85 | // Get old panels data from DB |
| 86 | $old_data = get_post_meta($object_id, 'panels_data', true); |
| 87 | if (!is_array($old_data) || empty($old_data['widgets'])) return $check; |
| 88 | |
| 89 | // Collect known old logic values |
| 90 | $old_logic_set = array(); |
| 91 | foreach ($old_data['widgets'] as $widget) { |
| 92 | if (isset($widget['extended_widget_opts']['class']['logic']) && $widget['extended_widget_opts']['class']['logic'] !== '') { |
| 93 | $old_logic_set[] = $widget['extended_widget_opts']['class']['logic']; |
| 94 | } |
| 95 | } |
| 96 | if (empty($old_logic_set)) return $check; |
| 97 | |
| 98 | // Check new data |
| 99 | $new_data = is_array($meta_value) ? $meta_value : maybe_unserialize($meta_value); |
| 100 | if (!is_array($new_data) || empty($new_data['widgets'])) return $check; |
| 101 | |
| 102 | $modified = false; |
| 103 | foreach ($new_data['widgets'] as &$widget) { |
| 104 | if (!isset($widget['extended_widget_opts']['class'])) continue; |
| 105 | |
| 106 | $cls = &$widget['extended_widget_opts']['class']; |
| 107 | $wopt_ver = isset($cls['wopt_version']) ? $cls['wopt_version'] : ''; |
| 108 | $has_snippet = !empty($cls['logic_snippet_id']); |
| 109 | $has_legacy = !empty($cls['logic']); |
| 110 | |
| 111 | // If wopt_version >= 4.2: legacy logic is obsolete — clear it |
| 112 | if ($wopt_ver !== '' && version_compare($wopt_ver, '4.2', '>=')) { |
| 113 | if ($has_legacy) { |
| 114 | $cls['logic'] = ''; |
| 115 | $modified = true; |
| 116 | } |
| 117 | unset($cls); |
| 118 | continue; |
| 119 | } |
| 120 | |
| 121 | // Auto-clear legacy logic if snippet_id is already set (migration done) |
| 122 | if ($has_snippet && $has_legacy) { |
| 123 | $cls['logic'] = ''; |
| 124 | $cls['wopt_version'] = WIDGETOPTS_VERSION; |
| 125 | $modified = true; |
| 126 | unset($cls); |
| 127 | continue; |
| 128 | } |
| 129 | |
| 130 | // User intentionally cleared legacy logic via Clear button |
| 131 | if (!empty($cls['logic_cleared'])) { |
| 132 | $cls['logic'] = ''; |
| 133 | unset($cls['logic_cleared']); |
| 134 | $cls['wopt_version'] = WIDGETOPTS_VERSION; |
| 135 | $modified = true; |
| 136 | unset($cls); |
| 137 | continue; |
| 138 | } |
| 139 | |
| 140 | if (!isset($cls['logic'])) { |
| 141 | unset($cls); |
| 142 | continue; |
| 143 | } |
| 144 | $val = $cls['logic']; |
| 145 | if ($val !== '' && !in_array($val, $old_logic_set, true)) { |
| 146 | // Injected value not in old data — strip |
| 147 | $cls['logic'] = ''; |
| 148 | $modified = true; |
| 149 | } |
| 150 | |
| 151 | unset($cls); |
| 152 | } |
| 153 | |
| 154 | if ($modified) { |
| 155 | $processing = true; |
| 156 | update_post_meta($object_id, 'panels_data', $new_data); |
| 157 | $processing = false; |
| 158 | return true; // Short-circuit original update |
| 159 | } |
| 160 | |
| 161 | return $check; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | if (!function_exists('widgetopts_siteorigin_panels_widget_classes')) { |
| 166 | add_filter('siteorigin_panels_widget_classes', 'widgetopts_siteorigin_panels_widget_classes', 10, 4); |
| 167 | function widgetopts_siteorigin_panels_widget_classes($classes, $widget, $instance, $widget_info) |
| 168 | { |
| 169 | if (isset($instance['extended_widget_opts'])) { |
| 170 | global $widget_options; |
| 171 | |
| 172 | $get_classes = widgetopts_classes_generator($instance['extended_widget_opts'], $widget_options, $widget_options['settings'], true); |
| 173 | $get_classes[] = 'widgetopts-SO'; |
| 174 | |
| 175 | $classes = apply_filters('widgetopts_siteorigin_panels_widget_classes', array_merge($classes, $get_classes), $widget_info); |
| 176 | } |
| 177 | |
| 178 | return $classes; |
| 179 | } |
| 180 | } |
| 181 |