API
1 month ago
Blocks
10 months ago
License
1 month ago
AdminNotice.php
3 weeks ago
AdminNotices.php
3 weeks ago
AjaxActions.php
3 weeks ago
Blocks.php
1 year ago
Compatibility.php
10 months ago
Learn.php
1 month ago
Menu.php
1 month ago
Migrations.php
1 year ago
NpsSurvey.php
5 months ago
Onboarding.php
3 weeks ago
Player.php
3 days ago
PluginInstaller.php
1 month ago
PreloadService.php
1 year ago
ProCompatibility.php
1 month ago
ReusableVideos.php
2 weeks ago
RewriteRulesManager.php
1 year ago
Scripts.php
2 weeks ago
Settings.php
1 month ago
Shortcodes.php
1 month ago
Streamer.php
3 weeks ago
Translation.php
3 weeks ago
Usage.php
3 weeks ago
VideoPostType.php
2 weeks ago
ReusableVideos.php
173 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Reusable video admin notice and block renderer. |
| 4 | * |
| 5 | * @package PrestoPlayer |
| 6 | * @subpackage Services |
| 7 | */ |
| 8 | |
| 9 | namespace PrestoPlayer\Services; |
| 10 | |
| 11 | use PrestoPlayer\Services\Scripts; |
| 12 | |
| 13 | /** |
| 14 | * Renders the Media Hub introduction notice and helpers for reusable video blocks. |
| 15 | */ |
| 16 | class ReusableVideos { |
| 17 | |
| 18 | /** |
| 19 | * Register shortcode. |
| 20 | * |
| 21 | * @return void |
| 22 | */ |
| 23 | public function register() { |
| 24 | add_action( 'admin_notices', array( $this, 'notice' ) ); |
| 25 | add_action( 'admin_init', array( $this, 'dismissNotice' ) ); |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * Render the Media Hub admin notice on the reusable video list screen. |
| 30 | * |
| 31 | * @return void |
| 32 | */ |
| 33 | public function notice() { |
| 34 | global $typenow, $current_screen; |
| 35 | |
| 36 | if ( ! ( 'pp_video_block' === $typenow && 'edit' === $current_screen->base ) ) { |
| 37 | return; |
| 38 | } |
| 39 | |
| 40 | $notice_name = 'presto_player_reusable_notice'; |
| 41 | if ( AdminNotices::isDismissed( $notice_name ) ) { |
| 42 | return; |
| 43 | } |
| 44 | ?> |
| 45 | <div class="notice" style="border-left-color: #7c3aed;"> |
| 46 | <p><strong><?php esc_html_e( 'What is the media hub?', 'presto-player' ); ?></strong></p> |
| 47 | <p><?php esc_html_e( 'The media hub is a more flexible way to add media to your site. It allows you to save audio and videos which you can later use in any post or page on your site - either through the Block Editor, a page builder, or by using a shortcode or php function.', 'presto-player' ); ?></p> |
| 48 | <p><a href=" |
| 49 | <?php |
| 50 | echo esc_url( |
| 51 | add_query_arg( |
| 52 | array( |
| 53 | 'presto_action' => 'dismiss_notices', |
| 54 | 'presto_notice' => $notice_name, |
| 55 | '_wpnonce' => wp_create_nonce( AdminNotices::DISMISS_NONCE_ACTION ), |
| 56 | ) |
| 57 | ) |
| 58 | ); |
| 59 | ?> |
| 60 | "><?php esc_html_e( 'Dismiss Notice', 'presto-player' ); ?></a></p> |
| 61 | </div> |
| 62 | <?php |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Persist the dismissal of the Media Hub notice on admin_init. |
| 67 | * |
| 68 | * @return void |
| 69 | */ |
| 70 | public function dismissNotice() { |
| 71 | // Permissions check. |
| 72 | if ( ! current_user_can( 'update_options' ) ) { |
| 73 | return; |
| 74 | } |
| 75 | |
| 76 | // Not our notices, bail. |
| 77 | if ( ! isset( $_GET['presto_action'] ) || 'dismiss_notice' !== $_GET['presto_action'] ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only branch guard; nonce verified before update below. |
| 78 | return; |
| 79 | } |
| 80 | |
| 81 | $notice = ! empty( $_GET['presto_notice'] ) ? sanitize_text_field( wp_unslash( $_GET['presto_notice'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Nonce verified before update below. |
| 82 | |
| 83 | if ( ! $notice ) { |
| 84 | return; |
| 85 | } |
| 86 | |
| 87 | // Notice is dismissed. |
| 88 | update_option( 'presto_player_dismissed_notice_' . sanitize_text_field( $notice ), 1 ); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Get reusable video block function. |
| 93 | * |
| 94 | * @param mixed $id The ID of the reusable block. |
| 95 | * @return string The content of the block. |
| 96 | */ |
| 97 | public static function get( $id ) { |
| 98 | $content_post = get_post( $id ); |
| 99 | $content = $content_post ? $content_post->post_content : ''; |
| 100 | return $content; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Render a reusable block by id. |
| 105 | * |
| 106 | * @param int $id The ID of the reusable block. |
| 107 | * @return string Rendered HTML. |
| 108 | */ |
| 109 | public static function getBlock( $id ) { |
| 110 | $blocks = parse_blocks( self::get( $id ) ); |
| 111 | $out = ''; |
| 112 | if ( is_feed() ) { |
| 113 | foreach ( $blocks as $block ) { |
| 114 | foreach ( $block['innerBlocks'] as $innerblock ) { |
| 115 | $out .= self::getFeedHtml( $innerblock ); |
| 116 | } |
| 117 | } |
| 118 | return $out; |
| 119 | } |
| 120 | |
| 121 | foreach ( $blocks as $block ) { |
| 122 | $out .= render_block( $block ); |
| 123 | } |
| 124 | |
| 125 | return $out; |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Display block function. |
| 130 | * |
| 131 | * @param mixed $id The ID of the reusable block. |
| 132 | */ |
| 133 | public static function display( $id ) { |
| 134 | echo self::getBlock( $id ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Output is rendered block HTML from core's render_block(). |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Return fallback html for feeds. |
| 139 | * |
| 140 | * @param array $block array of block attributes. |
| 141 | */ |
| 142 | public static function getFeedHtml( $block ) { |
| 143 | |
| 144 | $html = ''; |
| 145 | if ( 'presto-player/vimeo' === $block['blockName'] ) { |
| 146 | ob_start(); |
| 147 | ?> |
| 148 | <div class="presto-iframe-fallback-container"> |
| 149 | <iframe style="width: 100%" class="presto-fallback-iframe" id="presto-iframe-fallback-<?php echo (int) $block['attrs']['id']; ?>" src="<?php echo esc_attr( $block['attrs']['src'] ); ?>"></iframe> |
| 150 | </div> |
| 151 | <?php |
| 152 | $html = ob_get_clean(); |
| 153 | } elseif ( 'presto-player/youtube' === $block['blockName'] ) { |
| 154 | ob_start(); |
| 155 | ?> |
| 156 | <div class="presto-iframe-fallback-container"> |
| 157 | <iframe style="width: 100%" class="presto-fallback-iframe" id="presto-iframe-fallback-<?php echo (int) $block['attrs']['id']; ?>" src="<?php echo esc_attr( $block['attrs']['src'] ); ?>"></iframe> |
| 158 | </div> |
| 159 | <?php |
| 160 | $html = ob_get_clean(); |
| 161 | } else { |
| 162 | ob_start(); |
| 163 | ?> |
| 164 | <video controls preload="none"> |
| 165 | <source src="<?php echo esc_attr( $block['attrs']['src'] ); ?>" /> |
| 166 | </video> |
| 167 | <?php |
| 168 | $html = ob_get_clean(); |
| 169 | } |
| 170 | return $html; |
| 171 | } |
| 172 | } |
| 173 |