$field, 'value' => \get_field( $field['name'] ), 'post_id' => $post_id, ); } return $check; } /** * Fires immediately after updating metadata of a specific type. * * @param int $meta_id ID of updated metadata entry. * @param int $object_id ID of the object metadata is for. * @param string $meta_key Metadata key. * @param mixed $_meta_value Metadata value. Serialized if non-scalar. * * @since 4.5.0 */ public static function on_field_updated( $meta_id, $object_id, $meta_key, $_meta_value ) { if ( in_array( $meta_key, array_keys( self::$old_meta ) ) ) { // phpcs:ignore if ( WP_Meta_Data_Sensor::can_log_meta_key( 'post', $object_id, $meta_key ) ) { $old_value = self::convert_to_array_of_post_ids( self::$old_meta[ $meta_key ]['value'] ); $new_value = self::convert_to_array_of_post_ids( $_meta_value ); $removed = array_diff( $old_value, $new_value ); $added = array_diff( $new_value, $old_value ); if ( ! empty( $added ) ) { self::log_event( 2131, $added, $object_id, $meta_key, $meta_id ); } if ( ! empty( $removed ) ) { self::log_event( 2132, $removed, $object_id, $meta_key, $meta_id ); } } } } /** * Convert arbitrary value to an array of post IDs. * * @param mixed $value An array of posts or post IDs or post ID as string. * * @return int[] * * @since 4.5.0 */ private static function convert_to_array_of_post_ids( $value ) { $result = array(); if ( is_array( $value ) ) { $result = array_map( function ( $item ) { return ( $item instanceof \WP_Post ) ? $item->ID : intval( $item ); }, $value ); } return $result; } /** * Log event related to ACF relationship field. * * @param int $event_id Event ID. * @param int[]|WP_Post[] $relationship_post_ids Posts or post IDs. * @param int $object_id Object ID. * @param string $meta_key Meta key. * @param int $meta_id Meta ID. * * @since 4.5.0 */ private static function log_event( $event_id, $relationship_post_ids, $object_id, $meta_key, $meta_id ) { $post = get_post( $object_id ); $editor_link = WP_Content_Sensor::get_editor_link( $object_id ); Alert_Manager::trigger_event( $event_id, array( 'PostID' => $object_id, 'PostTitle' => $post->post_title, 'PostStatus' => $post->post_status, 'PostType' => $post->post_type, 'PostDate' => $post->post_date, 'PostUrl' => get_permalink( $post->ID ), 'MetaID' => $meta_id, 'MetaKey' => $meta_key, 'Relationships' => self::format_relationships_label( $relationship_post_ids ), 'MetaLink' => $meta_key, $editor_link['name'] => $editor_link['value'], ) ); } /** * Formats the relationship label for the activity log entry. * * @param int[] $post_ids List of post IDs. * * @return string * * @since 4.5.0 */ private static function format_relationships_label( $post_ids ) { return implode( ', ', array_map( function ( $post_id ) { return get_the_title( $post_id ) . ' (' . $post_id . ')'; }, $post_ids ) ); } } }