← All changes
|
jetpack_vendor/automattic/jetpack-publicize/src/class-publicize-base.php
+73
-22
16.2
→
16.3-beta
View file →
| @@ -271,8 +271,10 @@ | ||
| 271 | 271 | |
| 272 | 272 | // Custom priority to ensure post type support is added prior to thumbnail support being added to the theme. |
| 273 | 273 | add_action( 'init', array( $this, 'add_post_type_support' ), 8 ); |
| 274 | 274 | add_action( 'init', array( $this, 'register_post_meta' ), 20 ); |
| 275 | + // Last, so a veto from another filter is respected before any row changes. | |
| 276 | + add_filter( 'update_post_metadata', array( $this, 'collapse_duplicate_post_meta' ), PHP_INT_MAX, 5 ); | |
| 275 | 277 | |
| 276 | 278 | // The custom priority for this action ensures that any existing code that |
| 277 | 279 | // removes post-thumbnails support during 'init' continues to work. |
| 278 | 280 | add_action( 'init', __NAMESPACE__ . '\add_theme_post_thumbnails_support', 8 ); |
| @@ -1010,37 +1012,20 @@ | ||
| 1010 | 1012 | */ |
| 1011 | 1013 | abstract public function post_is_done_sharing( $post_id = null ); |
| 1012 | 1014 | |
| 1013 | 1015 | /** |
| 1014 | - * Retrieves full list of available Publicize connection services. | |
| 1016 | + * Retrieve the available Publicize services and their connect URLs. | |
| 1015 | 1017 | * |
| 1016 | - * Retrieves current available publicize service connections | |
| 1017 | - * with associated labels and URLs. | |
| 1018 | - * | |
| 1019 | 1018 | * @since 0.1.0 |
| 1020 | 1019 | * @since-jetpack 6.7.0 |
| 1020 | + * @deprecated 0.88.0 Services and their connect URLs come from the `publicize/services` REST endpoint. | |
| 1021 | 1021 | * |
| 1022 | - * @return array { | |
| 1023 | - * Array of UI service connection data for all services | |
| 1024 | - * | |
| 1025 | - * @type string 'name' Name of service. | |
| 1026 | - * @type string 'label' Display label for service. | |
| 1027 | - * @type string 'url' URL for adding connection to service. | |
| 1028 | - * } | |
| 1022 | + * @return array Always empty. | |
| 1029 | 1023 | */ |
| 1030 | 1024 | public function get_available_service_data() { |
| 1031 | - $available_services = $this->get_services( 'all' ); | |
| 1032 | - $available_service_data = array(); | |
| 1025 | + _deprecated_function( __METHOD__, 'publicize-0.88.0' ); | |
| 1033 | 1026 | |
| 1034 | - foreach ( $available_services as $service_name => $service ) { | |
| 1035 | - $available_service_data[] = array( | |
| 1036 | - 'name' => $service_name, | |
| 1037 | - 'label' => static::get_service_label( $service_name ), | |
| 1038 | - 'url' => $this->connect_url( $service_name ), | |
| 1039 | - ); | |
| 1040 | - } | |
| 1041 | - | |
| 1042 | - return $available_service_data; | |
| 1027 | + return array(); | |
| 1043 | 1028 | } |
| 1044 | 1029 | |
| 1045 | 1030 | /** |
| 1046 | 1031 | * Site Data |
| @@ -1321,8 +1306,74 @@ | ||
| 1321 | 1306 | |
| 1322 | 1307 | // The focal point lives on the image (attachment), not the post, so it is shared |
| 1323 | 1308 | // by every post that uses the image. Registered once, not per publicizeable type. |
| 1324 | 1309 | register_post_meta( 'attachment', self::ATTACHMENT_IMAGE_FOCAL_POINT, $image_focal_point_args ); |
| 1310 | + } | |
| 1311 | + | |
| 1312 | + /** | |
| 1313 | + * Collapse duplicate rows of a single-value Publicize post meta key into one on update. | |
| 1314 | + * | |
| 1315 | + * With several rows core skips its "unchanged" check, so an UPDATE that matches no changed row fails. | |
| 1316 | + * | |
| 1317 | + * @param null|bool $check Whether to short-circuit the update. | |
| 1318 | + * @param int $object_id Post ID. | |
| 1319 | + * @param string $meta_key Meta key. | |
| 1320 | + * @param mixed $meta_value Unslashed meta value. | |
| 1321 | + * @param mixed $prev_value Previous value to match, if any. | |
| 1322 | + * @return null|bool Null to let core update the meta, otherwise whether the update succeeded. | |
| 1323 | + */ | |
| 1324 | + public function collapse_duplicate_post_meta( $check, $object_id, $meta_key, $meta_value, $prev_value ) { | |
| 1325 | + // Cheap bail first: this runs on every post meta update site-wide. | |
| 1326 | + if ( null !== $check || ! empty( $prev_value ) || 0 !== strncmp( $meta_key, '_wpas_', 6 ) ) { | |
| 1327 | + return $check; | |
| 1328 | + } | |
| 1329 | + | |
| 1330 | + $single_keys = array( | |
| 1331 | + $this->POST_MESS, | |
| 1332 | + self::POST_PUBLICIZE_FEATURE_ENABLED, | |
| 1333 | + $this->POST_DONE . 'all', | |
| 1334 | + self::POST_JETPACK_SOCIAL_OPTIONS, | |
| 1335 | + self::POST_CONNECTION_OVERRIDES, | |
| 1336 | + self::POST_CUSTOMIZE_PER_NETWORK, | |
| 1337 | + ); | |
| 1338 | + if ( ! in_array( $meta_key, $single_keys, true ) ) { | |
| 1339 | + return $check; | |
| 1340 | + } | |
| 1341 | + | |
| 1342 | + $stored = get_metadata_raw( 'post', $object_id, $meta_key, false ); | |
| 1343 | + if ( ! is_array( $stored ) || count( $stored ) < 2 ) { | |
| 1344 | + return $check; | |
| 1345 | + } | |
| 1346 | + | |
| 1347 | + global $wpdb; | |
| 1348 | + // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Row IDs aren't exposed by the meta API. | |
| 1349 | + $rows = $wpdb->get_results( $wpdb->prepare( "SELECT meta_id, meta_value FROM {$wpdb->postmeta} WHERE post_id = %d AND meta_key = %s ORDER BY meta_id ASC", $object_id, $meta_key ) ); | |
| 1350 | + if ( ! is_array( $rows ) || count( $rows ) < 2 ) { | |
| 1351 | + return $check; | |
| 1352 | + } | |
| 1353 | + | |
| 1354 | + // Bail before deleting, so a failed write leaves every row intact. | |
| 1355 | + $keep = array_shift( $rows ); | |
| 1356 | + // Cast like `$wpdb` does, e.g. `true` is stored as "1". | |
| 1357 | + $wanted = (string) maybe_serialize( $meta_value ); | |
| 1358 | + if ( $wanted !== $keep->meta_value && ! update_metadata_by_mid( 'post', (int) $keep->meta_id, $meta_value ) ) { | |
| 1359 | + // A 0-row UPDATE (e.g. a concurrent collapse) looks like a failure, so confirm the row. | |
| 1360 | + $current = get_metadata_by_mid( 'post', (int) $keep->meta_id ); | |
| 1361 | + if ( ! $current || $wanted !== (string) maybe_serialize( $current->meta_value ) ) { | |
| 1362 | + return false; | |
| 1363 | + } | |
| 1364 | + } | |
| 1365 | + | |
| 1366 | + // The kept row holds the value, so a redundant row that is already gone is not a failure. | |
| 1367 | + foreach ( $rows as $row ) { | |
| 1368 | + delete_metadata_by_mid( 'post', (int) $row->meta_id ); | |
| 1369 | + } | |
| 1370 | + | |
| 1371 | + // The 0-row and already-deleted paths skip core's cache flush. | |
| 1372 | + wp_cache_delete( $object_id, 'post_meta' ); | |
| 1373 | + | |
| 1374 | + // Unlike core, report an unchanged value as success, which is what lets the REST save complete. | |
| 1375 | + return true; | |
| 1325 | 1376 | } |
| 1326 | 1377 | |
| 1327 | 1378 | /** |
| 1328 | 1379 | * Auth callback for the image focal point attachment meta. |