checkbox.php
3 weeks ago
date.php
3 weeks ago
datetime.php
3 weeks ago
field.php
3 weeks ago
gallery.php
3 weeks ago
map.php
3 weeks ago
media-url.php
3 weeks ago
media.php
3 weeks ago
number.php
3 weeks ago
post.php
3 weeks ago
radio.php
3 weeks ago
repeater.php
3 weeks ago
select.php
3 weeks ago
switcher.php
3 weeks ago
text.php
3 weeks ago
time.php
3 weeks ago
toggle.php
3 weeks ago
user.php
3 weeks ago
post.php
67 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Wpai\AddonAPI; |
| 4 | |
| 5 | if ( ! defined( 'ABSPATH' ) ) exit; |
| 6 | |
| 7 | class PMXI_Addon_Post_Field extends PMXI_Addon_Field { |
| 8 | |
| 9 | static $repeater_path = 'value'; |
| 10 | |
| 11 | public function parseDelimitedValues($value) { |
| 12 | $delimiter = $value['delim'] ?? ','; |
| 13 | $values = explode($delimiter, $value['value'] ?? ''); |
| 14 | $values = array_filter($values); |
| 15 | $values = array_map('trim', $values); |
| 16 | return $values; |
| 17 | } |
| 18 | |
| 19 | public function beforeImport($postId, $value, $data, $logger, $rawData) { |
| 20 | global $wpdb; |
| 21 | |
| 22 | $post_ids = []; |
| 23 | $values = $this->parseDelimitedValues($value); |
| 24 | $post_types = $this->args['search_post_type'] ?? [$data['articleData']['post_type']]; |
| 25 | |
| 26 | if (empty($values)) { |
| 27 | return $post_ids; |
| 28 | } |
| 29 | |
| 30 | foreach ($values as $ev) { |
| 31 | $relation = false; |
| 32 | |
| 33 | if (ctype_digit($ev)) { |
| 34 | if (empty($post_types)) { |
| 35 | // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- {$wpdb->posts} is an internal table name from $wpdb. |
| 36 | $relation = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wpdb->posts} WHERE ID = %d", $ev)); |
| 37 | } else { |
| 38 | $placeholders = implode(',', array_fill(0, count($post_types), '%s')); |
| 39 | // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- {$wpdb->posts} is an internal table name from $wpdb; $placeholders contains only %s tokens. |
| 40 | $relation = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wpdb->posts} WHERE ID = %d AND post_type IN ($placeholders)", array_merge([$ev], $post_types))); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | if (empty($relation)) { |
| 45 | if (empty($post_types)) { |
| 46 | // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- {$wpdb->posts} is an internal table name from $wpdb. |
| 47 | $relation = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wpdb->posts} WHERE post_type != %s AND ( post_title = %s OR post_name = %s )", 'revision', $ev, sanitize_title_for_query($ev))); |
| 48 | } else { |
| 49 | $placeholders = implode(',', array_fill(0, count($post_types), '%s')); |
| 50 | // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.PreparedSQLPlaceholders.ReplacementsWrongNumber -- {$wpdb->posts} is an internal table name from $wpdb; $placeholders contains only %s tokens. |
| 51 | $relation = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wpdb->posts} WHERE post_type IN ($placeholders) AND ( post_title = %s OR post_name = %s )", array_merge($post_types, [$ev, sanitize_title_for_query($ev)]))); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | if ($relation) { |
| 56 | $post_ids[] = (string) $relation->ID; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | if (empty($this->multiple)) { |
| 61 | return array_shift($post_ids); |
| 62 | } |
| 63 | |
| 64 | return $post_ids; |
| 65 | } |
| 66 | } |
| 67 |