PluginProbe ʕ •ᴥ•ʔ
EmbedPress – PDF Embedder, Embed PDF viewer, YouTube Videos, 3D FlipBook, Social feeds & more / 4.5.4
EmbedPress – PDF Embedder, Embed PDF viewer, YouTube Videos, 3D FlipBook, Social feeds & more v4.5.4
4.5.6 4.5.5 4.5.4 4.5.3 4.5.2 trunk 1.0.0 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.3.0 1.3.1 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.5.0 1.6.0 1.6.1 1.6.2 1.6.3 1.7.0 1.7.1 1.7.2 1.7.3 1.7.4 1.7.5 2.0.0 2.0.1 2.0.2 2.0.3 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.2.0 2.2.1 2.2.2 2.3.0 2.3.1 2.3.2 2.3.3 2.4.0 2.4.1 2.5.0 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 2.6.0 2.6.1 2.6.2 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.7.7 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.1.2 3.1.3 3.2.0 3.2.1 3.3.0 3.3.1 3.3.2 3.3.3 3.3.4 3.3.5 3.3.6 3.3.7 3.4.0 3.4.1 3.4.2 3.4.3 3.5.0 3.5.1 3.5.2 3.5.3 3.6.0 3.6.1 3.6.2 3.6.3 3.6.4 3.6.5 3.6.6 3.6.7 3.6.8 3.7.0 3.7.1 3.7.2 3.7.3 3.8.0 3.8.1 3.8.2 3.8.3 3.8.4 3.8.5 3.9.0 3.9.1 3.9.10 3.9.11 3.9.12 3.9.13 3.9.14 3.9.15 3.9.16 3.9.17 3.9.2 3.9.3 3.9.4 3.9.5 3.9.6 3.9.7 3.9.8 3.9.9 4.0.0 4.0.1 4.0.10 4.0.11 4.0.12 4.0.13 4.0.14 4.0.2 4.0.3 4.0.4 4.0.5 4.0.6 4.0.7 4.0.8 4.0.9 4.1.0 4.1.1 4.1.10 4.1.2 4.1.3 4.1.4 4.1.5 4.1.6 4.1.7 4.1.8 4.1.9 4.2.0 4.2.1 4.2.2 4.2.3 4.2.4 4.2.5 4.2.6 4.2.7 4.2.8 4.2.9 4.3.0 4.3.1 4.4.0 4.4.1 4.4.10 4.4.11 4.4.2 4.4.3 4.4.4 4.4.5 4.4.6 4.4.7 4.4.8 4.4.9 4.5.0 4.5.1
embedpress / EmbedPress / Includes / Classes / DynamicFieldResolver.php
embedpress / EmbedPress / Includes / Classes Last commit date
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