| @@ -1,186 +1,186 @@ | ||
| 1 | -<?php | |
| 2 | - | |
| 3 | -namespace FL\Assistant\Hooks; | |
| 4 | - | |
| 5 | -use FL\Assistant\Clients\Cloud\CloudClient; | |
| 6 | - | |
| 7 | -class CustomizerPreview { | |
| 8 | - | |
| 9 | - protected $item; | |
| 10 | - protected $uuid; | |
| 11 | - | |
| 12 | - public function __construct() { | |
| 13 | - add_action( 'customize_register', [ $this, 'init' ], PHP_INT_MAX ); | |
| 14 | - add_action( 'customize_preview_init', [ $this, 'init_preview_frame' ] ); | |
| 15 | - } | |
| 16 | - | |
| 17 | - public function init() { | |
| 18 | - if ( ! current_user_can( 'edit_others_posts' ) ) { | |
| 19 | - return false; | |
| 20 | - } | |
| 21 | - | |
| 22 | - if ( isset( $_GET['fl-asst-customizer-preview-init'] ) ) { | |
| 23 | - $this->load_item(); | |
| 24 | - $this->load_changeset(); | |
| 25 | - $this->redirect_to_changeset(); | |
| 26 | - } | |
| 27 | - | |
| 28 | - if ( isset( $_GET['fl-asst-customizer-preview'] ) && wp_is_uuid( $_GET['fl-asst-customizer-preview'] ) ) { | |
| 29 | - if ( ! isset( $_GET['changeset_uuid'] ) ) { | |
| 30 | - $this->uuid = $_GET['fl-asst-customizer-preview']; | |
| 31 | - $this->redirect_to_changeset(); | |
| 32 | - } else { | |
| 33 | - add_action( 'customize_controls_print_styles', [ $this, 'print_styles' ] ); | |
| 34 | - } | |
| 35 | - } | |
| 36 | - } | |
| 37 | - | |
| 38 | - public function load_item() { | |
| 39 | - $item_id = absint( $_GET['fl-asst-customizer-preview-init'] ); | |
| 40 | - | |
| 41 | - if ( $item_id ) { | |
| 42 | - $client = new CloudClient; | |
| 43 | - $item = $client->libraries->get_item( $item_id ); | |
| 44 | - | |
| 45 | - if ( $item ) { | |
| 46 | - $this->item = $item; | |
| 47 | - } | |
| 48 | - } | |
| 49 | - } | |
| 50 | - | |
| 51 | - public function load_changeset() { | |
| 52 | - if ( $this->item ) { | |
| 53 | - $posts = get_posts( [ | |
| 54 | - 'post_type' => 'customize_changeset', | |
| 55 | - 'post_status' => 'auto-draft', | |
| 56 | - 'meta_key' => 'fl_asst_changeset_item_id', | |
| 57 | - 'meta_value' => $this->item->id, | |
| 58 | - ] ); | |
| 59 | - | |
| 60 | - if ( count( $posts ) ) { | |
| 61 | - $this->uuid = $posts[0]->post_name; | |
| 62 | - $this->update_changeset( $posts[0]->ID ); | |
| 63 | - } else { | |
| 64 | - $this->create_changeset(); | |
| 65 | - } | |
| 66 | - } | |
| 67 | - } | |
| 68 | - | |
| 69 | - public function update_changeset( $post_id ) { | |
| 70 | - if ( $this->item ) { | |
| 71 | - wp_update_post( [ | |
| 72 | - 'ID' => $post_id, | |
| 73 | - 'post_content' => $this->create_changeset_content(), | |
| 74 | - ] ); | |
| 75 | - } | |
| 76 | - } | |
| 77 | - | |
| 78 | - public function create_changeset() { | |
| 79 | - if ( $this->item ) { | |
| 80 | - $this->uuid = wp_generate_uuid4(); | |
| 81 | - wp_insert_post( [ | |
| 82 | - 'post_type' => 'customize_changeset', | |
| 83 | - 'post_status' => 'auto-draft', | |
| 84 | - 'post_author' => get_current_user_id(), | |
| 85 | - 'post_name' => $this->uuid, | |
| 86 | - 'post_content' => $this->create_changeset_content(), | |
| 87 | - 'meta_input' => [ | |
| 88 | - 'fl_asst_changeset_item_id' => $this->item->id, | |
| 89 | - '_customize_restore_dismissed' => 1, | |
| 90 | - ] | |
| 91 | - ] ); | |
| 92 | - } | |
| 93 | - } | |
| 94 | - | |
| 95 | - public function create_changeset_content() { | |
| 96 | - global $wp_customize; | |
| 97 | - | |
| 98 | - $theme = $this->item->data->theme; | |
| 99 | - $mods = $this->item->data->mods; | |
| 100 | - $options = $this->item->data->options; | |
| 101 | - $css = $this->item->data->css; | |
| 102 | - $current_mods = get_theme_mods(); | |
| 103 | - $settings = $wp_customize->settings(); | |
| 104 | - $slug = get_stylesheet(); | |
| 105 | - $user_id = get_current_user_id(); | |
| 106 | - $date = date( 'Y-m-d h:i:s' ); | |
| 107 | - $content = []; | |
| 108 | - | |
| 109 | - foreach ( $mods as $key => $value ) { | |
| 110 | - if ( isset( $current_mods[ $key ] ) && $value === $current_mods[ $key ] ) { | |
| 111 | - continue; | |
| 112 | - } | |
| 113 | - $content[ "{$slug}::{$key}" ] = [ | |
| 114 | - 'value' => wp_slash( $value ), | |
| 115 | - 'type' => 'theme_mod', | |
| 116 | - 'user_id' => $user_id, | |
| 117 | - 'date_modified_gmt' => $date | |
| 118 | - ]; | |
| 119 | - } | |
| 120 | - | |
| 121 | - foreach ( $settings as $key => $setting ) { | |
| 122 | - if ( 'option' === $setting->type ) { | |
| 123 | - if ( isset( $options->$key ) && $options->$key !== $setting->value() ) { | |
| 124 | - $content[ $key ] = [ | |
| 125 | - 'value' => wp_slash( $options->$key ), | |
| 126 | - 'type' => 'option', | |
| 127 | - 'user_id' => $user_id, | |
| 128 | - 'date_modified_gmt' => $date | |
| 129 | - ]; | |
| 130 | - } | |
| 131 | - } | |
| 132 | - } | |
| 133 | - | |
| 134 | - if ( $css ) { | |
| 135 | - $content[ "custom_css[{$slug}]" ] = [ | |
| 136 | - 'value' => wp_slash( $value ), | |
| 137 | - 'type' => 'custom_css', | |
| 138 | - 'user_id' => $user_id, | |
| 139 | - 'date_modified_gmt' => $date | |
| 140 | - ]; | |
| 141 | - } | |
| 142 | - | |
| 143 | - return json_encode( $content, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT ); | |
| 144 | - } | |
| 145 | - | |
| 146 | - public function redirect_to_changeset() { | |
| 147 | - if ( $this->uuid ) { | |
| 148 | - $args = [ | |
| 149 | - 'changeset_uuid' => $this->uuid, | |
| 150 | - 'fl-asst-customizer-preview' => $this->uuid | |
| 151 | - ]; | |
| 152 | - wp_redirect( add_query_arg( $args, admin_url( 'customize.php' ) ) ); | |
| 153 | - die(); | |
| 154 | - } | |
| 155 | - } | |
| 156 | - | |
| 157 | - public function print_styles() { | |
| 158 | - echo '<style>'; | |
| 159 | - echo '.wp-customizer .wp-full-overlay { margin: 0 !important; }'; | |
| 160 | - echo '.wp-customizer #customize-controls { display: none !important; }'; | |
| 161 | - echo '</style>'; | |
| 162 | - } | |
| 163 | - | |
| 164 | - public function init_preview_frame() { | |
| 165 | - if ( ! isset( $_GET['customize_changeset_uuid'] ) || ! wp_is_uuid( $_GET['customize_changeset_uuid'] ) ) { | |
| 166 | - return; | |
| 167 | - } | |
| 168 | - | |
| 169 | - $posts = get_posts( [ | |
| 170 | - 'post_type' => 'customize_changeset', | |
| 171 | - 'post_status' => 'auto-draft', | |
| 172 | - 'post_name' => $_GET['customize_changeset_uuid'], | |
| 173 | - 'meta_key' => 'fl_asst_changeset_item_id' | |
| 174 | - ] ); | |
| 175 | - | |
| 176 | - if ( count( $posts ) ) { | |
| 177 | - add_action( 'wp_head', [ $this, 'print_preview_frame_styles' ] ); | |
| 178 | - } | |
| 179 | - } | |
| 180 | - | |
| 181 | - public function print_preview_frame_styles() { | |
| 182 | - echo '<style>'; | |
| 183 | - echo '.customize-partial-edit-shortcut { display: none !important; }'; | |
| 184 | - echo '</style>'; | |
| 185 | - } | |
| 186 | -} | |
| 1 | +<?php | |
| 2 | + | |
| 3 | +namespace FL\Assistant\Hooks; | |
| 4 | + | |
| 5 | +use FL\Assistant\Clients\Cloud\CloudClient; | |
| 6 | + | |
| 7 | +class CustomizerPreview { | |
| 8 | + | |
| 9 | + protected $item; | |
| 10 | + protected $uuid; | |
| 11 | + | |
| 12 | + public function __construct() { | |
| 13 | + add_action( 'customize_register', [ $this, 'init' ], PHP_INT_MAX ); | |
| 14 | + add_action( 'customize_preview_init', [ $this, 'init_preview_frame' ] ); | |
| 15 | + } | |
| 16 | + | |
| 17 | + public function init() { | |
| 18 | + if ( ! current_user_can( 'edit_others_posts' ) ) { | |
| 19 | + return false; | |
| 20 | + } | |
| 21 | + | |
| 22 | + if ( isset( $_GET['fl-asst-customizer-preview-init'] ) ) { | |
| 23 | + $this->load_item(); | |
| 24 | + $this->load_changeset(); | |
| 25 | + $this->redirect_to_changeset(); | |
| 26 | + } | |
| 27 | + | |
| 28 | + if ( isset( $_GET['fl-asst-customizer-preview'] ) && wp_is_uuid( $_GET['fl-asst-customizer-preview'] ) ) { | |
| 29 | + if ( ! isset( $_GET['changeset_uuid'] ) ) { | |
| 30 | + $this->uuid = $_GET['fl-asst-customizer-preview']; | |
| 31 | + $this->redirect_to_changeset(); | |
| 32 | + } else { | |
| 33 | + add_action( 'customize_controls_print_styles', [ $this, 'print_styles' ] ); | |
| 34 | + } | |
| 35 | + } | |
| 36 | + } | |
| 37 | + | |
| 38 | + public function load_item() { | |
| 39 | + $item_id = absint( $_GET['fl-asst-customizer-preview-init'] ); | |
| 40 | + | |
| 41 | + if ( $item_id ) { | |
| 42 | + $client = new CloudClient; | |
| 43 | + $item = $client->libraries->get_item( $item_id ); | |
| 44 | + | |
| 45 | + if ( $item ) { | |
| 46 | + $this->item = $item; | |
| 47 | + } | |
| 48 | + } | |
| 49 | + } | |
| 50 | + | |
| 51 | + public function load_changeset() { | |
| 52 | + if ( $this->item ) { | |
| 53 | + $posts = get_posts( [ | |
| 54 | + 'post_type' => 'customize_changeset', | |
| 55 | + 'post_status' => 'auto-draft', | |
| 56 | + 'meta_key' => 'fl_asst_changeset_item_id', | |
| 57 | + 'meta_value' => $this->item->id, | |
| 58 | + ] ); | |
| 59 | + | |
| 60 | + if ( count( $posts ) ) { | |
| 61 | + $this->uuid = $posts[0]->post_name; | |
| 62 | + $this->update_changeset( $posts[0]->ID ); | |
| 63 | + } else { | |
| 64 | + $this->create_changeset(); | |
| 65 | + } | |
| 66 | + } | |
| 67 | + } | |
| 68 | + | |
| 69 | + public function update_changeset( $post_id ) { | |
| 70 | + if ( $this->item ) { | |
| 71 | + wp_update_post( [ | |
| 72 | + 'ID' => $post_id, | |
| 73 | + 'post_content' => $this->create_changeset_content(), | |
| 74 | + ] ); | |
| 75 | + } | |
| 76 | + } | |
| 77 | + | |
| 78 | + public function create_changeset() { | |
| 79 | + if ( $this->item ) { | |
| 80 | + $this->uuid = wp_generate_uuid4(); | |
| 81 | + wp_insert_post( [ | |
| 82 | + 'post_type' => 'customize_changeset', | |
| 83 | + 'post_status' => 'auto-draft', | |
| 84 | + 'post_author' => get_current_user_id(), | |
| 85 | + 'post_name' => $this->uuid, | |
| 86 | + 'post_content' => $this->create_changeset_content(), | |
| 87 | + 'meta_input' => [ | |
| 88 | + 'fl_asst_changeset_item_id' => $this->item->id, | |
| 89 | + '_customize_restore_dismissed' => 1, | |
| 90 | + ] | |
| 91 | + ] ); | |
| 92 | + } | |
| 93 | + } | |
| 94 | + | |
| 95 | + public function create_changeset_content() { | |
| 96 | + global $wp_customize; | |
| 97 | + | |
| 98 | + $theme = $this->item->data->theme; | |
| 99 | + $mods = $this->item->data->mods; | |
| 100 | + $options = $this->item->data->options; | |
| 101 | + $css = $this->item->data->css; | |
| 102 | + $current_mods = get_theme_mods(); | |
| 103 | + $settings = $wp_customize->settings(); | |
| 104 | + $slug = get_stylesheet(); | |
| 105 | + $user_id = get_current_user_id(); | |
| 106 | + $date = date( 'Y-m-d h:i:s' ); | |
| 107 | + $content = []; | |
| 108 | + | |
| 109 | + foreach ( $mods as $key => $value ) { | |
| 110 | + if ( isset( $current_mods[ $key ] ) && $value === $current_mods[ $key ] ) { | |
| 111 | + continue; | |
| 112 | + } | |
| 113 | + $content[ "{$slug}::{$key}" ] = [ | |
| 114 | + 'value' => wp_slash( $value ), | |
| 115 | + 'type' => 'theme_mod', | |
| 116 | + 'user_id' => $user_id, | |
| 117 | + 'date_modified_gmt' => $date | |
| 118 | + ]; | |
| 119 | + } | |
| 120 | + | |
| 121 | + foreach ( $settings as $key => $setting ) { | |
| 122 | + if ( 'option' === $setting->type ) { | |
| 123 | + if ( isset( $options->$key ) && $options->$key !== $setting->value() ) { | |
| 124 | + $content[ $key ] = [ | |
| 125 | + 'value' => wp_slash( $options->$key ), | |
| 126 | + 'type' => 'option', | |
| 127 | + 'user_id' => $user_id, | |
| 128 | + 'date_modified_gmt' => $date | |
| 129 | + ]; | |
| 130 | + } | |
| 131 | + } | |
| 132 | + } | |
| 133 | + | |
| 134 | + if ( $css ) { | |
| 135 | + $content[ "custom_css[{$slug}]" ] = [ | |
| 136 | + 'value' => wp_slash( $value ), | |
| 137 | + 'type' => 'custom_css', | |
| 138 | + 'user_id' => $user_id, | |
| 139 | + 'date_modified_gmt' => $date | |
| 140 | + ]; | |
| 141 | + } | |
| 142 | + | |
| 143 | + return json_encode( $content, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT ); | |
| 144 | + } | |
| 145 | + | |
| 146 | + public function redirect_to_changeset() { | |
| 147 | + if ( $this->uuid ) { | |
| 148 | + $args = [ | |
| 149 | + 'changeset_uuid' => $this->uuid, | |
| 150 | + 'fl-asst-customizer-preview' => $this->uuid | |
| 151 | + ]; | |
| 152 | + wp_redirect( add_query_arg( $args, admin_url( 'customize.php' ) ) ); | |
| 153 | + die(); | |
| 154 | + } | |
| 155 | + } | |
| 156 | + | |
| 157 | + public function print_styles() { | |
| 158 | + echo '<style>'; | |
| 159 | + echo '.wp-customizer .wp-full-overlay { margin: 0 !important; }'; | |
| 160 | + echo '.wp-customizer #customize-controls { display: none !important; }'; | |
| 161 | + echo '</style>'; | |
| 162 | + } | |
| 163 | + | |
| 164 | + public function init_preview_frame() { | |
| 165 | + if ( ! isset( $_GET['customize_changeset_uuid'] ) || ! wp_is_uuid( $_GET['customize_changeset_uuid'] ) ) { | |
| 166 | + return; | |
| 167 | + } | |
| 168 | + | |
| 169 | + $posts = get_posts( [ | |
| 170 | + 'post_type' => 'customize_changeset', | |
| 171 | + 'post_status' => 'auto-draft', | |
| 172 | + 'post_name' => $_GET['customize_changeset_uuid'], | |
| 173 | + 'meta_key' => 'fl_asst_changeset_item_id' | |
| 174 | + ] ); | |
| 175 | + | |
| 176 | + if ( count( $posts ) ) { | |
| 177 | + add_action( 'wp_head', [ $this, 'print_preview_frame_styles' ] ); | |
| 178 | + } | |
| 179 | + } | |
| 180 | + | |
| 181 | + public function print_preview_frame_styles() { | |
| 182 | + echo '<style>'; | |
| 183 | + echo '.customize-partial-edit-shortcut { display: none !important; }'; | |
| 184 | + echo '</style>'; | |
| 185 | + } | |
| 186 | +} | |