This repository has been archived by the owner on Oct 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
app.php
93 lines (81 loc) · 2.4 KB
/
app.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php
/**
* Module Name: App
* Description: The browser app where the work gets done.
* Namespace: App
*
* @package pattern-manager
*/
declare(strict_types=1);
namespace PatternManager\App;
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Get the values needed to render/hydrate the app.
*/
function get_app_state() {
return array(
'patterns' => \PatternManager\PatternDataHandlers\get_theme_patterns_with_editor_links(),
'apiNonce' => wp_create_nonce( 'wp_rest' ),
'apiEndpoints' => array(
'deletePatternEndpoint' => get_rest_url( false, 'pattern-manager/v1/delete-pattern/' ),
),
'siteUrl' => get_bloginfo( 'url' ),
'adminUrl' => admin_url(),
);
}
/**
* Render and enqueue the output required for the the app.
*/
function pattern_manager_app() {
$module_dir_path = module_dir_path( __FILE__ );
$module_dir_url = module_dir_url( __FILE__ );
if ( file_exists( $module_dir_path . 'js/build/index.asset.php' ) ) {
$dependencies = require $module_dir_path . 'js/build/index.asset.php';
$dependencies = $dependencies['dependencies'];
} else {
return;
}
// Include the app.
$js_url = $module_dir_url . 'js/build/index.js';
$js_ver = filemtime( $module_dir_path . 'js/build/index.js' );
wp_enqueue_script( 'pattern-manager', $js_url, $dependencies, $js_ver, true );
// Enqueue sass and Tailwind styles, combined automatically using PostCSS in wp-scripts.
$css_url = $module_dir_url . 'js/build/index.css';
$css_ver = filemtime( $module_dir_path . 'js/build/index.css' );
wp_enqueue_style( 'pattern_manager_style', $css_url, array( 'wp-edit-blocks' ), $css_ver );
wp_localize_script(
'pattern-manager',
'patternManager',
get_app_state()
);
echo '<div id="pattern-manager-app"></div>';
}
/**
* Set the URL for the link in the menu.
*/
function patternmanager_adminmenu_page() {
add_menu_page(
__( 'Patterns', 'pattern-manager' ),
__( 'Patterns', 'pattern-manager' ),
'administrator',
'pattern-manager',
__NAMESPACE__ . '\pattern_manager_app',
'dashicons-text',
$position = 2,
);
}
add_action( 'admin_menu', __NAMESPACE__ . '\patternmanager_adminmenu_page' );
/**
* Unhook all the admin_notices.
*
* @return void
*/
function hide_admin_notices() {
if ( 'pattern-manager' === filter_input( INPUT_GET, 'page' ) ) {
remove_all_actions( 'admin_notices' );
}
}
add_action( 'admin_head', __NAMESPACE__ . '\hide_admin_notices', 1 );