block-embedpress.php
236 lines
| 1 | <?php |
| 2 | |
| 3 | use EmbedPress\Includes\Classes\Helper; |
| 4 | |
| 5 | // Exit if accessed directly. |
| 6 | if (!defined('ABSPATH')) { |
| 7 | exit; |
| 8 | } |
| 9 | /** |
| 10 | * It renders gutenberg block of embedpress on the frontend |
| 11 | * @param array $attributes |
| 12 | */ |
| 13 | |
| 14 | if(!function_exists('lock_content_form_handler')){ |
| 15 | add_action('wp_ajax_lock_content_form_handler', 'lock_content_form_handler'); |
| 16 | add_action('wp_ajax_nopriv_lock_content_form_handler', 'lock_content_form_handler'); |
| 17 | |
| 18 | function lock_content_form_handler() { |
| 19 | // print_r($embedHTML); |
| 20 | |
| 21 | $client_id = isset($_POST['client_id']) ? $_POST['client_id'] : ''; |
| 22 | $password = isset($_POST['password']) ? $_POST['password'] : ''; |
| 23 | $epbase64 = isset($_POST['epbase']) ? $_POST['epbase'] : ''; |
| 24 | $hash_key = isset($_POST['hash_key']) ? $_POST['hash_key'] : ''; |
| 25 | |
| 26 | // echo $client_id; |
| 27 | |
| 28 | |
| 29 | // Set the decryption key and initialization vector (IV) |
| 30 | $key = "g72@QKgEcANy8%D7xq8%@n%#"; |
| 31 | $iv = "^ZCC$93vsbyYjz01"; |
| 32 | |
| 33 | // Decode the base64 encoded cipher |
| 34 | $cipher = base64_decode($epbase64); |
| 35 | // Decrypt the cipher using AES-128-CBC encryption |
| 36 | |
| 37 | $wp_pass_key = hash('sha256', wp_salt(32) . md5($password)); |
| 38 | if ($wp_pass_key === $hash_key) { |
| 39 | setcookie("password_correct_", $password, time()+3600); |
| 40 | |
| 41 | $embed = openssl_decrypt($cipher, 'AES-128-CBC', $key, OPENSSL_RAW_DATA, $iv) . '<script> |
| 42 | var now = new Date(); |
| 43 | var time = now.getTime(); |
| 44 | var expireTime = time + 1000 * 60 * 60 * 24 * 30; |
| 45 | now.setTime(expireTime); |
| 46 | document.cookie = "password_correct_'.$client_id.'='.$hash_key.'; expires=" + now.toUTCString() + "; path=/"; |
| 47 | </script>'; |
| 48 | |
| 49 | } |
| 50 | else{ |
| 51 | $embed = 0; |
| 52 | } |
| 53 | |
| 54 | // Process the form data and return a response |
| 55 | $response = array( |
| 56 | 'success' => true, |
| 57 | 'password' => $password, |
| 58 | 'embedHtml' => $embed |
| 59 | ); |
| 60 | |
| 61 | echo json_encode($response); |
| 62 | |
| 63 | wp_die(); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | |
| 68 | function embedpress_render_block($attributes) |
| 69 | { |
| 70 | |
| 71 | $client_id = !empty($attributes['clientId']) ? md5($attributes['clientId']) : ''; |
| 72 | $block_id = !empty($attributes['clientId']) ? $attributes['clientId'] : ''; |
| 73 | |
| 74 | $pass_hash_key = isset($attributes['contentPassword']) ? md5($attributes['contentPassword']): ''; |
| 75 | |
| 76 | if (!empty($attributes['embedHTML'])) { |
| 77 | $embed = apply_filters('embedpress_gutenberg_embed', $attributes['embedHTML'], $attributes); |
| 78 | |
| 79 | $content_share_class = ''; |
| 80 | $share_position_class = ''; |
| 81 | $share_position = isset($attributes['sharePosition']) ? $attributes['sharePosition'] : 'right'; |
| 82 | |
| 83 | if(!empty($attributes['contentShare'])) { |
| 84 | $content_share_class = 'ep-content-share-enabled'; |
| 85 | $share_position_class = 'ep-share-position-'.$share_position; |
| 86 | } |
| 87 | $content_protection_class = ''; |
| 88 | if(!empty($attributes['lockContent']) && !empty($attributes['contentPassword'])) { |
| 89 | $content_protection_class = 'ep-content-protection-enabled'; |
| 90 | } |
| 91 | |
| 92 | $aligns = [ |
| 93 | 'left' => 'alignleft', |
| 94 | 'right' => 'alignright', |
| 95 | 'wide' => 'alignwide', |
| 96 | 'full' => 'alignfull', |
| 97 | 'center' => 'aligncenter', |
| 98 | ]; |
| 99 | if (isset($attributes['align'])) { |
| 100 | $alignment = isset($aligns[$attributes['align']]) ? $aligns[$attributes['align']] . ' clear' : ''; |
| 101 | } else { |
| 102 | $alignment = 'aligncenter'; // default alignment is center in js, so keeping same here |
| 103 | } |
| 104 | $embed = Helper::customLogo($embed, $attributes); |
| 105 | $url = !empty($attributes['href']) ? $attributes['href'] : ''; |
| 106 | |
| 107 | ob_start(); |
| 108 | ?> |
| 109 | <div class="embedpress-gutenberg-wrapper <?php echo esc_attr( $alignment.' '.$content_share_class.' '.$share_position_class.' '.$content_protection_class); ?>" id="<?php echo esc_attr($block_id); ?>"> |
| 110 | <?php |
| 111 | $share_position = isset($attributes['sharePosition']) ? $attributes['sharePosition'] : 'right'; |
| 112 | $custom_thumbnail = isset($attributes['customThumbnail']) ? $attributes['customThumbnail'] : ''; |
| 113 | ?> |
| 114 | <div class="wp-block-embed__wrapper <?php if(!empty($attributes['contentShare'])) echo esc_attr( 'position-'.$share_position.'-wraper'); ?> <?php if($attributes['videosize'] == 'responsive') echo esc_attr( 'ep-video-responsive' ); ?>"> |
| 115 | <div id="ep-gutenberg-content-<?php echo esc_attr( $client_id )?>" class="ep-gutenberg-content"> |
| 116 | <div class="ep-embed-content-wraper"> |
| 117 | <?php |
| 118 | $hash_pass = hash('sha256', wp_salt(32) . md5($attributes['contentPassword'])); |
| 119 | $password_correct = isset($_COOKIE['password_correct_'.$client_id]) ? $_COOKIE['password_correct_'.$client_id] : ''; |
| 120 | if(empty($attributes['lockContent']) || empty($attributes['contentPassword']) || (!empty(Helper::is_password_correct($client_id)) && ($hash_pass === $password_correct)) ){ |
| 121 | |
| 122 | if(!empty($attributes['contentShare'])) { |
| 123 | $content_id = $attributes['clientId']; |
| 124 | $embed .= Helper::embed_content_share($content_id, $attributes); |
| 125 | } |
| 126 | echo $embed; |
| 127 | } else { |
| 128 | if(!empty($attributes['contentShare'])) { |
| 129 | $content_id = $attributes['clientId']; |
| 130 | $embed .= Helper::embed_content_share($content_id, $attributes); |
| 131 | } |
| 132 | Helper::display_password_form($client_id, $embed, $pass_hash_key, $attributes); |
| 133 | } |
| 134 | ?> |
| 135 | </div> |
| 136 | </div> |
| 137 | </div> |
| 138 | </div> |
| 139 | <?php |
| 140 | |
| 141 | echo embedpress_render_block_style($attributes); |
| 142 | |
| 143 | |
| 144 | return ob_get_clean(); |
| 145 | |
| 146 | |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Make style function for embedpress render block |
| 152 | */ |
| 153 | |
| 154 | function embedpress_render_block_style($attributes) |
| 155 | { |
| 156 | |
| 157 | $uniqid = !empty($attributes['url']) ? '.ose-uid-' . md5($attributes['url']) : ''; |
| 158 | |
| 159 | $_iscustomlogo = ''; |
| 160 | |
| 161 | if(!empty($attributes['customlogo'])){ |
| 162 | $_iscustomlogo = $uniqid.' img.watermark.ep-custom-logo { |
| 163 | display: block !important; |
| 164 | }'; |
| 165 | } |
| 166 | $youtubeStyles = '<style> |
| 167 | ' . esc_attr($uniqid) . ' { |
| 168 | width: ' . esc_attr($attributes['width']) . 'px !important; |
| 169 | height: ' . esc_attr($attributes['height']) . 'px; |
| 170 | max-width: 100%; |
| 171 | } |
| 172 | |
| 173 | ' . esc_attr($uniqid) . '>iframe { |
| 174 | height: ' . esc_attr($attributes['height']) . 'px !important; |
| 175 | max-height: ' . esc_attr($attributes['height']) . 'px !important; |
| 176 | width: 100%; |
| 177 | } |
| 178 | |
| 179 | ' . esc_attr($uniqid) . ' .wistia_embed { |
| 180 | max-width: 100%; |
| 181 | } |
| 182 | |
| 183 | .alignright .ose-wistia' . esc_attr($uniqid) .'{ |
| 184 | margin-left: auto; |
| 185 | } |
| 186 | .alignleft .ose-wistia' . esc_attr($uniqid) .'{ |
| 187 | margin-right: auto; |
| 188 | } |
| 189 | .aligncenter .ose-wistia' . esc_attr($uniqid) .'{ |
| 190 | margin: auto; |
| 191 | } |
| 192 | '.$uniqid.' img.watermark{ |
| 193 | display: none; |
| 194 | } |
| 195 | '.$_iscustomlogo.' |
| 196 | </style>'; |
| 197 | |
| 198 | if($attributes['videosize'] == 'responsive') { |
| 199 | |
| 200 | $width = isset($attributes['width']) ? $attributes['width'] : 600; |
| 201 | $height = $width * (9/16); |
| 202 | |
| 203 | |
| 204 | $youtubeStyles = '<style> |
| 205 | ' . esc_attr($uniqid) . ' { |
| 206 | position: relative; |
| 207 | width: ' . esc_attr($attributes['width']) . 'px !important; |
| 208 | height: ' . esc_attr($height) . 'px !important; |
| 209 | max-width: 100%; |
| 210 | } |
| 211 | |
| 212 | .ose-wistia{ |
| 213 | height: auto !important; |
| 214 | padding-top: 0; |
| 215 | } |
| 216 | |
| 217 | ' . esc_attr($uniqid) . ' > iframe { |
| 218 | width: 100%; |
| 219 | height: 100%; |
| 220 | max-height:100%; |
| 221 | } |
| 222 | |
| 223 | .ep-video-responsive{ |
| 224 | display: inline-block!important; |
| 225 | max-width: 100%; |
| 226 | } |
| 227 | '.$uniqid.' img.watermark{ |
| 228 | display: none; |
| 229 | } |
| 230 | '.$_iscustomlogo.' |
| 231 | </style>'; |
| 232 | } |
| 233 | |
| 234 | return $youtubeStyles; |
| 235 | } |
| 236 | ?> |