Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 38 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
| StagingMenu | |
0.00% |
0 / 38 |
|
0.00% |
0 / 6 |
132 | |
0.00% |
0 / 1 |
| init | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| add_nfd_subnav | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
2 | |||
| add_staging_toolbar_items | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
12 | |||
| add_management_page | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
2 | |||
| tools_staging | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| handle_staging_redirect | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
20 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace NewfoldLabs\WP\Module\Staging; |
| 4 | |
| 5 | use function NewfoldLabs\WP\ModuleLoader\container; |
| 6 | |
| 7 | /** |
| 8 | * Class StagingMenu |
| 9 | */ |
| 10 | class StagingMenu { |
| 11 | /** |
| 12 | * Initialize. |
| 13 | */ |
| 14 | public static function init() { |
| 15 | // add admin menu |
| 16 | add_action( 'admin_bar_menu', array( __CLASS__, 'add_staging_toolbar_items' ) ); |
| 17 | add_filter( 'nfd_plugin_subnav', array( __CLASS__, 'add_nfd_subnav' ) ); |
| 18 | // handle staging redirect from old url |
| 19 | add_action( 'admin_init', array( __CLASS__, 'handle_staging_redirect' ) ); |
| 20 | // add tools page |
| 21 | add_action( 'admin_menu', array( __CLASS__, 'add_management_page' ) ); |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * Add to the Newfold subnav. |
| 26 | * |
| 27 | * @param array $subnav The nav array. |
| 28 | * @return array The filtered nav array |
| 29 | */ |
| 30 | public static function add_nfd_subnav( $subnav ) { |
| 31 | $staging = array( |
| 32 | 'route' => container()->plugin()->id . '#/settings/staging', |
| 33 | 'title' => _x( 'Staging', 'Menu item text', 'wp-module-staging' ), |
| 34 | 'priority' => 62, |
| 35 | ); |
| 36 | array_push( $subnav, $staging ); |
| 37 | return $subnav; |
| 38 | } |
| 39 | |
| 40 | |
| 41 | /** |
| 42 | * Customize the admin bar. |
| 43 | * |
| 44 | * @param \WP_Admin_Bar $admin_bar An instance of the WP_Admin_Bar class. |
| 45 | */ |
| 46 | public static function add_staging_toolbar_items( \WP_Admin_Bar $admin_bar ) { |
| 47 | if ( current_user_can( 'manage_options' ) ) { |
| 48 | |
| 49 | if ( container()->get( 'isStaging' ) ) { |
| 50 | $args = array( |
| 51 | 'id' => 'newfold-staging', |
| 52 | 'href' => apply_filters( 'nfd_build_url', admin_url( 'admin.php?page=' . container()->plugin()->id . '#/staging' ) ), |
| 53 | 'title' => '<div style="background-color: #ce0000; padding: 0 10px;color:#fff;">' . esc_html__( 'Staging Environment', 'wp-module-staging' ) . '</div>', |
| 54 | 'meta' => array( |
| 55 | 'title' => esc_attr__( 'Staging Actions', 'wp-module-staging' ), |
| 56 | ), |
| 57 | ); |
| 58 | $admin_bar->add_menu( $args ); |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Adds the Page to the WordPress Tools menu. |
| 65 | * |
| 66 | * @return void |
| 67 | */ |
| 68 | public static function add_management_page() { |
| 69 | |
| 70 | add_management_page( |
| 71 | __( 'Staging', 'wp-module-staging' ), |
| 72 | __( 'Staging', 'wp-module-staging' ), |
| 73 | 'manage_options', |
| 74 | Staging::PAGE_SLUG, |
| 75 | array( __CLASS__, 'tools_staging' ) |
| 76 | ); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Redirects to the plugin staging page. |
| 81 | * This is the callback for the tools staging page. |
| 82 | * |
| 83 | * @return void |
| 84 | */ |
| 85 | public static function tools_staging() { |
| 86 | $new_url = admin_url( 'admin.php?page=' . container()->plugin()->id . '#/settings/staging' ); |
| 87 | wp_safe_redirect( $new_url ); |
| 88 | exit; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Handle staging redirect from old URL. |
| 93 | * This runs on admin_init to catch the redirect before headers are sent. |
| 94 | * |
| 95 | * @return void |
| 96 | */ |
| 97 | public static function handle_staging_redirect() { |
| 98 | if ( |
| 99 | is_admin() && |
| 100 | isset( $_GET['page'] ) && |
| 101 | Staging::PAGE_SLUG === $_GET['page'] |
| 102 | ) { |
| 103 | $new_url = admin_url( 'admin.php?page=' . container()->plugin()->id . '#/settings/staging' ); |
| 104 | wp_safe_redirect( $new_url ); |
| 105 | exit; |
| 106 | } |
| 107 | } |
| 108 | } |