From d890265cb2965eb9d7548b335baeeabd57686211 Mon Sep 17 00:00:00 2001 From: Travis Weston Date: Fri, 28 Jul 2023 15:24:54 -0400 Subject: [PATCH 1/2] WIP: PEERS-606 From 0a0c68bc52889a12fa4d224b1ccf66383a53a11f Mon Sep 17 00:00:00 2001 From: Travis Weston Date: Fri, 28 Jul 2023 16:20:28 -0400 Subject: [PATCH 2/2] Make it so post meta can be attached to all post types --- config/post-meta.json | 4 +--- src/class-generator.php | 2 +- src/meta.php | 32 ++++++++++++++++++++++++++------ 3 files changed, 28 insertions(+), 10 deletions(-) diff --git a/config/post-meta.json b/config/post-meta.json index 3028aaf..10a1f2e 100644 --- a/config/post-meta.json +++ b/config/post-meta.json @@ -2,8 +2,6 @@ "wp_pdf_generator_show": { "default": true, "type": "boolean", - "post_types": [ - "post" - ] + "post_types": "all" } } diff --git a/src/class-generator.php b/src/class-generator.php index 816ebd1..e9cbe35 100644 --- a/src/class-generator.php +++ b/src/class-generator.php @@ -48,7 +48,7 @@ public function add_download_button( $content ): string { */ public function generate_pdf(): void { if ( - ! is_single() || + ! is_singular() || ! get_the_ID() || ! (bool) get_post_meta( get_the_ID(), 'wp_pdf_generator_show', true ) || ! isset( $_GET['download_pdf'] ) // phpcs:ignore WordPress.Security.NonceVerification.Recommended diff --git a/src/meta.php b/src/meta.php index 9ba3ab7..edb1f75 100644 --- a/src/meta.php +++ b/src/meta.php @@ -19,14 +19,14 @@ * @see \register_term_meta * * @param string $object_type The type of meta to register, which must be one of 'post' or 'term'. - * @param string[] $object_slugs The post type or taxonomy slugs to register with. + * @param string|string[] $object_slugs The post type or taxonomy slugs to register with. * @param string $meta_key The meta key to register. * @param array $args Optional. Additional arguments for register_post_meta or register_term_meta. Defaults to an empty array. * @return bool True if the meta key was successfully registered in the global array, false if not. */ function register_meta_helper( string $object_type, - array $object_slugs, + string|array $object_slugs, string $meta_key, array $args = [] ) : bool { @@ -46,7 +46,7 @@ function register_meta_helper( * * @link https://developer.wordpress.org/reference/functions/register_meta/ * - * @param array $args { + * @param array $args { * Array of args to be passed to register_meta(). * * @type string $object_subtype A subtype; e.g. if the object type is "post", the post type. If left empty, @@ -67,9 +67,9 @@ function register_meta_helper( * complex meta values this argument may optionally be an array with 'schema' * or 'prepare_callback' keys instead of a boolean. * } - * @param string $object_type The type of meta to register, which must be one of 'post' or 'term'. - * @param array $object_slugs The post type or taxonomy slugs to register with. - * @param string $meta_key The meta key to register. + * @param string $object_type The type of meta to register, which must be one of 'post' or 'term'. + * @param string|array $object_slugs The post type or taxonomy slugs to register with. + * @param string $meta_key The meta key to register. */ $args = apply_filters( 'wp_pdf_generator_register_meta_helper_args', @@ -86,6 +86,26 @@ function register_meta_helper( $meta_key ); + // Allow setting meta for all of an object type. + if ( + ( + is_array( $object_slugs ) && + count( $object_slugs ) === 1 && + 'all' === $object_slugs[0] + ) || + ( + is_string( $object_slugs ) && + 'all' === $object_slugs + ) + ) { + return register_meta( $object_type, $meta_key, $args ); + } + + // Fix potential errors since we're allowing `$object_slugs` to be a string or array. + if ( is_string( $object_slugs ) ) { + $object_slugs = [ $object_slugs ]; + } + // Fork for object type. switch ( $object_type ) { case 'post':