-
Notifications
You must be signed in to change notification settings - Fork 0
/
opensheetmusicdisplay.php
620 lines (592 loc) · 19.6 KB
/
opensheetmusicdisplay.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
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
<?php
/**
* Plugin Name: OpenSheetMusicDisplay
* Description: Block (and shortcode) to render MusicXML in the browser as sheet music using OSMD. Additionally provides a block to deeplink musicXML into the PracticeBird app.
* Version: 1.4.0
* Author: opensheetmusicdisplay.org
* Author URI: https://opensheetmusicdisplay.org
* License: GPL-2.0-or-later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: opensheetmusicdisplay
*
* @package phonicscore
*/
/**
* Registers all block assets so that they can be enqueued through the block editor
* in the corresponding context.
*
* @see https://developer.wordpress.org/block_editor/tutorials/block_tutorial/applying_styles_with_stylesheets/
*/
if ( ! defined( 'ABSPATH' ) ) exit;
//These are the default values on the user settings page. NOT what the user has set, but the initial values for these attributes.
//What the user has set is defined in phonicscore_opensheetmusicdisplay_user_set_defaults
function phonicescore_opensheetmusicdisplay_settings_page_defaults(){
$default_values = array(
'drawTitle' => 'drawTitle',
'drawSubtitle' => 'drawSubtitle',
'drawComposer' => 'drawComposer',
'drawLyricist' => 'drawLyricist',
'drawMetronomeMarks' => 'drawMetronomeMarks',
'drawPartNames' => 'drawPartNames',
'drawPartAbbreviations' => 'drawPartAbbreviations',
'drawMeasureNumbers' => 'drawMeasureNumbers',
'drawMeasureNumbersOnlyAtSystemStart' => false,
'drawTimeSignatures' => 'drawTimeSignatures',
'newSystemFromXML' => false,
'zoom' => 1.0
);
$default_values = apply_filters('phonicscore/opensheetmusicdisplay/settings-default-values', $default_values);
return $default_values;
}
define("phonicscore_opensheetmusicdisplay_settings_page_defaults", phonicescore_opensheetmusicdisplay_settings_page_defaults());
include_once 'opensheetmusicdisplay-settings.php';
define("phonicscore_opensheetmusicdisplay_base_attributes", phonicscore_opensheetmusicdisplay_get_attributes_list());
define("phonicscore_opensheetmusicdisplay_user_set_defaults", phonicscore_opensheetmusicdisplay_get_user_set_defaults());
define("phonicscore_opensheetmusicdisplay_processed_defaults", phonicscore_opensheetmusicdisplay_get_processed_defaults());
if(!defined('SERVER_SCHEME')){
define('SERVER_SCHEME', (isset($_SERVER['HTTPS']) ? "https" : "http"));
}
if(!defined('SERVER_HOST') && isset($_SERVER['HTTP_HOST'])){
define('SERVER_HOST', sanitize_text_field($_SERVER['HTTP_HOST']));
}
if(!defined('SERVER_REQUEST_URI') && isset($_SERVER['REQUEST_URI'])){
define('SERVER_REQUEST_URI', sanitize_text_field($_SERVER['REQUEST_URI']));
}
function phonicscore_opensheetmusicdisplay_generate_admin_client_attributes(){
$jsonBaseDefaults = wp_json_encode(phonicscore_opensheetmusicdisplay_base_attributes, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_NUMERIC_CHECK);
$jsonUserDefaults = wp_json_encode(phonicscore_opensheetmusicdisplay_user_set_defaults, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_NUMERIC_CHECK);
return <<<EOT
(function(){
const baseDefaults = {$jsonBaseDefaults};
const userDefaults = {$jsonUserDefaults};
function registerAttributes(settings, name){
if( name === 'phonicscore/opensheetmusicdisplay'){
settings.attributes = {...settings.attributes, ...baseDefaults};
}
return settings;
}
function getUserDefaults(attributes){
if(!attributes){
attributes = {};
}
attributes = {...attributes, ...userDefaults};
return attributes;
}
if(typeof wp !== 'undefined'){
if(typeof baseDefaults === 'object'){
wp.hooks.addFilter(
'blocks.registerBlockType',
'phonicscore/opensheetmusicdisplay/block-type-hook',
registerAttributes
);
}
if(typeof userDefaults === 'object'){
wp.hooks.addFilter(
'phonicscore_opensheetmusicdisplay_attributes-user-defaults',
'phonicscore/opensheetmusicdisplay/get-user-defaults',
getUserDefaults
);
}
}
})();
EOT;
}
function phonicscore_opensheetmusicdisplay_block_init() {
$dir = __DIR__;
$script_asset_path = "$dir/build/osmd_block.asset.php";
if ( ! file_exists( $script_asset_path ) ) {
throw new Error(
'You need to run `npm start` or `npm run build` for the "phonicscore/opensheetmusicdisplay" block first.'
);
}
$index_js = 'build/osmd_block.js';
$script_asset = require( $script_asset_path );
//Use default dependencies, add OSMD as one
if(array_key_exists('dependencies', $script_asset) && is_array($script_asset['dependencies'])){
$script_asset['dependencies'][] = 'phonicscore_opensheetmusicdisplay_opensheetmusicdisplay_block_exports';
$script_asset['dependencies'][] = 'phonicscore_opensheetmusicdisplay_opensheetmusicdisplay_dist';
$script_asset['dependencies'][] = 'fredmeister77_queueable_attributes_dist';
}
wp_register_script(
'phonicscore_opensheetmusicdisplay_block_editor',
plugins_url( $index_js, __FILE__ ),
$script_asset['dependencies'],
$script_asset['version']
);
wp_set_script_translations( 'phonicscore_opensheetmusicdisplay_block_editor', 'phonicscore_opensheetmusicdisplay' );
$editor_css = 'build/styles/osmd_block.css';
wp_register_style(
'phonicscore_opensheetmusicdisplay_block_editor',
plugins_url( $editor_css, __FILE__ ),
array(),
filemtime( "$dir/$editor_css" )
);
$style_css = 'build/styles/style-osmd_block.css';
wp_register_style(
'phonicscore_opensheetmusicdisplay_block',
plugins_url( $style_css, __FILE__ ),
array(),
filemtime( "$dir/$style_css" )
);
register_block_type(
'phonicscore/opensheetmusicdisplay',
array(
'editor_script' => 'phonicscore_opensheetmusicdisplay_block_editor',
'editor_style' => 'phonicscore_opensheetmusicdisplay_block_editor',
'style' => 'phonicscore_opensheetmusicdisplay_block',
'render_callback' => 'phonicscore_opensheetmusicdisplay_render_callback',
'attributes' => phonicscore_opensheetmusicdisplay_base_attributes
)
);
}
function phonicscore_opensheetmusicdisplay_get_attributes_list() {
$attributes = array(
'alignRests' => array(
'type' => 'number',
'default' => 0
),
'autoBeam' => array(
'type' => 'boolean',
'default' => false
),
'autoBeamOptions' => array(
'type' => 'object',
'default' => array(
'beam_middle_rests_only' => false,
'beam_rests' => false,
'maintain_stem_directions' => false
)
),
'autoResize' => array(
'type' => 'boolean',
'default' => true
),
'backend' => array(
'type' => 'string',
'default' => 'svg'
),
'coloringMode' => array(
'type' => 'number',
'default' => null
),
'coloringSetCustom' => array(
'type' => 'array',
'default' => null
),
'coloringEnabled' => array(
'type' => 'boolean',
'default' => true
),
'colorStemsLikeNoteheads' => array(
'type' => 'boolean',
'default' => false
),
'defaultColorNotehead' => array(
'type' => 'string',
'default' => null
),
'defaultColorStem' => array(
'type' => 'string',
'default' => null
),
'defaultColorRest' => array(
'type' => 'string',
'default' => null
),
'defaultColorLabel' => array(
'type' => 'string',
'default' => null
),
'defaultColorTitle' => array(
'type' => 'string',
'default' => null
),
'defaultFontFamily' => array(
'type' => 'string',
'default' => null
),
'defaultFontStyle' => array(
'type' => 'number',
'default' => 0
),
'disableCursor' => array(
'type' => 'boolean',
'default' => true
),
'followCursor' => array(
'type' => 'boolean',
'default' => false
),
'drawingParameters' => array(
'type' => 'string',
'default' => null
),
'drawCredits' => array(
'type' => 'boolean',
'default' => true
),
'drawTitle' => array(
'type' => 'boolean',
'default' => true
),
'drawSubtitle' => array(
'type' => 'boolean',
'default' => true
),
'drawComposer' => array(
'type' => 'boolean',
'default' => true
),
'drawLyricist' => array(
'type' => 'boolean',
'default' => true
),
'drawMetronomeMarks' => array(
'type' => 'boolean',
'default' => true
),
'drawPartNames' => array(
'type' => 'boolean',
'default' => true
),
'drawPartAbbreviations' => array(
'type' => 'boolean',
'default' => true
),
'drawMeasureNumbers' => array(
'type' => 'boolean',
'default' => true
),
'drawMeasureNumbersOnlyAtSystemStart' => array(
'type' => 'boolean',
'default' => false
),
'drawTimeSignatures' => array(
'type' => 'boolean',
'default' => true
),
'measureNumberInterval' => array(
'type' => 'number',
'default' => 2
),
'useXMLMeasureNumbers' => array(
'type' => 'boolean',
'default' => true
),
'drawFingerings' => array(
'type' => 'boolean',
'default' => true
),
'fingeringPosition' => array(
'type' => 'string',
'default' => null
),
'fingeringInsideStafflines' => array(
'type' => 'boolean',
'default' => false
),
'drawLyrics' => array(
'type' => 'boolean',
'default' => true
),
'drawSlurs' => array(
'type' => 'boolean',
'default' => true
),
'drawUpToMeasureNumber' => array(
'type' => 'number',
'default' => null
),
'drawUpToSystemNumber' => array(
'type' => 'number',
'default' => null
),
'drawUpToPageNumber' => array(
'type' => 'number',
'default' => null
),
'drawFromMeasureNumber' => array(
'type' => 'number',
'default' => null
),
'fillEmptyMeasuresWithWholeRest' => array(
'type' => 'number',
'default' => 0
),
'setWantedStemDirectionByXml' => array(
'type' => 'boolean',
'default' => true
),
'tupletsRatioed' => array(
'type' => 'boolean',
'default' => false
),
'tupletsBracketed' => array(
'type' => 'boolean',
'default' => false
),
'tripletsBracketed' => array(
'type' => 'boolean',
'default' => false
),
'pageFormat' => array(
'type' => 'string',
'default' => null
),
'pageBackgroundColor' => array(
'type' => 'string',
'default' => null
),
'renderSingleHorizontalStaffline' => array(
'type' => 'boolean',
'default' => false
),
'newSystemFromXML' => array(
'type' => 'boolean',
'default' => false
),
'newPageFromXML' => array(
'type' => 'boolean',
'default' => false
),
'percussionOneLineCutoff' => array(
'type' => 'number',
'default' => 4
),
'percussionForceVoicesOneLineCutoff' => array(
'type' => 'number',
'default' => 3
),
'spacingFactorSoftmax' => array(
'type' => 'number',
'default' => 5
),
'spacingBetweenTextLines' => array(
'type' => 'number',
'default' => null
),
'stretchLastSystemLine' => array(
'type' => 'boolean',
'default' => false
),
'autoGenerateMutipleRestMeasuresFromRestMeasures' => array(
'type' => 'boolean',
'default' => true
),
'width' => array(
'type' => 'number',
'default' => 100.0
),
'aspectRatio' => array(
'type' => 'number',
'default' => 0.0
),
'zoom' => array(
'type' => 'number',
'default' => 1.0
),
'musicXmlId' =>array(
'type' => 'number',
'default' => -1
),
'musicXmlUrl' => array(
'type' => 'string',
'default' => null
),
'musicXmlTitle' => array(
'type' => 'string',
'default' => null
)
);
$attributes = apply_filters('phonicscore/opensheetmusicdisplay/block-attributes', $attributes);
return $attributes;
}
function phonicscore_opensheetmusicdisplay_get_user_set_defaults(){
$phonicscore_opensheetmusicdisplay_user_set_defaults = get_option( 'phonicscore_opensheetmusicdisplay_default_settings_option_name' ); // Array of All Options
$attributes = array();
if(is_array($phonicscore_opensheetmusicdisplay_user_set_defaults)){
foreach(phonicscore_opensheetmusicdisplay_base_attributes as $key => $value){
if(!array_key_exists($key, phonicscore_opensheetmusicdisplay_settings_page_defaults)){
continue;
}
switch($value['type']){
case 'boolean':
if(array_key_exists($key, $phonicscore_opensheetmusicdisplay_user_set_defaults) && $phonicscore_opensheetmusicdisplay_user_set_defaults[$key]
&& $value['default'] === false){
$attributes[$key]['default'] = true;
} else if((!array_key_exists($key, $phonicscore_opensheetmusicdisplay_user_set_defaults) || !$phonicscore_opensheetmusicdisplay_user_set_defaults[$key]) && $value['default'] === true){
$attributes[$key]['default'] = false;
}
break;
default:
if(array_key_exists($key, $phonicscore_opensheetmusicdisplay_user_set_defaults) &&
$phonicscore_opensheetmusicdisplay_user_set_defaults[$key] != $value['default']){
$attributes[$key]['default'] = $phonicscore_opensheetmusicdisplay_user_set_defaults[$key];
}
break;
}
}
}
return $attributes;
}
function phonicscore_opensheetmusicdisplay_get_processed_defaults(){
$attributes = phonicscore_opensheetmusicdisplay_base_attributes;
foreach(phonicscore_opensheetmusicdisplay_user_set_defaults as $key => $value){
$attributes[$key]['default'] = $value['default'];
}
return $attributes;
}
function phonicscore_opensheetmusicdisplay_render_callback($block_attributes, $content){
$asJson = wp_json_encode($block_attributes, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_NUMERIC_CHECK | JSON_HEX_QUOT | JSON_HEX_TAG);
$width = '100%';
$className = '';
if(is_array($block_attributes)){
if(array_key_exists('width', $block_attributes)){
$width = $block_attributes['width'] . '%';
}
if(array_key_exists('className', $block_attributes)){
$className = $block_attributes['className'];
}
}
return
<<<EOT
<div class="phonicscore-opensheetmusicdisplay__placeholder $className" style="width: $width !important; max-width: $width !important;">
<div class="phonicscore-opensheetmusicdisplay__loading-spinner hide"></div>
<div class="phonicscore-opensheetmusicdisplay__render-block" style="width: $width !important; max-width: $width !important;"></div>
<code style="display:none;" class="attributesAsJson">$asJson</code>
</div>
EOT;
}
function phonicscore_opensheetmusicdisplay_shortcode_callback($shortCodeAtts, $content, $name) {
foreach (phonicscore_opensheetmusicdisplay_processed_defaults as $key => $value) {
//Shortcode have their atts automatically lower cased. We can't do that though because of omsd option names.
//Find them in the shortcode atts and adapt them before passing to render function.
$keyLowerCase = strtolower($key);
if(array_key_exists($keyLowerCase, $shortCodeAtts) ) {
switch ($value['type']) {
case 'number':
$shortCodeAtts[$key] = floatval($shortCodeAtts[$keyLowerCase]);
break;
case 'boolean':
if($shortCodeAtts[$keyLowerCase] == 'true'){
$shortCodeAtts[$key] = true;
} else {
$shortCodeAtts[$key] = false;
}
break;
case 'array':
case 'object':
$shortCodeAtts[$key] = json_decode($shortCodeAtts[$keyLowerCase]);
break;
default:
$shortCodeAtts[$key] = $shortCodeAtts[$keyLowerCase];
break;
}
if($key != $keyLowerCase) {
unset($shortCodeAtts[$keyLowerCase]);
}
} else if($value['default'] != null) {//else use the (valid) default value
$shortCodeAtts[$key] = $value['default'];
}
}
return phonicscore_opensheetmusicdisplay_render_callback($shortCodeAtts, $content);
}
function phonicscore_opensheetmusicdisplay_shortcode_init(){
add_shortcode('opensheetmusicdisplay', 'phonicscore_opensheetmusicdisplay_shortcode_callback');
}
function phonicscore_opensheetmusicdisplay_enqueue_scripts(){
wp_enqueue_script(
'phonicscore_opensheetmusicdisplay_opensheetmusicdisplay_dist',
esc_url( plugins_url( 'build/osmd/opensheetmusicdisplay.min.js', __FILE__ ) ),
array( ),
'1.8.5',
true
);
wp_enqueue_script(
'phonicscore_opensheetmusicdisplay_opensheetmusicdisplay_block_exports',
esc_url( plugins_url( 'build/osmd/opensheetmusicdisplay-wordpress-block.min.js', __FILE__ ) ),
array( ),
'0.1.1',
true
);
wp_enqueue_script(
'phonicscore_opensheetmusicdisplay_frontend_script',
esc_url( plugins_url( 'build/osmd/osmd-loader.min.js', __FILE__ ) ),
array( 'phonicscore_opensheetmusicdisplay_opensheetmusicdisplay_dist', 'phonicscore_opensheetmusicdisplay_opensheetmusicdisplay_block_exports' ),
'1.1.7',
true
);
}
function phonicscore_opensheetmusicdisplay_enqueue_admin_scripts($hook){
if ( 'post.php' != $hook && 'post-new.php' != $hook ) {
return;
}
wp_enqueue_script(
'phonicscore_opensheetmusicdisplay_opensheetmusicdisplay_dist',
esc_url( plugins_url( 'build/osmd/opensheetmusicdisplay.min.js', __FILE__ ) ),
array( ),
'1.8.5',
true
);
wp_enqueue_script(
'phonicscore_opensheetmusicdisplay_opensheetmusicdisplay_block_exports',
esc_url( plugins_url( 'build/osmd/opensheetmusicdisplay-wordpress-block.min.js', __FILE__ ) ),
array( ),
'0.1.1',
true
);
wp_add_inline_script(
'phonicscore_opensheetmusicdisplay_opensheetmusicdisplay_block_exports',
phonicscore_opensheetmusicdisplay_generate_admin_client_attributes(),
);
wp_enqueue_script(
'fredmeister77_queueable_attributes_dist',
esc_url( plugins_url( 'build/osmd/queueable_attributes.min.js', __FILE__ ) ),
array( ),
'0.1.1',
true
);
wp_enqueue_script(
'phonicscore_opensheetmusicdisplay_opensheetmusicdisplay_block_detection',
esc_url( plugins_url( 'build/osmd/new_block_detection.min.js', __FILE__ ) ),
array( ),
'0.0.1',
true
);
}
function phonicscore_opensheetmusicdisplay_musicxml_mime_types( $mimes ) {
$mimes['musicxml'] = 'application/vnd.recordare.musicxml+xml';
$mimes['mxl'] = 'application/vnd.recordare.musicxml';
$mimes['xml'] = 'text/xml|application/xml';
return $mimes;
}
add_filter( 'upload_mimes', 'phonicscore_opensheetmusicdisplay_musicxml_mime_types', 98 );
include_once 'MultipleMimes.php';
phonicscore_opensheetmusicdisplay_MultipleMimes::init();
function phonicscore_opensheetmusicdisplay_admin_notices() {
?>
<div class="notice notice-warning is-dismissible">
<p><?php _e( '<strong>Heads up!</strong> The OpenSheetMusicDisplay Block will no longer automatically rerender on resize in the editor. It will still automatically resize for the frontend.', 'phonicscore_opensheetmusicdisplay' ); ?></p>
</div>
<?php
}
function phonicscore_opensheetmusicdisplay_activate_plugin(){
if(function_exists('register_block_type')){
add_action( 'init', 'phonicscore_opensheetmusicdisplay_block_init' );
}
add_action( 'init', 'phonicscore_opensheetmusicdisplay_shortcode_init' );
add_action( 'wp_enqueue_scripts', 'phonicscore_opensheetmusicdisplay_enqueue_scripts' );
if(function_exists('register_block_type')){
add_action( 'admin_enqueue_scripts', 'phonicscore_opensheetmusicdisplay_enqueue_admin_scripts' );
if (current_user_can('edit_posts') && is_admin()){
$resizeUpdateMessageViewed = get_user_meta(get_current_user_id(), 'phonicscore_opensheetmusicdisplay_resize_update_message_viewed', true);
if($resizeUpdateMessageViewed === '' || $resizeUpdateMessageViewed == false){
add_action( 'admin_notices', 'phonicscore_opensheetmusicdisplay_admin_notices' );
update_user_meta(get_current_user_id(), 'phonicscore_opensheetmusicdisplay_resize_update_message_viewed', true);
}
}
}
}
add_action('plugins_loaded', 'phonicscore_opensheetmusicdisplay_activate_plugin', 10);
include_once 'practicebird_block.php';
?>