Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PEERS-606: [v1.0.0] Core post types integration #34

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions config/post-meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
"wp_pdf_generator_show": {
"default": true,
"type": "boolean",
"post_types": [
"post"
]
"post_types": "all"
}
}
2 changes: 1 addition & 1 deletion src/class-generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 26 additions & 6 deletions src/meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, mixed> $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 {
Expand All @@ -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,
Expand All @@ -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',
Expand All @@ -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':
Expand Down