Elementor_Enhancer.php
3 years ago
EmbedPress_Core_Installer.php
6 years ago
EmbedPress_Notice.php
4 years ago
EmbedPress_Plugin_Usage_Tracker.php
3 years ago
Feature_Enhancer.php
3 years ago
Helper.php
3 years ago
Helper.php
211 lines
| 1 | <?php |
| 2 | |
| 3 | namespace EmbedPress\Includes\Classes; |
| 4 | use EmbedPress\Shortcode; |
| 5 | |
| 6 | if ( !defined('ABSPATH') ) { |
| 7 | exit; |
| 8 | } // Exit if accessed directly |
| 9 | |
| 10 | class Helper { |
| 11 | |
| 12 | /** |
| 13 | * Parse a query string into an associative array. |
| 14 | * |
| 15 | * If multiple values are found for the same key, the value of that key |
| 16 | * value pair will become an array. This function does not parse nested |
| 17 | * PHP style arrays into an associative array (e.g., foo[a]=1&foo[b]=2 will |
| 18 | * be parsed into ['foo[a]' => '1', 'foo[b]' => '2']). |
| 19 | * |
| 20 | * @param string $str Query string to parse |
| 21 | * @param int|bool $urlEncoding How the query string is encoded |
| 22 | * |
| 23 | * @return array |
| 24 | */ |
| 25 | public static function parse_query($str, $urlEncoding = true) |
| 26 | { |
| 27 | $result = []; |
| 28 | |
| 29 | if ($str === '') { |
| 30 | return $result; |
| 31 | } |
| 32 | |
| 33 | if ($urlEncoding === true) { |
| 34 | $decoder = function ($value) { |
| 35 | return rawurldecode(str_replace('+', ' ', $value)); |
| 36 | }; |
| 37 | } elseif ($urlEncoding === PHP_QUERY_RFC3986) { |
| 38 | $decoder = 'rawurldecode'; |
| 39 | } elseif ($urlEncoding === PHP_QUERY_RFC1738) { |
| 40 | $decoder = 'urldecode'; |
| 41 | } else { |
| 42 | $decoder = function ($str) { return $str; }; |
| 43 | } |
| 44 | |
| 45 | foreach (explode('&', $str) as $kvp) { |
| 46 | $parts = explode('=', $kvp, 2); |
| 47 | $key = $decoder($parts[0]); |
| 48 | $value = isset($parts[1]) ? $decoder($parts[1]) : null; |
| 49 | if (!isset($result[$key])) { |
| 50 | $result[$key] = $value; |
| 51 | } else { |
| 52 | if (!is_array($result[$key])) { |
| 53 | $result[$key] = [$result[$key]]; |
| 54 | } |
| 55 | $result[$key][] = $value; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | return $result; |
| 60 | } |
| 61 | public static function get_pdf_renderer() { |
| 62 | $renderer = EMBEDPRESS_URL_ASSETS . 'pdf/web/viewer.html'; |
| 63 | // @TODO; apply settings query args here |
| 64 | return $renderer; |
| 65 | } |
| 66 | |
| 67 | public static function get_extension_from_file_url($url) { |
| 68 | $urlSplit = explode(".", $url); |
| 69 | $ext = end($urlSplit); |
| 70 | return $ext; |
| 71 | |
| 72 | } |
| 73 | |
| 74 | public static function is_file_url($url) { |
| 75 | $pattern = '/\.([0-9a-z]+)(?=[?#])|(\.)(?:[\w]+)$/i'; |
| 76 | return preg_match($pattern, $url) === 1; |
| 77 | } |
| 78 | |
| 79 | public static function is_opensea($url) { |
| 80 | return strpos($url, "opensea.io") !== false; |
| 81 | } |
| 82 | public static function is_youtube_channel($url) { |
| 83 | return (bool) (preg_match('~(?:https?:\/\/)?(?:www\.)?(?:youtube.com\/)(?:channel\/|c\/|user\/|@)(\w+)~i', (string) $url)); |
| 84 | } |
| 85 | |
| 86 | public static function is_youtube($url) { |
| 87 | return (bool) (preg_match('~(?:https?://)?(?:www\.)?(?:youtube\.com|youtu\.be)/watch\?v=([^&]+)~i', (string) $url)); |
| 88 | } |
| 89 | |
| 90 | // Saved sources data temporary in wp_options table |
| 91 | public static function get_source_data($blockid, $source_url, $source_option_name, $source_temp_option_name) { |
| 92 | |
| 93 | |
| 94 | if(self::is_youtube_channel($source_url)){ |
| 95 | $source_name = 'YoutubeChannel'; |
| 96 | } |
| 97 | else if(self::is_youtube($source_url)){ |
| 98 | $source_name = 'Youtube'; |
| 99 | } |
| 100 | else if (!empty(self::is_file_url($source_url))) { |
| 101 | $source_name = 'document_' . self::get_extension_from_file_url($source_url); |
| 102 | } |
| 103 | else if(self::is_opensea($source_url)){ |
| 104 | $source_name = 'OpenSea'; |
| 105 | } |
| 106 | else{ |
| 107 | Shortcode::get_embera_instance(); |
| 108 | $collectios = Shortcode::get_collection(); |
| 109 | $provider = $collectios->findProviders($source_url); |
| 110 | if(!empty($provider[$source_url])){ |
| 111 | $source_name = $provider[$source_url]->getProviderName(); |
| 112 | } |
| 113 | else{ |
| 114 | $source_name = 'Unknown Source'; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | if(!empty($blockid) && $blockid != 'undefined'){ |
| 119 | $sources = json_decode(get_option($source_temp_option_name), true); |
| 120 | |
| 121 | if(!$sources) { |
| 122 | $sources = array(); |
| 123 | } |
| 124 | $exists = false; |
| 125 | |
| 126 | foreach($sources as $i => $source) { |
| 127 | if ($source['id'] === $blockid) { |
| 128 | $sources[$i]['source']['name'] = $source_name; |
| 129 | $sources[$i]['source']['url'] = $source_url; |
| 130 | $exists = true; |
| 131 | break; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | if(!$exists) { |
| 136 | $sources[] = array('id' => $blockid, 'source' => array('name' => $source_name, 'url' => $source_url, 'count' => 1)); |
| 137 | } |
| 138 | |
| 139 | update_option($source_temp_option_name, json_encode($sources)); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | // Saved source data when post updated |
| 144 | public static function get_save_source_data_on_post_update( $source_option_name, $source_temp_option_name ) { |
| 145 | |
| 146 | if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { |
| 147 | return; |
| 148 | } |
| 149 | $temp_data = json_decode(get_option($source_temp_option_name), true); |
| 150 | $source_data = json_decode(get_option($source_option_name), true); |
| 151 | if(!$temp_data) { |
| 152 | $temp_data = array(); |
| 153 | } |
| 154 | if(!$source_data) { |
| 155 | $source_data = array(); |
| 156 | } |
| 157 | |
| 158 | $sources = array_merge($temp_data, $source_data); |
| 159 | |
| 160 | $unique_sources = array(); |
| 161 | foreach ($sources as $source) { |
| 162 | $unique_sources[$source['id']] = $source; |
| 163 | } |
| 164 | |
| 165 | $unique_sources = array_values($unique_sources); |
| 166 | |
| 167 | delete_option($source_temp_option_name); |
| 168 | |
| 169 | update_option($source_option_name, json_encode($unique_sources)); |
| 170 | } |
| 171 | |
| 172 | //Delete source data from option table when widget is removed |
| 173 | public static function get_delete_source_data($blockid, $source_option_name, $source_temp_option_name) { |
| 174 | if (!empty($blockid) && $blockid != 'undefined') { |
| 175 | $sources = json_decode(get_option($source_option_name), true); |
| 176 | $temp_sources = json_decode(get_option($source_temp_option_name), true); |
| 177 | if ($sources) { |
| 178 | foreach ($sources as $i => $source) { |
| 179 | if ($source['id'] === $blockid) { |
| 180 | unset($sources[$i]); |
| 181 | break; |
| 182 | } |
| 183 | } |
| 184 | update_option($source_option_name, json_encode(array_values($sources))); |
| 185 | } |
| 186 | if ($temp_sources) { |
| 187 | foreach ($temp_sources as $i => $source) { |
| 188 | if ($source['id'] === $blockid) { |
| 189 | unset($temp_sources[$i]); |
| 190 | break; |
| 191 | } |
| 192 | } |
| 193 | update_option($source_temp_option_name, json_encode(array_values($temp_sources))); |
| 194 | } |
| 195 | } |
| 196 | wp_die(); |
| 197 | } |
| 198 | |
| 199 | //Delete source temporary data when reload without update or publish |
| 200 | public static function get_delete_source_temp_data_on_reload($source_temp_option_name) { |
| 201 | $source_temp_data = json_decode(get_option($source_temp_option_name), true); |
| 202 | if ($source_temp_data ) { |
| 203 | delete_option( $source_temp_option_name ); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | public static function get_file_title($url){ |
| 208 | return get_the_title(attachment_url_to_postid( $url )); |
| 209 | } |
| 210 | } |
| 211 |