-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.php
More file actions
424 lines (360 loc) · 11.1 KB
/
Copy pathindex.php
File metadata and controls
424 lines (360 loc) · 11.1 KB
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
<?php
/*
* Plugin Name: Presentation Block
* Plugin URI: https://wordpress.org/plugins/presentation-block/
* Description: Creates a custom post type “Presentation”, which will render as a presentation on the front end using Reveal.js.
* Version: 1.0.4
* Author: AbstractWP
* Author URI: https://www.abstractwp.com/
* Text Domain: slide
* License: GPL-2.0-or-later
* License URI https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
*/
namespace presentation;
define( 'PRESENTATION_VER', '1.0.3' );
/**
* Add scripts and style for presentation post type on admin.
*/
function presentation_admin_enqueue_scripts() {
if ( get_post_type() !== 'presentation' ) {
return;
}
wp_enqueue_code_editor( array( 'type' => 'text/css' ) );
wp_enqueue_script(
'slide',
plugins_url( 'build/js/index.js', __FILE__ ),
array(
'wp-element',
'wp-i18n',
'wp-blocks',
'wp-rich-text',
'wp-plugins',
'wp-edit-post',
'wp-data',
'wp-components',
'wp-block-editor',
'wp-url',
'wp-compose',
'wp-hooks',
),
filemtime( dirname( __FILE__ ) . '/build/js/index.js' ),
true
);
wp_enqueue_style(
'slide',
plugins_url( 'index.css', __FILE__ ),
array(),
filemtime( dirname( __FILE__ ) . '/index.css' )
);
wp_deregister_style( 'wp-block-library-theme' );
wp_register_style(
'wp-block-library-theme',
plugins_url( 'common.css', __FILE__ ),
array(),
filemtime( dirname( __FILE__ ) . '/common.css' )
);
global $wp_styles;
$template_directory_uri = get_template_directory_uri();
foreach ( $wp_styles->queue as $handle ) {
$info = $wp_styles->registered[ $handle ];
if ( substr( $info->src, 0, strlen( $template_directory_uri ) ) === $template_directory_uri ) {
wp_dequeue_style( $handle );
}
}
}
add_action( 'admin_enqueue_scripts', __NAMESPACE__ . '\presentation_admin_enqueue_scripts', 99999 );
// Register presentation custom post type.
add_action( 'init',
function() {
require 'register.php';
}
);
register_activation_hook(
__FILE__,
function() {
require 'register.php';
flush_rewrite_rules();
}
);
/**
* Use special template for presentation post type.
*
* @param string $path the template path.
*/
function presentation_template_include( $path ) {
if ( ! is_singular( 'presentation' ) ) {
return $path;
}
if ( isset( $_GET['speaker'] ) ) { // phpcs:ignore.
return dirname( __FILE__ ) . '/speaker.php';
}
the_post();
return dirname( __FILE__ ) . '/template.php';
}
add_filter( 'template_include', __NAMESPACE__ . '\presentation_template_include' );
/**
* Add scripts and style for presentation post type.
* Dequeue the theme style. It is not needed for the presentations, as they are individually crafted.
*/
function presentation_frontend_enqueue_scripts() {
if ( ! is_singular( 'presentation' ) ) {
return;
}
global $wp_styles;
$template_directory_uri = get_template_directory_uri();
foreach ( $wp_styles->queue as $handle ) {
$info = $wp_styles->registered[ $handle ];
if ( substr( $info->src, 0, strlen( $template_directory_uri ) ) === $template_directory_uri ) {
wp_dequeue_style( $handle );
}
}
if ( isset( $_GET['speaker'] ) ) { // phpcs:ignore.
wp_enqueue_script(
'slide-speaker',
plugins_url( 'build/js/speaker.js', __FILE__ ),
array(),
filemtime( dirname( __FILE__ ) . '/build/js/speaker.js' ),
true
);
wp_enqueue_style(
'slide-speaker',
plugins_url( 'speaker.css', __FILE__ ),
array(),
filemtime( dirname( __FILE__ ) . '/speaker.css' )
);
return;
}
wp_enqueue_script(
'slide-reveal',
plugins_url( 'reveal/reveal.min.js', __FILE__ ),
array(),
'4.3.1',
true
);
wp_enqueue_script(
'slide-reveal-notes',
plugins_url( 'reveal/notes.min.js', __FILE__ ),
array( 'slide-reveal' ),
filemtime( dirname( __FILE__ ) . '/reveal/notes.min.js' ),
true
);
wp_enqueue_script(
'slide-template',
plugins_url( 'build/js/template.js', __FILE__ ),
array( 'slide-reveal', 'slide-reveal-notes', 'wp-i18n' ),
filemtime( dirname( __FILE__ ) . '/build/js/template.js' ),
true
);
$post_id = get_the_ID();
$contain = (bool) get_post_meta( $post_id, 'presentation-contain', true ) ? get_post_meta( $post_id, 'presentation-contain', true ) : false;
wp_localize_script(
'slide-template',
'slideTemplate',
array(
'revealSettings' => array(
'transition' => get_post_meta( $post_id, 'presentation-transition', true ) ? get_post_meta( $post_id, 'presentation-transition', true ) : 'none',
'backgroundTransition' => get_post_meta( $post_id, 'presentation-background-transition', true ) ? get_post_meta( $post_id, 'presentation-background-transition', true ) : 'none',
'transitionSpeed' => get_post_meta( $post_id, 'presentation-transition-speed', true ) ? get_post_meta( $post_id, 'presentation-transition-speed', true ) : 'default',
'controls' => (bool) get_post_meta( $post_id, 'presentation-controls', true ) ? get_post_meta( $post_id, 'presentation-controls', true ) : false,
'progress' => (bool) get_post_meta( $post_id, 'presentation-progress', true ) ? get_post_meta( $post_id, 'presentation-progress', true ) : false,
'hash' => true,
'history' => true,
'preloadIframes' => true,
'hideAddressBar' => true,
'height' => 720,
'width' => (int) get_post_meta( $post_id, 'presentation-width', true ) ? get_post_meta( $post_id, 'presentation-width', true ) : 960,
'margin' => $contain ? 0 : 0.08,
'keyboard' => array(
'38' => 'prev',
'40' => 'next',
),
'overview' => false,
// We center in CSS.
'center' => false,
'pdfMaxPagesPerSlide' => 1,
),
'contain' => $contain,
)
);
wp_enqueue_style(
'slide-reveal',
plugins_url( 'reveal/reveal.min.css', __FILE__ ),
array(),
'4.3.1'
);
if ( isset( $_GET['print-pdf'] ) ) { // phpcs:ignore.
wp_enqueue_style(
'slide-reveal-pdf',
plugins_url( 'reveal/pdf.min.css', __FILE__ ),
array(),
'3.8.0'
);
}
$font_url = get_post_meta( get_the_ID(), 'presentation-font-family-url', true );
if ( $font_url ) {
wp_enqueue_style(
'slide-default-font',
$font_url,
array(),
PRESENTATION_VER
);
}
$heading_font_url = get_post_meta( get_the_ID(), 'presentation-font-family-heading-url', true );
if ( $heading_font_url ) {
wp_enqueue_style(
'slide-heading-font',
$heading_font_url,
array(),
PRESENTATION_VER
);
}
wp_deregister_style( 'wp-block-library-theme' );
wp_enqueue_style(
'slide-common',
plugins_url( 'common.css', __FILE__ ),
array(),
filemtime( dirname( __FILE__ ) . '/common.css' )
);
}
add_action( 'wp_enqueue_scripts', __NAMESPACE__ . '\presentation_frontend_enqueue_scripts', 99999 );
/**
* Update editor settings on presentation post type.
*/
function presentation_editor_settings () {
if ( 'presentation' !== get_current_screen()->post_type ) {
return;
}
remove_editor_styles();
remove_theme_support( 'editor-color-palette' );
remove_theme_support( 'editor-font-sizes' );
add_theme_support( 'align-wide' );
if ( ! isset( $_GET['post'] ) ) { // phpcs:ignore.
return;
}
$post = get_post( $_GET['post'] ); // phpcs:ignore.
if ( ! $post ) {
return;
}
$palette = get_post_meta( $post->ID, 'presentation-color-palette', true );
if ( ! $palette ) {
return;
}
$palette = explode( ',', $palette );
$palette = array_map( 'trim', $palette );
$palette = array_map( 'sanitize_hex_color', $palette );
$palette = array_map(
function( $hex ) {
return array( 'color' => $hex );
},
$palette
);
if ( count( $palette ) ) {
add_theme_support( 'editor-color-palette', $palette );
}
}
foreach ( array( 'load-post.php', 'load-post-new.php' ) as $presentation_screen ) {
add_action( $presentation_screen, __NAMESPACE__ . '\presentation_editor_settings' , 99999 );
}
/**
* Prepare styles array for block settings.
*
* @param array $settings the setting data.
*/
function presentation_block_editor_settings( $settings ) {
if ( 'presentation' !== get_current_screen()->post_type ) {
return $settings;
}
$settings['styles'] = array();
return $settings;
}
add_filter( 'block_editor_settings', __NAMESPACE__ . '\presentation_block_editor_settings', 99999 );
/**
* Add default template for presentation type.
*
* @param string $post_content the content of post type.
* @param object $post the post data.
*/
function presentation_add_default_content( $post_content, $post ) {
if ( 'presentation' !== $post->post_type ) {
return $post_content;
}
return file_get_contents( __DIR__ . '/default-content.html' ); // phpcs:ignore.
}
add_filter( 'default_content', __NAMESPACE__ . '\presentation_add_default_content', 10, 2 );
/**
* Update slide block content for presentation.
*
* @param string $block_content the content of block.
* @param object $block the block data.
*/
function presentation_render_block( $block_content, $block ) {
if ( ! current_user_can( 'edit_posts' ) ) {
return $block_content;
}
if ( 'slide/slide' !== $block['blockName'] ) {
return $block_content;
}
if ( empty( $block['attrs']['notes'] ) ) {
return $block_content;
}
$pos = strrpos( $block_content, '</section>', -1 );
$notes = '<aside class="notes">' . $block['attrs']['notes'] . '</aside>';
return substr_replace( $block_content, $notes, $pos, 0 );
}
add_filter( 'render_block', __NAMESPACE__ . '\presentation_render_block', 10, 2 );
/**
* Only allow formanting and media blocks on presentation cpt.
*
* @param array $allowed_block_types the list of blocks.
* @param object $editor_context the editor context data.
*/
function presentation_allowed_block_types ( $allowed_block_types, $editor_context ) {
if ( ! empty( $editor_context->post ) ) {
if ( 'presentation' === $editor_context->post->post_type ) {
return array(
'core/heading',
'core/paragraph',
'core/list',
'core/list-item',
'core/quote',
'core/image',
'core/media-text',
'core/group',
'slide/slide',
);
}
}
return $allowed_block_types;
}
add_filter('allowed_block_types_all', __NAMESPACE__ . '\presentation_allowed_block_types', 10, 2);
/**
* Registers the block pattern categories.
*/
function presentation_register_block_pattern_categories() {
if ( function_exists( 'register_block_pattern_category' ) ) {
register_block_pattern_category( 'slide', array( 'label' => esc_html_x( 'Slide', 'Slide patterns category', 'slide' ) ) );
}
}
add_action( 'init', __NAMESPACE__ . '\presentation_register_block_pattern_categories' );
/**
* Registers the block patterns.
*/
function presentation_register_block_patterns() {
$files = array(
'home.php'
);
$path = trailingslashit( plugin_dir_path( __FILE__ ) ) . 'patterns/';
foreach ( $files as $file ) {
if ( file_exists( $path . $file ) ) {
require_once $path . $file;
}
}
}
add_action( 'init', __NAMESPACE__ . '\presentation_register_block_patterns', 10, 1 );
/**
* Remove core patterns on presentation.
*/
function remove_block_patterns_support() {
remove_theme_support( 'core-block-patterns', array( 'presentation' ));
}
add_action('init', __NAMESPACE__ . '\remove_block_patterns_support');