Skip to content

Commit b7cdaae

Browse files
committed
Render the GoDAM player from attributes instead of a built shortcode string
The video embed page, the video-shortcode REST route, the GoDAM Gallery Elementor widget, the preview page and the entry screens of the Fluent Forms, WPForms, Everest Forms, SureForms and Gravity Forms recorder fields now hand their values to the player as an attributes array. A new helper, rtgodam_render_shortcode_from_atts(), calls the registered shortcode callback directly, so a value is only ever an attribute value and is never parsed as shortcode syntax. - The embed page and the route reduce godam_context and block_source to slug characters with sanitize_key(); the route argument uses sanitize_key too. - Stored recording and transcoded URLs are passed through esc_url_raw(). - The route gets a rtgodam_shortcode_atts filter for the attributes array. The rtgodam_shortcode_output filter, which receives the shortcode string, is deprecated but still applied when a site uses it; every value in that string is now an ID, a slug, 'show'/'hide' or JSON. The rendered player HTML for normal values is unchanged. Adds unit tests for the helper, the embed and preview pages, the route, the Elementor gallery widget and each form plugin's entry view. The tests register a harmless shortcode and check that extra shortcode text in a value does not run it, and that a normal value still reaches the player.
1 parent 9938049 commit b7cdaae

22 files changed

Lines changed: 2039 additions & 46 deletions

‎inc/classes/elementor-widgets/class-godam-gallery.php‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -502,12 +502,17 @@ protected function render() {
502502
);
503503
}
504504

505-
$shortcode_atts_string = '';
506-
foreach ( $shortcode_atts as $key => $value ) {
507-
$shortcode_atts_string .= sprintf( ' %s="%s"', $key, esc_attr( $value ) );
508-
}
505+
// Pass the settings as an attributes array rather than building a
506+
// "[godam_video_gallery …]" string, so a setting value is never parsed as
507+
// shortcode syntax. Values stay strings, as the shortcode parser delivers them.
508+
$shortcode_atts = array_map(
509+
function ( $value ) {
510+
return is_scalar( $value ) ? (string) $value : '';
511+
},
512+
$shortcode_atts
513+
);
509514

510-
echo do_shortcode( '[godam_video_gallery' . $shortcode_atts_string . ']' );
515+
echo rtgodam_render_shortcode_from_atts( 'godam_video_gallery', $shortcode_atts ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- The gallery template escapes its output.
511516
}
512517

513518
/**

‎inc/classes/everest-forms/class-everest-forms-field-godam-video.php‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,12 +128,14 @@ public function update_entry_meta_display_godam_recorder( $meta_value, $value, $
128128

129129
$transcoded_url_meta_key = 'rtgodam_transcoded_url_everestforms_' . $form_id . '_' . $entry_id;
130130
$transcoded_url_output = '';
131+
$transcoded_url_source = '';
132+
$transcoded_url = '';
131133

132134
if ( ! empty( $entry_data ) ) {
133-
$transcoded_url = esc_url( $entry_data->meta[ $transcoded_url_meta_key ] ?? '' );
135+
$transcoded_url_source = $entry_data->meta[ $transcoded_url_meta_key ] ?? '';
136+
$transcoded_url = esc_url( $transcoded_url_source );
134137

135138
if ( ! empty( $transcoded_url ) ) {
136-
$transcoded_url = "transcoded_url={$transcoded_url}";
137139
$transcoded_url_output = sprintf(
138140
"<div style='margin: 8px 0;' class='godam-transcoded-url-info'><span class='dashicons dashicons-yes-alt'></span><strong>%s</strong></div>",
139141
$is_audio
@@ -151,8 +153,7 @@ public function update_entry_meta_display_godam_recorder( $meta_value, $value, $
151153
if ( $is_audio ) {
152154
$audio_output = '<audio controls style="width: 100%; margin-top: 10px;">';
153155
if ( ! empty( $transcoded_url ) ) {
154-
$audio_src = str_replace( 'transcoded_url=', '', $transcoded_url );
155-
$audio_output .= sprintf( '<source src="%s" type="audio/mpeg">', esc_url( $audio_src ) );
156+
$audio_output .= sprintf( '<source src="%s" type="audio/mpeg">', esc_url( $transcoded_url ) );
156157
}
157158
$audio_output .= sprintf( '<source src="%s" type="%s">', esc_url( $value ), esc_attr( $mime_type ) );
158159
$audio_output .= esc_html__( 'Your browser does not support the audio element.', 'godam' );
@@ -164,7 +165,13 @@ public function update_entry_meta_display_godam_recorder( $meta_value, $value, $
164165

165166
$media_output = $audio_output;
166167
} else {
167-
$video_output = do_shortcode( "[godam_video src='{$value}' {$transcoded_url} ]" );
168+
$video_output = rtgodam_render_shortcode_from_atts(
169+
'godam_video',
170+
array(
171+
'src' => esc_url_raw( $value ),
172+
'transcoded_url' => esc_url_raw( $transcoded_url_source ),
173+
)
174+
);
168175

169176
// Workaround, replace all line breaks and new lines with empty string.
170177
$video_output = str_replace( array( "\r", "\n" ), '', $video_output );

‎inc/classes/fluentforms/fields/class-recorder-field.php‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -847,7 +847,7 @@ public function render_recorder_field_markup( $response, $field, $form_id, $is_h
847847
$transcoded_url = '';
848848

849849
if ( ! empty( $submission_meta ) ) {
850-
$transcoded_url = esc_url( $submission_meta->value );
850+
$transcoded_url = esc_url_raw( $submission_meta->value );
851851
$transcoded_url_output = sprintf(
852852
"<div style='margin: 8px 0;' class='godam-transcoded-url-info'><span class='dashicons dashicons-yes-alt'></span><strong>%s</strong></div>",
853853
esc_html__( 'Video saved and transcoded successfully on GoDAM', 'godam' )
@@ -857,7 +857,14 @@ public function render_recorder_field_markup( $response, $field, $form_id, $is_h
857857
/**
858858
* Generate video output.
859859
*/
860-
$video_output = do_shortcode( "[godam_video src='{$video_url}' transcoded_url='{$transcoded_url}' aspectRatio='']" );
860+
$video_output = rtgodam_render_shortcode_from_atts(
861+
'godam_video',
862+
array(
863+
'src' => esc_url_raw( $video_url ),
864+
'transcoded_url' => $transcoded_url,
865+
'aspectratio' => '',
866+
)
867+
);
861868
$video_output = '<div class="gf-godam-video-preview">' . $video_output . '</div>';
862869

863870
$download_url = sprintf(

‎inc/classes/gravity-forms/class-gf-field-godam-video.php‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -423,10 +423,17 @@ public function get_value_entry_detail( $value, $currency = '', $use_text = fals
423423
if ( $is_video ) {
424424

425425
if ( $transcoded_url ) {
426-
$transcoded_url = esc_url( $transcoded_url );
426+
$transcoded_url = esc_url_raw( $transcoded_url );
427427
}
428428

429-
$video_output = do_shortcode( "[godam_video src='{$file_path}' transcoded_url='{$transcoded_url}' aspectRatio='']" );
429+
$video_output = rtgodam_render_shortcode_from_atts(
430+
'godam_video',
431+
array(
432+
'src' => esc_url_raw( $file_path ),
433+
'transcoded_url' => (string) $transcoded_url,
434+
'aspectratio' => '',
435+
)
436+
);
430437
$video_output = '<div class="gf-godam-video-preview">' . $video_output . '</div>';
431438
$output_arr[] = $video_output;
432439
}

‎inc/classes/rest-api/class-dynamic-shortcode.php‎

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function get_rest_routes() {
5858
'required' => false,
5959
'type' => 'string',
6060
'default' => '',
61-
'sanitize_callback' => 'sanitize_text_field',
61+
'sanitize_callback' => 'sanitize_key',
6262
),
6363
),
6464
),
@@ -77,7 +77,8 @@ public function render_shortcode( WP_REST_Request $request ) {
7777

7878
$engagements = $request->get_param( 'engagements' );
7979

80-
$godam_context = $request->get_param( 'godam_context' );
80+
// Slug characters only, whatever reaches this method.
81+
$godam_context = sanitize_key( (string) $request->get_param( 'godam_context' ) );
8182

8283
/**
8384
* Fires before resolving this attachment's URL/meta/sources, so
@@ -128,9 +129,7 @@ public function render_shortcode( WP_REST_Request $request ) {
128129
'type' => 'video/quicktime' === $video_src_type ? 'video/mp4' : $video_src_type,
129130
);
130131

131-
// Convert JSON to use custom placeholders instead of square brackets.
132-
$sources_json = wp_json_encode( $sources );
133-
$sources_with_placeholders = str_replace( array( '[', ']' ), array( '__rtgob__', '__rtgcb__' ), $sources_json );
132+
$sources_json = wp_json_encode( $sources );
134133

135134
// Add action before shortcode rendering.
136135
do_action( 'rtgodam_shortcode_before_render', $id );
@@ -161,18 +160,53 @@ public function render_shortcode( WP_REST_Request $request ) {
161160
$video_date = apply_filters( 'rtgodam_shortcode_video_date', $video_date, $id );
162161

163162
ob_start();
164-
$shortcode = "[godam_video id='{$id}' engagements='" . esc_attr( $engagements ) . "' sources='{$sources_with_placeholders}'";
163+
$atts = array(
164+
'id' => (string) $id,
165+
'engagements' => (string) $engagements,
166+
'sources' => $sources_json,
167+
);
165168

166169
if ( ! empty( $godam_context ) ) {
167-
$shortcode .= " godam_context='" . esc_attr( $godam_context ) . "'";
170+
$atts['godam_context'] = $godam_context;
168171
}
169172

170-
$shortcode .= ']';
173+
/**
174+
* Filters the attributes the player is rendered with.
175+
*
176+
* @since n.e.x.t
177+
*
178+
* @param array $atts Attributes passed to the [godam_video] renderer.
179+
* @param int $id Attachment ID.
180+
*/
181+
$atts = apply_filters( 'rtgodam_shortcode_atts', $atts, $id );
182+
183+
if ( has_filter( 'rtgodam_shortcode_output' ) ) {
184+
// Keep sites that still change the shortcode string working. Every value in it is
185+
// an ID, a slug, 'show'/'hide' or JSON with its brackets swapped for placeholders.
186+
$shortcode = "[godam_video id='" . absint( $atts['id'] ?? $id ) . "' engagements='" . esc_attr( $atts['engagements'] ?? '' ) . "' sources='" . str_replace( array( '[', ']' ), array( '__rtgob__', '__rtgcb__' ), (string) ( $atts['sources'] ?? '' ) ) . "'";
171187

172-
// Add filter for shortcode.
173-
$shortcode = apply_filters( 'rtgodam_shortcode_output', $shortcode, $id );
188+
if ( ! empty( $atts['godam_context'] ) ) {
189+
$shortcode .= " godam_context='" . esc_attr( $atts['godam_context'] ) . "'";
190+
}
191+
192+
$shortcode .= ']';
193+
194+
/**
195+
* Filters the shortcode string the player is rendered from.
196+
*
197+
* @since 1.1.4
198+
* @deprecated n.e.x.t Use the rtgodam_shortcode_atts filter instead.
199+
*
200+
* @param string $shortcode The [godam_video] shortcode.
201+
* @param int $id Attachment ID.
202+
*/
203+
$shortcode = apply_filters_deprecated( 'rtgodam_shortcode_output', array( $shortcode, $id ), 'n.e.x.t', 'rtgodam_shortcode_atts' );
204+
205+
echo do_shortcode( $shortcode );
206+
} else {
207+
echo rtgodam_render_shortcode_from_atts( 'godam_video', $atts ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- The player template escapes its output.
208+
}
174209

175-
echo do_shortcode( $shortcode );
176210
$html = ob_get_clean();
177211

178212
// Add filter for final HTML output.

‎inc/classes/sureforms/class-form-submit.php‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -348,8 +348,6 @@ public function render_custom_field_markup( $markup, $value ) {
348348
$transcoded_url_output = '';
349349

350350
if ( ! empty( $transcoded_url ) ) {
351-
$transcoded_url = esc_url( $transcoded_url );
352-
$transcoded_url = "transcoded_url={$transcoded_url}";
353351
$transcoded_url_output = sprintf(
354352
"<div style='margin: 8px 0;' class='godam-transcoded-url-info'><span class='dashicons dashicons-yes-alt'></span><strong>%s</strong></div>",
355353
$is_audio
@@ -368,15 +366,21 @@ public function render_custom_field_markup( $markup, $value ) {
368366
if ( $is_audio ) {
369367
$audio_output = '<audio controls>';
370368
if ( ! empty( $transcoded_url ) ) {
371-
$audio_output .= sprintf( '<source src="%s" type="audio/mpeg">', esc_url( str_replace( 'transcoded_url=', '', $transcoded_url ) ) );
369+
$audio_output .= sprintf( '<source src="%s" type="audio/mpeg">', esc_url( $transcoded_url ) );
372370
}
373371
$audio_output .= sprintf( '<source src="%s" type="%s">', esc_url( $value ), esc_attr( $file_type['type'] ) );
374372
$audio_output .= esc_html__( 'Your browser does not support the audio element.', 'godam' );
375373
$audio_output .= '</audio>';
376374

377375
$media_output = '<div class="srfm-godam-audio-preview">' . $audio_output . '</div>';
378376
} else {
379-
$video_output = do_shortcode( "[godam_video src='{$value}' {$transcoded_url} ]" );
377+
$video_output = rtgodam_render_shortcode_from_atts(
378+
'godam_video',
379+
array(
380+
'src' => esc_url_raw( $value ),
381+
'transcoded_url' => esc_url_raw( (string) $transcoded_url ),
382+
)
383+
);
380384
$media_output = '<div class="srfm-godam-video-preview">' . $video_output . '</div>';
381385
}
382386

‎inc/classes/wpforms/wpforms-field-godam-record-entry-edit.php‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,17 @@
4848
<?php esc_html_e( 'Your browser does not support the audio element.', 'godam' ); ?>
4949
</audio>
5050
<?php else : ?>
51-
<?php echo do_shortcode( "[godam_video poster='{$godam_thumbnail_url}' src='{$godam_value}' transcoded_url='{$godam_transcoded_url}']" ); ?>
51+
<?php
52+
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- The player template escapes its output.
53+
echo rtgodam_render_shortcode_from_atts(
54+
'godam_video',
55+
array(
56+
'poster' => esc_url_raw( $godam_thumbnail_url ),
57+
'src' => esc_url_raw( $godam_value ),
58+
'transcoded_url' => esc_url_raw( $godam_transcoded_url ),
59+
)
60+
);
61+
?>
5262
<?php endif; ?>
5363
</div>
5464
<a

‎inc/classes/wpforms/wpforms-field-godam-record-entry-view.php‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,16 @@ class="godam-video-link <?php echo ( empty( $value ) ? 'hidden' : '' ); ?>"
8484
// No need to escape here, the entire template will be returned as strings,
8585
// which will be later on escaped using wp_kses_post() by WPForms before rendering the field.
8686
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
87-
echo force_balance_tags( do_shortcode( "[godam_video poster='{$godam_thumbnail_url}' src='{$godam_attachment_url}' transcoded_url='{$godam_transcoded_url}']" ) );
87+
echo force_balance_tags(
88+
rtgodam_render_shortcode_from_atts(
89+
'godam_video',
90+
array(
91+
'poster' => esc_url_raw( $godam_thumbnail_url ),
92+
'src' => esc_url_raw( $godam_attachment_url ),
93+
'transcoded_url' => esc_url_raw( (string) $godam_transcoded_url ),
94+
)
95+
)
96+
);
8897
?>
8998
<?php endif; ?>
9099
</div>

‎inc/helpers/custom-functions.php‎

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2192,10 +2192,10 @@ function godam_preview_page_content( $video_id ) {
21922192
) . ' /-->'
21932193
);
21942194
} else {
2195-
$godam_shortcode = $godam_is_audio
2196-
? '[godam_audio id="' . $video_id . '"]'
2197-
: '[godam_video id="' . $video_id . '"]';
2198-
$godam_media_output = do_shortcode( $godam_shortcode );
2195+
$godam_media_output = rtgodam_render_shortcode_from_atts(
2196+
$godam_is_audio ? 'godam_audio' : 'godam_video',
2197+
array( 'id' => (string) $video_id )
2198+
);
21992199
}
22002200
?>
22012201
<div class="godam-video-preview--notice">
@@ -2293,6 +2293,30 @@ function rtgodam_get_block_source_from_context( $context ) {
22932293
return isset( $map[ $context ] ) ? $map[ $context ] : $context;
22942294
}
22952295

2296+
/**
2297+
* Render a registered shortcode from an attributes array.
2298+
*
2299+
* Calls the shortcode's callback directly, so each attribute value is passed to
2300+
* the renderer as plain data and is never parsed as shortcode syntax. Use it
2301+
* instead of do_shortcode() when a value comes from a request, a form
2302+
* submission or other stored user data.
2303+
*
2304+
* @since n.e.x.t
2305+
*
2306+
* @param string $tag Shortcode tag, for example 'godam_video'.
2307+
* @param array<string, mixed> $atts Attributes, keyed in lowercase as the shortcode parser would key them.
2308+
* @return string The shortcode output, or an empty string when the shortcode is not registered or its callback cannot be called.
2309+
*/
2310+
function rtgodam_render_shortcode_from_atts( $tag, $atts = array() ) {
2311+
global $shortcode_tags;
2312+
2313+
if ( ! shortcode_exists( $tag ) || ! is_callable( $shortcode_tags[ $tag ] ) ) {
2314+
return '';
2315+
}
2316+
2317+
return (string) call_user_func( $shortcode_tags[ $tag ], $atts, '', $tag );
2318+
}
2319+
22962320
/**
22972321
* Generate HTML content for the video embed page.
22982322
*
@@ -2301,10 +2325,10 @@ function rtgodam_get_block_source_from_context( $context ) {
23012325
* making it suitable for embedding in iframes or modals.
23022326
*
23032327
* @param int $video_id The ID of the video attachment to embed.
2304-
* @param string $godam_context Optional. The player context passed to the godam_video shortcode (e.g. 'video-only').
2328+
* @param string $godam_context Optional. The player context passed to the godam_video shortcode (e.g. 'video-only'). Reduced to slug characters with sanitize_key().
23052329
* @param string $bg_color Optional. Hex background color applied as --godam-video-bg-color CSS variable on the wrapper.
23062330
* @param bool $show_engagements Optional. Whether to show engagements in the embed. Default false (no engagements shown).
2307-
* @param string $block_source Optional. Analytics placement slug forwarded to the player (e.g. 'video-gallery' for gallery iframes).
2331+
* @param string $block_source Optional. Analytics placement slug forwarded to the player (e.g. 'video-gallery' for gallery iframes). Reduced to slug characters with sanitize_key().
23082332
* @param int $host_post_id Optional. Post ID of the page embedding this iframe, so analytics attribute to the host page.
23092333
*
23102334
* @since 1.5.0
@@ -2319,6 +2343,10 @@ function godam_embed_page_content( $video_id, $godam_context = '', $bg_color = '
23192343
$video_id = intval( $video_id );
23202344
$engagements_value = rtgodam_is_engagement_feature_enabled() && $show_engagements ? 'show' : '';
23212345

2346+
// Both values arrive from the URL; keep them to slug characters.
2347+
$godam_context = sanitize_key( $godam_context );
2348+
$block_source = sanitize_key( $block_source );
2349+
23222350
if ( ! empty( $video_id ) ) {
23232351
/**
23242352
* Fires before resolving this attachment's post/mime data, so
@@ -2342,20 +2370,19 @@ function godam_embed_page_content( $video_id, $godam_context = '', $bg_color = '
23422370
<?php
23432371
} else {
23442372
// Display video content.
2345-
$godam_shortcode = '[godam_video id="' . $video_id . '"';
2373+
$godam_atts = array( 'id' => (string) $video_id );
23462374
if ( ! empty( $godam_context ) ) {
2347-
$godam_shortcode .= ' godam_context="' . esc_attr( $godam_context ) . '"';
2375+
$godam_atts['godam_context'] = $godam_context;
23482376
}
23492377
if ( ! empty( $engagements_value ) ) {
2350-
$godam_shortcode .= ' engagements="' . esc_attr( $engagements_value ) . '"';
2378+
$godam_atts['engagements'] = $engagements_value;
23512379
}
23522380
if ( ! empty( $block_source ) ) {
2353-
$godam_shortcode .= ' block_source="' . esc_attr( $block_source ) . '"';
2381+
$godam_atts['block_source'] = $block_source;
23542382
}
23552383
if ( ! empty( $host_post_id ) ) {
2356-
$godam_shortcode .= ' host_post_id="' . absint( $host_post_id ) . '"';
2384+
$godam_atts['host_post_id'] = (string) absint( $host_post_id );
23572385
}
2358-
$godam_shortcode .= ']';
23592386

23602387
$godam_wrapper_style = ! empty( $bg_color ) ? '--godam-video-bg-color: ' . $bg_color . ';' : '';
23612388
?>
@@ -2364,7 +2391,7 @@ class="godam-video-embed" data-godam-context="<?php echo esc_attr( $godam_contex
23642391
data-show-engagements="<?php echo esc_attr( $show_engagements ? 'true' : 'false' ); ?>"
23652392
style="<?php echo esc_attr( $godam_wrapper_style ); ?>"
23662393
>
2367-
<?php echo do_shortcode( $godam_shortcode ); ?>
2394+
<?php echo rtgodam_render_shortcode_from_atts( 'godam_video', $godam_atts ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- The player template escapes its output. ?>
23682395
</div>
23692396
<?php
23702397
}

‎tests/bootstrap.php‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,10 @@ function set_transient( $key, $value, $expiration = 0 ) { // phpcs:ignore Generi
439439
*/
440440
require_once __DIR__ . '/stubs/hooks.php';
441441
require_once __DIR__ . '/stubs/i18n.php';
442+
// Shortcode registry and runner, so a test can register a shortcode and see whether
443+
// text handed to do_shortcode() runs it. Loaded here so its do_shortcode() is the one
444+
// that every test sees.
445+
require_once __DIR__ . '/stubs/shortcode-runtime.php';
442446
// HTTP / REST stubs for the Analytics microservice-proxy tests (fetch_top_videos
443447
// / fetch_top_products): fake wp_remote_* responses + get_option, and minimal
444448
// WP_REST_Request / WP_REST_Response so the proxy's error-vs-empty guard can be

0 commit comments

Comments
 (0)