-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathindex.php
248 lines (223 loc) · 6.89 KB
/
index.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
<?php
/**
* Plugin Name: Gutenberg
* Plugin URI: https://wordpress.github.io/gutenberg/
* Description: Prototyping since 1440. Development plugin for the editor focus in core.
* Version: 0.1.0
*
* @package gutenberg
*/
/**
* Gutenberg's Menu.
*
* Adds a new wp-admin menu page for the Gutenberg editor.
*
* @since 0.1.0
*/
function gutenberg_menu() {
add_menu_page(
'Gutenberg',
'Gutenberg',
'manage_options',
'gutenberg',
'the_gutenberg_project',
'dashicons-edit'
);
}
add_action( 'admin_menu', 'gutenberg_menu' );
/**
* Registers common scripts to be used as dependencies of the editor and plugins.
*
* @since 0.1.0
*/
function gutenberg_register_scripts() {
$suffix = SCRIPT_DEBUG ? '' : '.min';
// Vendor Scripts.
$react_suffix = ( SCRIPT_DEBUG ? '.development' : '.production' ) . $suffix;
wp_register_script( 'react', 'https://unpkg.com/react@next/umd/react' . $react_suffix . '.js' );
wp_register_script( 'react-dom', 'https://unpkg.com/react-dom@next/umd/react-dom' . $react_suffix . '.js', array( 'react' ) );
wp_register_script( 'react-dom-server', 'https://unpkg.com/react-dom@next/umd/react-dom-server' . $react_suffix . '.js', array( 'react' ) );
// Editor Scripts.
wp_register_script( 'tinymce-nightly', 'https://fiddle.azurewebsites.net/tinymce/nightly/tinymce.min.js' );
wp_register_script( 'wp-i18n', plugins_url( 'i18n/build/index.js', __FILE__ ), array(), filemtime( plugin_dir_path( __FILE__ ) . 'i18n/build/index.js' ) );
wp_register_script( 'wp-element', plugins_url( 'element/build/index.js', __FILE__ ), array( 'react', 'react-dom', 'react-dom-server' ), filemtime( plugin_dir_path( __FILE__ ) . 'element/build/index.js' ) );
wp_register_script( 'wp-blocks', plugins_url( 'blocks/build/index.js', __FILE__ ), array( 'wp-element', 'tinymce-nightly' ), filemtime( plugin_dir_path( __FILE__ ) . 'blocks/build/index.js' ) );
// Editor Styles.
wp_register_style( 'wp-blocks', plugins_url( 'blocks/build/style.css', __FILE__ ), array(), filemtime( plugin_dir_path( __FILE__ ) . 'blocks/build/style.css' ) );
}
add_action( 'init', 'gutenberg_register_scripts' );
/**
* Adds the filters to register additional links for the Gutenberg editor in
* the post/page screens.
*
* @since 0.1.0
*/
function gutenberg_add_edit_links_filters() {
// For hierarchical post types
add_filter( 'page_row_actions', 'gutenberg_add_edit_links', 10, 2 );
// For non-hierarchical post types
add_filter( 'post_row_actions', 'gutenberg_add_edit_links', 10, 2 );
}
add_action( 'admin_init', 'gutenberg_add_edit_links_filters' );
/**
* Registers additional links in the post/page screens to edit any post/page in
* the Gutenberg editor.
*
* @since 0.1.0
*/
function gutenberg_add_edit_links( $actions, $post ) {
$can_edit_post = current_user_can( 'edit_post', $post->ID );
$title = _draft_or_post_title( $post->ID );
if ( $can_edit_post && 'trash' !== $post->post_status ) {
// Build the Gutenberg edit action. See also:
// WP_Posts_List_Table::handle_row_actions()
$gutenberg_url = menu_page_url( 'gutenberg', false );
$gutenberg_action = sprintf(
'<a href="%s" aria-label="%s">%s</a>',
add_query_arg( 'post_id', $post->ID, $gutenberg_url ),
esc_attr( sprintf(
/* translators: %s: post title */
__( 'Edit “%s” in the Gutenberg editor', 'gutenberg' ),
$title
) ),
__( 'Gutenberg', 'gutenberg' )
);
// Insert the Gutenberg action immediately after the Edit action.
$edit_offset = array_search( 'edit', array_keys( $actions ), true );
$actions = array_merge(
array_slice( $actions, 0, $edit_offset + 1 ),
array( 'gutenberg hide-if-no-js' => $gutenberg_action ),
array_slice( $actions, $edit_offset + 1 )
);
}
return $actions;
}
/**
* Returns Jed-formatted localization data.
*
* @since 0.1.0
*
* @return array
*/
function gutenberg_get_jed_locale_data( $domain ) {
$translations = get_translations_for_domain( $domain );
$locale = array(
'domain' => $domain,
'locale_data' => array(
$domain => array(
'' => array(
'domain' => $domain,
'lang' => is_admin() ? get_user_locale() : get_locale()
)
)
)
);
if ( ! empty( $translations->headers['Plural-Forms'] ) ) {
$locale['locale_data'][ $domain ]['']['plural_forms'] = $translations->headers['Plural-Forms'];
}
foreach ( $translations->entries as $msgid => $entry ) {
$locale['locale_data'][ $domain ][ $msgid ] = $entry->translations;
}
return $locale;
}
/**
* Scripts & Styles.
*
* Enqueues the needed scripts and styles when visiting the top-level page of
* the Gutenberg editor.
*
* @since 0.1.0
*
* @param string $hook Screen name.
*/
function gutenberg_scripts_and_styles( $hook ) {
if ( 'toplevel_page_gutenberg' !== $hook ) {
return;
}
/**
* Scripts
*/
// The editor code itself
wp_enqueue_script(
'wp-editor',
plugins_url( 'editor/build/index.js', __FILE__ ),
array( 'wp-i18n', 'wp-blocks', 'wp-element' ),
false, // $ver
true // $in_footer
);
// Load an actual post if an ID is specified
$post_to_edit = null;
if ( isset( $_GET['post_id'] ) && (int) $_GET['post_id'] > 0 ) {
$request = new WP_REST_Request(
'GET',
sprintf( '/wp/v2/posts/%d', (int) $_GET['post_id'] )
);
$request->set_param( 'context', 'edit' );
$response = rest_do_request( $request );
if ( 200 === $response->get_status() ) {
$post_to_edit = $response->get_data();
}
}
// Initialize the post data...
if ( $post_to_edit ) {
// ...with a real post
wp_localize_script( 'wp-editor', '_wpGutenbergPost', $post_to_edit );
} else {
// ...with some test content
// TODO: replace this with error handling
wp_add_inline_script(
'wp-editor',
file_get_contents( plugin_dir_path( __FILE__ ) . 'post-content.js' )
);
}
// Prepare Jed locale data
$locale_data = gutenberg_get_jed_locale_data( 'gutenberg' );
wp_add_inline_script(
'wp-editor',
'wp.i18n.setLocaleData( ' . json_encode( $locale_data ) . ' );',
'before'
);
// Initialize the editor
wp_add_inline_script( 'wp-editor', 'wp.editor.createEditorInstance( \'editor\', _wpGutenbergPost );' );
/**
* Styles
*/
wp_enqueue_style(
'wp-editor-font',
'https://fonts.googleapis.com/css?family=Noto+Serif:400,400i,700,700i'
);
wp_enqueue_style(
'wp-editor',
plugins_url( 'editor/build/style.css', __FILE__ ),
array( 'wp-blocks' )
);
}
add_action( 'admin_enqueue_scripts', 'gutenberg_scripts_and_styles' );
/**
* Load plugin text domain for translations.
*
* @since 0.1.0
*/
function gutenberg_load_plugin_textdomain() {
load_plugin_textdomain(
'gutenberg',
false,
dirname( plugin_basename( __FILE__ ) ) . '/languages/'
);
}
add_action( 'plugins_loaded', 'gutenberg_load_plugin_textdomain' );
/**
* Project.
*
* The main entry point for the Gutenberg editor. Renders the editor on the
* wp-admin page for the plugin.
*
* @since 0.1.0
*/
function the_gutenberg_project() {
?>
<div class="gutenberg">
<section id="editor" class="gutenberg__editor"></section>
</div>
<?php
}