Analytics
2 months ago
Database
1 month ago
DynamicFieldResolver.php
1 month ago
Elementor_Enhancer.php
6 months ago
EmbedPress_Core_Installer.php
6 years ago
EmbedPress_Notice.php
3 months ago
EmbedPress_Plugin_Usage_Tracker.php
2 months ago
Extend_CustomPlayer_Controls.php
1 month ago
Extend_Elementor_Controls.php
1 year ago
FeatureNoticeManager.php
8 months ago
FeatureNotices.php
8 months ago
Feature_Enhancer.php
1 month ago
Helper.php
1 month ago
Pdf_Thumbnail_Handler.php
2 months ago
PermalinkHelper.php
10 months ago
README_FEATURE_NOTICES.md
8 months ago
DynamicFieldResolver.php
152 lines
| 1 | <?php |
| 2 | |
| 3 | namespace EmbedPress\Includes\Classes; |
| 4 | |
| 5 | if (!defined('ABSPATH')) { |
| 6 | exit; |
| 7 | } |
| 8 | |
| 9 | /** |
| 10 | * Resolves dynamic embed URLs from custom-field sources (ACF, MetaBox, Pods, |
| 11 | * Toolset, JetEngine, raw post meta). |
| 12 | * |
| 13 | * Used by: |
| 14 | * - Elementor PDF / Document widgets via resolve_elementor_dynamic() |
| 15 | * - Gutenberg block renderer + shortcode via resolve_field() |
| 16 | * |
| 17 | * Why one class: the three surfaces (Elementor / Gutenberg / shortcode) all |
| 18 | * need the same source map. Keeping it in one place means adding a new field |
| 19 | * plugin (e.g. SCF, Carbon Fields) touches one method, not three. |
| 20 | */ |
| 21 | class DynamicFieldResolver |
| 22 | { |
| 23 | /** |
| 24 | * Resolve a custom-field URL given an explicit source + field name. |
| 25 | * Used by Gutenberg blocks and shortcodes (which know their source directly). |
| 26 | * |
| 27 | * @param string $source acf|metabox|pods|toolset|jetengine|meta |
| 28 | * @param string $field Field name / key on the current post. |
| 29 | * @param int|null $post_id Defaults to current queried post. |
| 30 | * @return string Resolved URL, or '' when no value / source unavailable. |
| 31 | */ |
| 32 | public static function resolve_field($source, $field, $post_id = null) |
| 33 | { |
| 34 | $field = sanitize_key((string) $field); |
| 35 | if ($field === '') { |
| 36 | return ''; |
| 37 | } |
| 38 | |
| 39 | if ($post_id === null) { |
| 40 | $post_id = get_the_ID(); |
| 41 | } |
| 42 | if (!$post_id) { |
| 43 | return ''; |
| 44 | } |
| 45 | |
| 46 | $url = ''; |
| 47 | |
| 48 | switch ($source) { |
| 49 | case 'acf': |
| 50 | if (function_exists('get_field')) { |
| 51 | $value = get_field($field, $post_id); |
| 52 | $url = is_array($value) && isset($value['url']) ? $value['url'] : (string) $value; |
| 53 | } |
| 54 | break; |
| 55 | |
| 56 | case 'metabox': |
| 57 | // MetaBox exposes rwmb_meta(); fall back to raw post meta if unavailable. |
| 58 | if (function_exists('rwmb_meta')) { |
| 59 | $value = rwmb_meta($field, '', $post_id); |
| 60 | $url = is_array($value) && isset($value['url']) ? $value['url'] : (string) $value; |
| 61 | } else { |
| 62 | $url = (string) get_post_meta($post_id, $field, true); |
| 63 | } |
| 64 | break; |
| 65 | |
| 66 | case 'pods': |
| 67 | if (function_exists('pods_field')) { |
| 68 | $value = pods_field(get_post_type($post_id), $post_id, $field, true); |
| 69 | $url = is_array($value) && isset($value['guid']) |
| 70 | ? $value['guid'] |
| 71 | : (is_array($value) && isset($value['url']) ? $value['url'] : (string) $value); |
| 72 | } else { |
| 73 | $url = (string) get_post_meta($post_id, $field, true); |
| 74 | } |
| 75 | break; |
| 76 | |
| 77 | case 'toolset': |
| 78 | // Toolset Types prefixes meta keys with `wpcf-`. |
| 79 | $url = (string) get_post_meta($post_id, 'wpcf-' . $field, true); |
| 80 | break; |
| 81 | |
| 82 | case 'jetengine': |
| 83 | case 'meta': |
| 84 | default: |
| 85 | $url = (string) get_post_meta($post_id, $field, true); |
| 86 | break; |
| 87 | } |
| 88 | |
| 89 | // Back-compat: existing free 4.5.x filter, applied by the legacy |
| 90 | // Elementor PDF/Document inline resolvers. Keep firing it from the |
| 91 | // central path so third-party code keeps working. |
| 92 | $url = apply_filters('embedpress/custom_meta_field_value', $url, $field); |
| 93 | |
| 94 | return is_string($url) ? trim($url) : ''; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Resolve a dynamic URL coming from Elementor's `__dynamic__` payload |
| 99 | * (the picker emits an HTML-encoded blob containing the source + field). |
| 100 | * |
| 101 | * @param string $dynamic_value Raw `__dynamic__[<control>]` value. |
| 102 | * @return string Resolved URL, or '' if nothing could be resolved. |
| 103 | */ |
| 104 | public static function resolve_elementor_dynamic($dynamic_value) |
| 105 | { |
| 106 | if (empty($dynamic_value)) { |
| 107 | return ''; |
| 108 | } |
| 109 | |
| 110 | $decoded = urldecode($dynamic_value); |
| 111 | |
| 112 | if (!preg_match('/name="([^"]+)"/', $decoded, $name_matches)) { |
| 113 | return ''; |
| 114 | } |
| 115 | $name_key = $name_matches[1]; |
| 116 | |
| 117 | $source = ''; |
| 118 | $pattern = ''; |
| 119 | |
| 120 | if ($name_key === 'acf-url' && class_exists('ACF') && function_exists('get_field')) { |
| 121 | $source = 'acf'; |
| 122 | $pattern = '/"key":"[^"]+:(.*?)"/'; |
| 123 | } elseif ($name_key === 'toolset-url' && class_exists('Types_Helper_Output_Meta_Box')) { |
| 124 | $source = 'toolset'; |
| 125 | $pattern = '/"key":"[^"]+:(.*?)"/'; |
| 126 | } elseif ($name_key === 'jet-post-custom-field' && class_exists('Jet_Engine')) { |
| 127 | $source = 'jetengine'; |
| 128 | $pattern = '/"meta_field":"([^"]+)"/'; |
| 129 | } |
| 130 | |
| 131 | if ($source === '' || !preg_match($pattern, $decoded, $matches) || empty($matches[1])) { |
| 132 | return self::elementor_fallback($decoded); |
| 133 | } |
| 134 | |
| 135 | $url = self::resolve_field($source, $matches[1]); |
| 136 | |
| 137 | if ($url === '') { |
| 138 | $url = self::elementor_fallback($decoded); |
| 139 | } |
| 140 | |
| 141 | return esc_url_raw($url); |
| 142 | } |
| 143 | |
| 144 | private static function elementor_fallback($decoded) |
| 145 | { |
| 146 | if (preg_match('/"fallback":"([^"]+)"/', $decoded, $m)) { |
| 147 | return (string) $m[1]; |
| 148 | } |
| 149 | return ''; |
| 150 | } |
| 151 | } |
| 152 |