0 && wp_attachment_is_image( $id ) ) { return $id; } if ( '' !== $url && 1 === preg_match( '#^https?://#i', $url ) ) { return (int) attachment_url_to_postid( $url ); } return 0; } /** * Unserializes a value another plugin stored, without ever creating objects. * * @since 1.0.0 * * @param mixed $value Stored value. * @return mixed The value; arrays come back as arrays, anything that is not serialized as it was. */ public static function unserialize( $value ) { if ( ! is_string( $value ) || ! is_serialized( $value ) ) { return $value; } return unserialize( $value, array( 'allowed_classes' => false ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.serialize_unserialize -- Data another plugin stored; classes are not allowed. } /** * Keywords from a comma-separated list: at most five, without empty ones. * * @since 1.0.0 * * @param string $items Text such as `one, two`. * @return string[] */ public static function keywords( string $items ): array { return Sanitize::keywords( array_map( 'trim', explode( ',', $items ) ) ); } /** * Drops fields that have nothing in them. Zero and false are values; empty strings and arrays are not. * * @since 1.0.0 * * @param array $fields Fields. * @return array */ public static function filled( array $fields ): array { return array_filter( $fields, static function ( $value ) { return ! ( '' === $value || array() === $value || null === $value || 0 === $value ); } ); } /** * The ElevenRank meta keys that hold a template, which need their variables converted. * * @since 1.0.0 * * @return string[] */ public static function template_keys(): array { return array( Keys::TITLE, Keys::DESCRIPTION, Keys::OG_TITLE, Keys::OG_DESCRIPTION, Keys::TWITTER_TITLE, Keys::TWITTER_DESCRIPTION ); } /** * Reads the meta of a set of objects in one query. * * @since 1.0.0 * * @param string $table Meta table (`postmeta`, `termmeta` or `usermeta`) without prefix. * @param string $id_column `post_id`, `term_id` or `user_id`. * @param int[] $ids Object ids. * @param string $prefix Only meta keys that start with this. * @return array> Values by object id and meta key. */ public static function meta_for( string $table, string $id_column, array $ids, string $prefix ): array { global $wpdb; if ( ! $ids ) { return array(); } $ids = array_map( 'absint', $ids ); $rows = array(); $place = implode( ',', array_fill( 0, count( $ids ), '%d' ) ); // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare,WordPress.DB.PreparedSQLPlaceholders.ReplacementsWrongNumber $found = $wpdb->get_results( $wpdb->prepare( "SELECT %i AS object_id, meta_key, meta_value FROM %i WHERE %i IN ({$place}) AND meta_key LIKE %s", array_merge( array( $id_column, $wpdb->prefix . $table, $id_column ), $ids, array( $wpdb->esc_like( $prefix ) . '%' ) ) ), ARRAY_A ); // phpcs:enable foreach ( is_array( $found ) ? $found : array() as $row ) { $rows[ (int) $row['object_id'] ][ (string) $row['meta_key'] ] = (string) $row['meta_value']; } return $rows; } /** * Ids of objects that have at least one meta key with a prefix, in id order, after a cursor. * * @since 1.0.0 * * @param string $table Meta table without prefix: `postmeta`, `termmeta` or `usermeta`. * @param string $id_column `post_id`, `term_id` or `user_id`. * @param string $prefix Meta key prefix. * @param int $after Only ids above this. * @param int $limit Most ids. * @return int[] */ public static function ids_with_meta( string $table, string $id_column, string $prefix, int $after, int $limit ): array { global $wpdb; // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching $ids = $wpdb->get_col( $wpdb->prepare( 'SELECT DISTINCT %i FROM %i WHERE meta_key LIKE %s AND %i > %d ORDER BY %i ASC LIMIT %d', $id_column, $wpdb->prefix . $table, $wpdb->esc_like( $prefix ) . '%', $id_column, $after, $id_column, $limit ) ); // phpcs:enable return array_map( 'intval', is_array( $ids ) ? $ids : array() ); } /** * Number of objects that have at least one meta key with a prefix. * * @since 1.0.0 * * @param string $table Meta table without prefix. * @param string $id_column Id column. * @param string $prefix Meta key prefix. * @return int */ public static function count_with_meta( string $table, string $id_column, string $prefix ): int { global $wpdb; // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching return (int) $wpdb->get_var( $wpdb->prepare( 'SELECT COUNT(DISTINCT %i) FROM %i WHERE meta_key LIKE %s', $id_column, $wpdb->prefix . $table, $wpdb->esc_like( $prefix ) . '%' ) ); // phpcs:enable } }