index.php
100 lines
| 1 | <?php |
| 2 | |
| 3 | use Kubio\Core\Utils; |
| 4 | |
| 5 | function kubio_set_admin_bar_menu_customize_to_kubio_editor( WP_Admin_Bar &$admin_bar ) { |
| 6 | |
| 7 | if ( ! is_user_logged_in() || ! kubio_theme_has_kubio_block_support() ) { |
| 8 | return; |
| 9 | } |
| 10 | |
| 11 | $url = kubio_frontend_get_editor_url(); |
| 12 | $appearance_node = $admin_bar->get_node( 'customize' ); |
| 13 | if ( ! $appearance_node ) { |
| 14 | return; |
| 15 | } |
| 16 | $appearance_node->href = $url; |
| 17 | $admin_bar->add_node( $appearance_node ); |
| 18 | } |
| 19 | |
| 20 | function kubio_set_admin_bar_customize_to_kubio_editor() { |
| 21 | if ( ! kubio_theme_has_kubio_block_support() ) { |
| 22 | return; |
| 23 | } |
| 24 | global $submenu; |
| 25 | if ( ! isset( $submenu['themes.php'] ) ) { |
| 26 | return; |
| 27 | } |
| 28 | |
| 29 | foreach ( $submenu['themes.php'] as $key => $theme_submenu ) { |
| 30 | $slug = $theme_submenu[1]; |
| 31 | if ( $slug !== 'customize' ) { |
| 32 | continue; |
| 33 | } |
| 34 | $submenu['themes.php'][ $key ][2] = 'admin.php?page=kubio'; |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | function kubio_update_theme_page_customize_url( $prepared_themes ) { |
| 39 | if ( ! kubio_theme_has_kubio_block_support() ) { |
| 40 | return $prepared_themes; |
| 41 | } |
| 42 | foreach ( $prepared_themes as $key => $theme ) { |
| 43 | if ( $theme['active'] === true ) { |
| 44 | $prepared_themes[ $key ]['actions']['customize'] = Utils::kubioGetEditorURL(); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | return $prepared_themes; |
| 49 | } |
| 50 | |
| 51 | function kubio_update_dashboard_customizer_url() { |
| 52 | |
| 53 | // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotValidated, WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 54 | $request_uri = isset( $_SERVER['REQUEST_URI'] ) ? $_SERVER['REQUEST_URI'] : ''; |
| 55 | $is_dashboard_admin_or_theme = str_contains( $request_uri, 'wp-admin/index.php' ) || str_contains( $request_uri, 'wp-admin/themes.php' ); |
| 56 | |
| 57 | //Dashboard page or theme page |
| 58 | if ( ! $is_dashboard_admin_or_theme ) { |
| 59 | return; |
| 60 | } |
| 61 | |
| 62 | $kubio_url = Utils::kubioGetEditorURL(); |
| 63 | ob_start(); |
| 64 | ?> |
| 65 | <script> |
| 66 | (function($) { |
| 67 | $(document).ready(function(){ |
| 68 | |
| 69 | <?php if ( kubio_theme_has_kubio_block_support() ) : ?> |
| 70 | var customizeLink = document.querySelector('a.load-customize'); |
| 71 | if(customizeLink) { |
| 72 | customizeLink.setAttribute('href', <?php echo wp_json_encode( $kubio_url ); ?>); |
| 73 | } |
| 74 | <?php endif; ?> |
| 75 | |
| 76 | |
| 77 | //in the active theme page replace the edit with kubio link |
| 78 | var themeSingleTemplate = document.querySelector('#tmpl-theme-single') |
| 79 | if(themeSingleTemplate) { |
| 80 | let innerHtml = themeSingleTemplate.innerHTML; |
| 81 | let newInnerHtml = innerHtml.replace(/href='([^']*page=kubio)'/, "href='" + <?php echo wp_json_encode( $kubio_url ); ?> + "'"); |
| 82 | themeSingleTemplate.innerHTML = newInnerHtml; |
| 83 | } |
| 84 | }) |
| 85 | })(jQuery) |
| 86 | </script> |
| 87 | <?php |
| 88 | |
| 89 | // phpcs:ignore WordPress.WP.AlternativeFunctions.strip_tags_strip_tags |
| 90 | $script = strip_tags( ob_get_clean() ); |
| 91 | wp_add_inline_script( 'jquery', $script ); |
| 92 | return; |
| 93 | } |
| 94 | |
| 95 | add_filter( 'admin_init', 'kubio_update_dashboard_customizer_url' ); |
| 96 | add_action( 'wp_prepare_themes_for_js', 'kubio_update_theme_page_customize_url', 1000 ); |
| 97 | add_action( 'admin_bar_menu', 'kubio_set_admin_bar_menu_customize_to_kubio_editor', 1000 ); |
| 98 | add_action( 'admin_menu', 'kubio_set_admin_bar_customize_to_kubio_editor', 1000 ); |
| 99 | |
| 100 |