AI
10 months ago
admin-pages
1 year ago
api
2 weeks ago
blog
1 year ago
customizer
1 year ago
filters
1 month ago
full-site-editing
1 month ago
importer
1 year ago
integrations
1 month ago
menu
1 year ago
polyfills
1 year ago
preview
8 months ago
recommendations
1 month ago
shapes
2 years ago
shortcodes
1 year ago
src
2 weeks ago
add-edit-in-kubio.php
1 year ago
editor-assets.php
2 weeks ago
filters.php
1 year ago
frontend.php
1 year ago
global-data.php
1 year ago
init.php
1 year ago
kubio-block-library.php
1 month ago
kubio-editor.php
1 month ago
load.php
1 month ago
add-edit-in-kubio.php
420 lines
| 1 | <?php |
| 2 | |
| 3 | use Kubio\Core\Utils; |
| 4 | |
| 5 | |
| 6 | function kubio_edit_post_row_style() { |
| 7 | ?> |
| 8 | <style> |
| 9 | |
| 10 | span.kubio_edit_post * { |
| 11 | fill: currentColor; |
| 12 | } |
| 13 | span.kubio-edit svg { |
| 14 | width: 12px; |
| 15 | height: 12px; |
| 16 | margin-right: 2px; |
| 17 | fill: currentColor; |
| 18 | } |
| 19 | |
| 20 | span.kubio-edit { |
| 21 | display: inline !important; |
| 22 | vertical-align: middle; |
| 23 | } |
| 24 | </style> |
| 25 | <?php |
| 26 | } |
| 27 | function kubio_add_edit_post_row_actions( $actions, $post ) { |
| 28 | |
| 29 | if ( ! current_user_can( 'edit_theme_options' ) ) { |
| 30 | return $actions; |
| 31 | } |
| 32 | |
| 33 | $supported_post_type = array( 'page', 'wp_template', 'wp_template_part' ); |
| 34 | if ( ! in_array( $post->post_type, $supported_post_type ) ) { |
| 35 | return $actions; |
| 36 | } |
| 37 | |
| 38 | $post_id = $post->ID; |
| 39 | $is_translate_redirect = false; |
| 40 | //when you try to edit the translated page redirect to the original language |
| 41 | if ( kubio_wpml_is_active() ) { |
| 42 | $translated_id = kubio_wpml_get_original_language_post_id( $post_id, $post->post_type ); |
| 43 | if ( $translated_id !== $post_id ) { |
| 44 | $is_translate_redirect = true; |
| 45 | $post_id = $translated_id; |
| 46 | if ( in_array( $post->post_type, array( 'wp_template', 'wp_template_part' ) ) ) { |
| 47 | $post = get_post( $translated_id ); |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | if ( in_array( $post->post_type, array( 'wp_template', 'wp_template_part' ) ) ) { |
| 52 | $template = _kubio_build_template_result_from_post( $post ); |
| 53 | |
| 54 | if ( is_wp_error( $template ) ) { |
| 55 | return $actions; |
| 56 | } |
| 57 | |
| 58 | $post_id = $template->id; |
| 59 | } |
| 60 | |
| 61 | $args = array( |
| 62 | 'page' => 'kubio', |
| 63 | 'postId' => $post_id, |
| 64 | 'postType' => $post->post_type, |
| 65 | ); |
| 66 | if ( $is_translate_redirect ) { |
| 67 | $args['isTranslateRedirect'] = 1; |
| 68 | } |
| 69 | $edit_url = add_query_arg( |
| 70 | $args, |
| 71 | admin_url( 'admin.php' ) |
| 72 | ); |
| 73 | |
| 74 | $status = get_post_status( $post_id ); |
| 75 | $is_trashed = strpos( $post_id, '__trashed' ); |
| 76 | |
| 77 | if ( $status === 'draft' || $status === 'auto-draft' ) { |
| 78 | $edit_url = add_query_arg( |
| 79 | array( |
| 80 | 'action' => 'edit', |
| 81 | 'post' => $post_id, |
| 82 | 'kubio-publish-draft' => 1, |
| 83 | ), |
| 84 | admin_url( 'post.php' ) |
| 85 | ); |
| 86 | } |
| 87 | |
| 88 | if ( $status !== 'trash' && $is_trashed === false ) { |
| 89 | $link = sprintf( |
| 90 | '<a href="%s"><span style="display:none" class="kubio-edit">%s</span>%s</a>', |
| 91 | esc_url( $edit_url ), |
| 92 | wp_kses_post( KUBIO_LOGO_SVG ), |
| 93 | esc_html__( 'Edit with Kubio', 'kubio' ) |
| 94 | ); |
| 95 | |
| 96 | $actions = array_merge( |
| 97 | array( |
| 98 | 'kubio_edit_post' => $link, |
| 99 | ), |
| 100 | $actions |
| 101 | ); |
| 102 | |
| 103 | if ( ! has_action( 'admin_footer', 'kubio_edit_post_row_style' ) ) { |
| 104 | add_action( 'admin_footer', 'kubio_edit_post_row_style' ); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | return $actions; |
| 109 | } |
| 110 | |
| 111 | function kubio_post_edit_add_button() { |
| 112 | |
| 113 | global $post; |
| 114 | |
| 115 | if ( ! $post ) { |
| 116 | return; |
| 117 | } |
| 118 | |
| 119 | if ( ! current_user_can( 'edit_theme_options' ) ) { |
| 120 | return; |
| 121 | } |
| 122 | |
| 123 | if ( ! in_array( $post->post_type, array( 'wp_template', 'wp_template_part', 'page' ) ) ) { |
| 124 | return; |
| 125 | } |
| 126 | |
| 127 | if ( kubio_is_kubio_editor_page() ) { |
| 128 | return; |
| 129 | } |
| 130 | |
| 131 | $post_id = $post->ID; |
| 132 | $is_translate_redirect = false; |
| 133 | $translated_id = kubio_wpml_get_original_language_post_id( $post_id, $post->post_type ); |
| 134 | if ( $translated_id !== $post_id ) { |
| 135 | $is_translate_redirect = true; |
| 136 | $post_id = $translated_id; |
| 137 | if ( in_array( $post->post_type, array( 'wp_template', 'wp_template_part' ), true ) ) { |
| 138 | $post = get_post( $translated_id ); |
| 139 | } |
| 140 | } |
| 141 | if ( in_array( $post->post_type, array( 'wp_template', 'wp_template_part' ), true ) ) { |
| 142 | $template = _kubio_build_template_result_from_post( $post ); |
| 143 | |
| 144 | if ( is_wp_error( $template ) ) { |
| 145 | esc_html_e( 'Unknown', 'kubio' ); |
| 146 | } |
| 147 | |
| 148 | $post_id = $template->id; |
| 149 | } |
| 150 | $args = array( |
| 151 | 'page' => 'kubio', |
| 152 | 'postId' => $post_id, |
| 153 | 'postType' => $post->post_type, |
| 154 | ); |
| 155 | if ( $is_translate_redirect ) { |
| 156 | $args['isTranslateRedirect'] = 1; |
| 157 | } |
| 158 | $edit_url = add_query_arg( |
| 159 | $args, |
| 160 | admin_url( 'admin.php' ) |
| 161 | ); |
| 162 | |
| 163 | add_action( |
| 164 | 'admin_head', |
| 165 | function () { |
| 166 | ?> |
| 167 | <style> |
| 168 | a.components-button.edit-in-kubio.is-primary svg { |
| 169 | width: 1em; |
| 170 | height: 1em; |
| 171 | margin-right: 0.5em; |
| 172 | fill: currentColor; |
| 173 | } |
| 174 | </style> |
| 175 | <?php |
| 176 | |
| 177 | if ( Utils::wpVersionCompare( '6.3', '>=' ) ) { |
| 178 | ?> |
| 179 | <style> |
| 180 | a.components-button.edit-in-kubio.is-primary { |
| 181 | |
| 182 | margin-right: 30px; |
| 183 | } |
| 184 | </style> |
| 185 | <?php |
| 186 | } |
| 187 | } |
| 188 | ); |
| 189 | |
| 190 | ob_start(); |
| 191 | ?> |
| 192 | |
| 193 | <script> |
| 194 | (function () { |
| 195 | const url = <?php echo wp_json_encode( $edit_url ); ?>; |
| 196 | const icon = <?php echo wp_json_encode( base64_encode( KUBIO_LOGO_SVG ) ); ?>; |
| 197 | const label = <?php echo wp_json_encode( '<span>' . esc_html__( 'Edit with Kubio', 'kubio' ) . '</span>' ); ?>; |
| 198 | let unsubscribe = null; |
| 199 | top.kubioEditInKubioURL = url; |
| 200 | |
| 201 | const createButton = () => { |
| 202 | |
| 203 | |
| 204 | if (unsubscribe) { |
| 205 | unsubscribe(); |
| 206 | unsubscribe = null; |
| 207 | } |
| 208 | |
| 209 | setInterval(() => { |
| 210 | const toolbar = document.querySelector('.edit-post-header-toolbar'); |
| 211 | if (toolbar) { |
| 212 | if ( |
| 213 | !toolbar.querySelector('.components-button.edit-in-kubio') |
| 214 | ) { |
| 215 | const link = document.createElement('a'); |
| 216 | link.href = url; |
| 217 | link.innerHTML = atob(icon) + label; |
| 218 | link.setAttribute( |
| 219 | 'class', |
| 220 | 'components-button edit-in-kubio is-primary' |
| 221 | ); |
| 222 | toolbar.appendChild(link); |
| 223 | link.addEventListener('click', function (event) { |
| 224 | const editorSelect = wp.data.select('core/editor'); |
| 225 | if (editorSelect) { |
| 226 | if ( |
| 227 | 'draft' === |
| 228 | editorSelect.getEditedPostAttribute( |
| 229 | 'status' |
| 230 | ) || |
| 231 | 'auto-draft' === |
| 232 | editorSelect.getEditedPostAttribute( |
| 233 | 'status' |
| 234 | ) |
| 235 | ) { |
| 236 | event.preventDefault(); |
| 237 | event.stopPropagation(); |
| 238 | wp.hooks.doAction( |
| 239 | 'kubio.post-edit.open-draft-page', |
| 240 | { target: event.currentTarget, url } |
| 241 | ); |
| 242 | } |
| 243 | } |
| 244 | }); |
| 245 | wp.hooks.doAction('kubio.post-edit.button-created', { |
| 246 | target: link, |
| 247 | url, |
| 248 | }); |
| 249 | } |
| 250 | } |
| 251 | }, 2000); |
| 252 | }; |
| 253 | |
| 254 | unsubscribe = wp.data.subscribe(createButton); |
| 255 | })(); |
| 256 | </script> |
| 257 | <?php |
| 258 | |
| 259 | $script = str_replace( "\n\t\t", "\n", ob_get_clean() ); |
| 260 | |
| 261 | // phpcs:ignore WordPress.WP.AlternativeFunctions.strip_tags_strip_tags |
| 262 | wp_add_inline_script( 'wp-block-editor', strip_tags( $script ), 'after' ); |
| 263 | } |
| 264 | |
| 265 | |
| 266 | add_filter( 'page_row_actions', 'kubio_add_edit_post_row_actions', 0, 2 ); |
| 267 | add_filter( 'post_row_actions', 'kubio_add_edit_post_row_actions', 0, 2 ); |
| 268 | |
| 269 | |
| 270 | add_action( 'enqueue_block_editor_assets', 'kubio_post_edit_add_button', 0, 2 ); |
| 271 | |
| 272 | function kubio_frontend_get_editor_url() { |
| 273 | global $post; |
| 274 | if ( gettype( $post ) === 'integer' ) { |
| 275 | $post = get_post( $post ); |
| 276 | } |
| 277 | |
| 278 | // Add site-editor link. |
| 279 | $url = null; |
| 280 | if ( ! is_admin() && current_user_can( 'edit_theme_options' ) ) { |
| 281 | |
| 282 | // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotValidated, WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 283 | $current_url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; |
| 284 | |
| 285 | $args = array(); |
| 286 | if ( is_singular() || is_single() ) { |
| 287 | $post_id = $post->ID; |
| 288 | $is_translate_redirect = false; |
| 289 | //when you try to edit the translated page redirect to the original language |
| 290 | $translated_post_id = kubio_wpml_get_original_language_post_id( $post_id, $post->post_type ); |
| 291 | if ( $translated_post_id !== $post_id ) { |
| 292 | $post_id = $translated_post_id; |
| 293 | $is_translate_redirect = true; |
| 294 | } |
| 295 | $args = array( |
| 296 | 'postId' => $post_id, |
| 297 | 'postType' => $post->post_type, |
| 298 | ); |
| 299 | if ( $is_translate_redirect ) { |
| 300 | $args['isTranslateRedirect'] = 1; |
| 301 | } |
| 302 | } else { |
| 303 | |
| 304 | $block_template = null; |
| 305 | |
| 306 | if ( is_front_page() && is_home() ) { |
| 307 | $stylesheet = get_stylesheet(); |
| 308 | $query = new WP_Query( |
| 309 | array( |
| 310 | 'post_type' => 'wp_template', |
| 311 | 'post_status' => array( 'publish' ), |
| 312 | 'post_name__in' => array( 'index', 'home' ), |
| 313 | 'posts_per_page' => 1, |
| 314 | 'no_found_rows' => true, |
| 315 | // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query |
| 316 | 'tax_query' => array( |
| 317 | array( |
| 318 | 'taxonomy' => 'wp_theme', |
| 319 | 'field' => 'name', |
| 320 | 'terms' => array( $stylesheet ), |
| 321 | ), |
| 322 | ), |
| 323 | ) |
| 324 | ); |
| 325 | |
| 326 | $block_template = $query->have_posts() ? _kubio_build_template_result_from_post( $query->next_post() ) : null; |
| 327 | } |
| 328 | |
| 329 | //for 404 if you try to use the current_url it crashes the editor so we have a special case for it |
| 330 | if ( is_404() ) { |
| 331 | $block_template = resolve_block_template( '404', array( '404.php' ), null ); |
| 332 | } |
| 333 | |
| 334 | if ( $block_template ) { |
| 335 | $args = array( |
| 336 | 'postId' => urlencode( $block_template->id ), |
| 337 | 'postType' => 'wp_template', |
| 338 | ); |
| 339 | } else { |
| 340 | $args['pageURL'] = $current_url; |
| 341 | |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | $args = apply_filters( 'kubio/frontend/edit-in-kubio-args', $args ); |
| 346 | |
| 347 | $url = Utils::kubioGetEditorURL( $args ); |
| 348 | } |
| 349 | |
| 350 | return $url; |
| 351 | } |
| 352 | |
| 353 | function kubio_frontend_adminbar_items( $wp_admin_bar ) { |
| 354 | |
| 355 | if ( ! current_user_can( 'edit_theme_options' ) ) { |
| 356 | return; |
| 357 | } |
| 358 | |
| 359 | $url = kubio_frontend_get_editor_url(); |
| 360 | |
| 361 | if ( $url ) { |
| 362 | |
| 363 | global $post; |
| 364 | if ( gettype( $post ) === 'integer' ) { |
| 365 | $post = get_post( $post ); |
| 366 | } |
| 367 | if ( empty( $post ) ) { |
| 368 | return; |
| 369 | } |
| 370 | //for now only disable on product as maybe some users update custom post types. Disable what we know it does not work |
| 371 | if ( $post->post_type === 'product' ) { |
| 372 | return; |
| 373 | } |
| 374 | $wp_admin_bar->add_menu( |
| 375 | array( |
| 376 | 'id' => 'kubio-site-editor', |
| 377 | 'title' => sprintf( '<span class="kubio-admin-bar-menu-item">%s<span>%s</span></span>', wp_kses_post( KUBIO_LOGO_SVG ), __( 'Edit with Kubio', 'kubio' ) ), |
| 378 | 'href' => $url, |
| 379 | ) |
| 380 | ); |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | add_action( 'admin_bar_menu', 'kubio_frontend_adminbar_items', 80 ); |
| 385 | |
| 386 | |
| 387 | function kubio_frontend_adminbar_items_style() { |
| 388 | ?> |
| 389 | <style> |
| 390 | .kubio-admin-bar-menu-item { |
| 391 | display: flex; |
| 392 | align-items:center; |
| 393 | } |
| 394 | |
| 395 | .kubio-admin-bar-menu-item span { |
| 396 | display: block; |
| 397 | white-space: nowrap; |
| 398 | } |
| 399 | .kubio-admin-bar-menu-item svg { |
| 400 | max-height: 14px; |
| 401 | fill: #09f; |
| 402 | flex-grow: 0; |
| 403 | min-width: 20px; |
| 404 | margin-right: 0.4em !important; |
| 405 | } |
| 406 | |
| 407 | a:focus .kubio-admin-tbar-menu-item, |
| 408 | a:hover .kubio-admin-tbar-menu-item { |
| 409 | background: rgba(0, 153 ,255 , 0.6); |
| 410 | color: #fff; |
| 411 | |
| 412 | } |
| 413 | |
| 414 | </style> |
| 415 | <?php |
| 416 | } |
| 417 | |
| 418 | add_action( 'wp_after_admin_bar_render', 'kubio_frontend_adminbar_items_style' ); |
| 419 | |
| 420 |