index.php
94 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | function kubio_get_editor_url() { |
| 5 | return add_query_arg( |
| 6 | array( 'page' => 'kubio' ), |
| 7 | admin_url( 'admin.php' ) |
| 8 | ); |
| 9 | |
| 10 | } |
| 11 | |
| 12 | function kubio_set_admin_bar_menu_customize_to_kubio_editor( WP_Admin_Bar &$admin_bar ) { |
| 13 | |
| 14 | if ( ! is_user_logged_in() || ! kubio_theme_has_kubio_block_support() ) { |
| 15 | return; |
| 16 | } |
| 17 | |
| 18 | $url = kubio_fronend_get_editor_url(); |
| 19 | $appearanceNode = $admin_bar->get_node( 'customize' ); |
| 20 | if ( ! $appearanceNode ) { |
| 21 | return; |
| 22 | } |
| 23 | $appearanceNode->href = $url; |
| 24 | $admin_bar->add_node( $appearanceNode ); |
| 25 | } |
| 26 | |
| 27 | function kubio_set_admin_bar_customize_to_kubio_editor() { |
| 28 | if ( ! kubio_theme_has_kubio_block_support() ) { |
| 29 | return; |
| 30 | } |
| 31 | global $submenu; |
| 32 | if ( ! isset( $submenu['themes.php'] ) ) { |
| 33 | return; |
| 34 | } |
| 35 | |
| 36 | foreach ( $submenu['themes.php'] as $key => $theme_submenu ) { |
| 37 | $slug = $theme_submenu[1]; |
| 38 | if ( $slug !== 'customize' ) { |
| 39 | continue; |
| 40 | } |
| 41 | $submenu['themes.php'][ $key ][2] = 'admin.php?page=kubio'; |
| 42 | } |
| 43 | |
| 44 | } |
| 45 | |
| 46 | function kubio_update_theme_page_customize_url( $prepared_themes ) { |
| 47 | if ( ! kubio_theme_has_kubio_block_support() ) { |
| 48 | return $prepared_themes; |
| 49 | } |
| 50 | foreach ( $prepared_themes as $key => $theme ) { |
| 51 | if ( $theme['active'] === true ) { |
| 52 | $prepared_themes[ $key ]['actions']['customize'] = kubio_get_editor_url(); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | return $prepared_themes; |
| 57 | } |
| 58 | |
| 59 | function kubio_update_dashboard_customizer_url() { |
| 60 | if ( ! kubio_theme_has_kubio_block_support() ) { |
| 61 | return; |
| 62 | } |
| 63 | $request_uri = $_SERVER['REQUEST_URI']; |
| 64 | $is_dashboard_admin = str_contains( $request_uri, 'wp-admin/index.php' ); |
| 65 | if ( ! $is_dashboard_admin ) { |
| 66 | return; |
| 67 | } |
| 68 | |
| 69 | $kubio_url = kubio_get_editor_url(); |
| 70 | ob_start(); |
| 71 | ?> |
| 72 | <script> |
| 73 | (function($) { |
| 74 | $(document).ready(function(){ |
| 75 | var customizeLink = document.querySelector('a.load-customize'); |
| 76 | if(!customizeLink) { |
| 77 | return |
| 78 | } |
| 79 | customizeLink.setAttribute('href', "<?php echo $kubio_url ?>") |
| 80 | }) |
| 81 | })(jQuery) |
| 82 | </script> |
| 83 | <?php |
| 84 | $script = strip_tags( ob_get_clean() ); |
| 85 | wp_add_inline_script( 'jquery', $script ); |
| 86 | return; |
| 87 | } |
| 88 | |
| 89 | add_filter( 'admin_init', 'kubio_update_dashboard_customizer_url' ); |
| 90 | add_action( 'wp_prepare_themes_for_js', 'kubio_update_theme_page_customize_url', 1000 ); |
| 91 | add_action( 'admin_bar_menu', 'kubio_set_admin_bar_menu_customize_to_kubio_editor', 1000 ); |
| 92 | add_action( 'admin_menu', 'kubio_set_admin_bar_customize_to_kubio_editor', 1000 ); |
| 93 | |
| 94 |