cap->edit_others_posts)) { return ''; } global $wpdb; $statuses = array_values(get_post_stati(['public' => true])); $in = implode(',', array_fill(0, count($statuses), '%s')); return (string) $wpdb->prepare( "({$wpdb->posts}.post_author = %d OR ({$wpdb->posts}.post_status IN ($in) AND {$wpdb->posts}.post_password = ''))", // phpcs:ignore ...array_merge([get_current_user_id()], $statuses) ); } /** 投稿者の表示名・ログイン名に一致する投稿。 */ public static function author(string $term): string { global $wpdb; $like = '%' . $wpdb->esc_like($term) . '%'; // ログイン名は、ユーザーを管理できる人の検索でだけ対象にする。 return (string) $wpdb->prepare( "{$wpdb->posts}.post_author IN (SELECT ID FROM {$wpdb->users} WHERE display_name LIKE %s OR (1 = %d AND user_login LIKE %s))", // phpcs:ignore $like, current_user_can('list_users') ? 1 : 0, $like ); } /** 指定タクソノミーのターム名に一致する投稿。 */ public static function taxonomy(string $taxonomy, string $term): string { global $wpdb; $like = '%' . $wpdb->esc_like($term) . '%'; return (string) $wpdb->prepare( "EXISTS (SELECT 1 FROM {$wpdb->term_relationships} tr" . " JOIN {$wpdb->term_taxonomy} tt ON tt.term_taxonomy_id = tr.term_taxonomy_id" . " JOIN {$wpdb->terms} t ON t.term_id = tt.term_id" . " WHERE tr.object_id = {$wpdb->posts}.ID AND tt.taxonomy = %s AND t.name LIKE %s)", // phpcs:ignore $taxonomy, $like ); } /** * メタ値に「関連先の ID」を保存する項目(ACF の投稿オブジェクト・ユーザー・タクソノミーなど)を、関連先の名前で検索する。 * 保存値は ID 単体、またはシリアライズされた ID の配列。ID は完全一致で判定する(部分一致の誤ヒットを避ける)。 * @param 'post'|'user'|'term' $target */ public static function reference(string $metaKey, string $target, string $term): ?string { global $wpdb; $like = '%' . $wpdb->esc_like($term) . '%'; [$visible, $visibleArgs] = self::visibleTargets(); $postCond = $visible === '' ? 'r.post_title LIKE %s' : "({$visible} AND r.post_title LIKE %s)"; $postArgs = array_merge($visibleArgs, [$like]); // ログイン名は、ユーザーを管理できる人の検索でだけ対象にする。 $userCond = current_user_can('list_users') ? '(r.display_name LIKE %s OR r.user_login LIKE %s)' : 'r.display_name LIKE %s'; $map = [ 'post' => [$wpdb->posts, 'ID', $postCond, $postArgs], 'user' => [$wpdb->users, 'ID', $userCond, array_fill(0, current_user_can('list_users') ? 2 : 1, $like)], 'term' => [$wpdb->terms, 'term_id', 'r.name LIKE %s', [$like]], ]; if (!isset($map[$target])) { return null; } [$table, $idCol, $cond, $condArgs] = $map[$target]; return (string) $wpdb->prepare( "EXISTS (SELECT 1 FROM {$wpdb->postmeta} rm JOIN {$table} r ON (" . "rm.meta_value = CAST(r.{$idCol} AS CHAR)" . " OR rm.meta_value LIKE CONCAT(%s, r.{$idCol}, %s)" . " OR rm.meta_value LIKE CONCAT(%s, r.{$idCol}, %s))" . " WHERE rm.post_id = {$wpdb->posts}.ID AND rm.meta_key = %s AND {$cond})", // phpcs:ignore // シリアライズ配列の中の ID(文字列 "123" と整数 i:123;)を挟むワイルドカードと、メタキー・検索語。 ...array_merge(['%"', '"%', '%;i:', ';%', $metaKey], $condArgs) ); } /** * 関連検索の対象にしてよい関連先(別名 r)の条件と、プレースホルダーの値。制限が要らない場合は空文字。 * セルの表示(Access::canRead)と同じく、添付ファイルは親の投稿の状態を、添付ファイルの権限で判定する。 * @return array{0:string,1:list} */ private static function visibleTargets(): array { if (current_user_can('manage_options')) { return ['', []]; } global $wpdb; [$self, $selfArgs] = self::visibleRows('r'); $statuses = array_values(get_post_stati(['public' => true])); $parent = ['p.post_status IN (' . implode(',', array_fill(0, count($statuses), '%s')) . ')']; $cap = get_post_type_object('attachment')->cap; if (self::canSeePrivate($cap)) { $parent[] = "p.post_status = 'private'"; } if (current_user_can($cap->edit_others_posts)) { $parent[] = "p.post_status <> 'private'"; } $sql = "({$self} OR (r.post_type = 'attachment' AND r.post_status = 'inherit' AND (r.post_parent = 0" . " OR NOT EXISTS (SELECT 1 FROM {$wpdb->posts} x WHERE x.ID = r.post_parent)" . " OR EXISTS (SELECT 1 FROM {$wpdb->posts} p WHERE p.ID = r.post_parent AND (" . implode(' OR ', $parent) . ')))))'; return [$sql, array_merge($selfArgs, $statuses)]; } /** 他人の非公開を閲覧できるか(read_private、または他人の非公開を編集する権限)。 */ private static function canSeePrivate(object $cap): bool { return current_user_can($cap->read_private_posts) || (current_user_can($cap->edit_others_posts) && isset($cap->edit_private_posts) && current_user_can($cap->edit_private_posts)); } /** * 公開済みか、自分の投稿か、投稿タイプごとの権限で閲覧できる行の条件。 * 非公開は read_private(または他人の非公開を編集する権限)、下書きなどは他人の投稿を編集する権限で判定する。 * @return array{0:string,1:list} */ private static function visibleRows(string $alias): array { $private = []; $unpublished = []; foreach (get_post_types([], 'objects') as $type) { if (self::canSeePrivate($type->cap)) { $private[] = $type->name; } if (current_user_can($type->cap->edit_others_posts)) { $unpublished[] = $type->name; } } $statuses = array_values(get_post_stati(['public' => true])); $list = static fn (array $values): string => implode(',', array_fill(0, count($values), '%s')); $or = ["{$alias}.post_status IN (" . $list($statuses) . ')', "{$alias}.post_author = %d"]; $args = array_merge($statuses, [get_current_user_id()]); if ($private) { $or[] = "({$alias}.post_type IN (" . $list($private) . ") AND {$alias}.post_status = 'private')"; $args = array_merge($args, $private); } if ($unpublished) { $or[] = "({$alias}.post_type IN (" . $list($unpublished) . ") AND {$alias}.post_status NOT IN ('private', 'inherit'))"; $args = array_merge($args, $unpublished); } return ['(' . implode(' OR ', $or) . ')', $args]; } }