From 003d317d4863e4c75809734c9dadfd7375001288 Mon Sep 17 00:00:00 2001 From: Carsten Bach Date: Fri, 9 Feb 2024 08:52:50 +0100 Subject: [PATCH] Automatically fix some basic CS issues using phpcbf --- inc/sharing-image/admin-ui.php | 2 +- inc/sharing-image/frontend.php | 25 +-- inc/sharing-image/generation.php | 304 +++++++++++++++++-------------- inc/sharing-image/namespace.php | 4 +- inc/sharing-image/options.php | 2 +- inc/wordpress-seo/admin-ui.php | 8 +- inc/wordpress-seo/namespace.php | 2 +- inc/wordpress-seo/open-graph.php | 2 +- inc/wordpress-seo/options.php | 4 +- 9 files changed, 190 insertions(+), 163 deletions(-) diff --git a/inc/sharing-image/admin-ui.php b/inc/sharing-image/admin-ui.php index 7ba47af..9d2946a 100644 --- a/inc/sharing-image/admin-ui.php +++ b/inc/sharing-image/admin-ui.php @@ -59,7 +59,7 @@ function bootstrap(): void { * * @return void */ -function remove_menu() : void { +function remove_menu(): void { if ( is_super_admin() && true === constant( 'WP_DEBUG' ) ) { return; diff --git a/inc/sharing-image/frontend.php b/inc/sharing-image/frontend.php index a9892c7..a43a968 100644 --- a/inc/sharing-image/frontend.php +++ b/inc/sharing-image/frontend.php @@ -32,9 +32,14 @@ function bootstrap(): void { add_filter( 'sharing_image_hide_meta', '__return_true' ); /** - * Because 'wpseo_opengraph_image_size' FILTER IS BUGGY + * Add the generated image to yoasts opengraph output. * - * we can not use the already working + * @see https://wpset.org/sharing-image/#faq + * The docs suggest an alternative approach + * using the 'wpseo_add_opengraph_images' action to add og:images. + * + * Because 'wpseo_opengraph_image_size' FILTER IS BUGGY, + * we can not use the already working: * - 'wpseo_opengraph_image' and * - 'wpseo_twitter_image' filters. * @@ -54,20 +59,19 @@ function bootstrap(): void { * * @return Indexable_Presentation */ -function add_image_to_yoast_opengraph( Indexable_Presentation $presentation ) :Indexable_Presentation { +function add_image_to_yoast_opengraph( Indexable_Presentation $presentation ): Indexable_Presentation { $generated_image_url = esc_url( sharing_image_poster() ); if ( $generated_image_url ) { $si_options = get_option( Options\OPTION_NAME ); $si_config = get_option( 'sharing_image_config' ); - if ( - ! \is_array( $si_options ) - || ! \is_array( $si_options[0] ) - || ! \is_array( $si_config ) - || ! isset( $si_options[0]['width'] ) - || ! isset( $si_options[0]['height'] ) - || ! isset( $si_config['format'] ) + if ( ! \is_array( $si_options ) + || ! \is_array( $si_options[0] ) + || ! \is_array( $si_config ) + || ! isset( $si_options[0]['width'] ) + || ! isset( $si_options[0]['height'] ) + || ! isset( $si_config['format'] ) ) { return $presentation; } @@ -84,4 +88,3 @@ function add_image_to_yoast_opengraph( Indexable_Presentation $presentation ) :I return $presentation; } - diff --git a/inc/sharing-image/generation.php b/inc/sharing-image/generation.php index d62c0cc..06e4e6e 100644 --- a/inc/sharing-image/generation.php +++ b/inc/sharing-image/generation.php @@ -45,7 +45,7 @@ * * @return void */ -function bootstrap() :void { +function bootstrap(): void { // add_action( 'admin_init', __NAMESPACE__ . '\\load', 0 ); add_action( 'init', __NAMESPACE__ . '\\load', 0 ); // Figuren_Theater\Media\Auto_Featured_Image runs on 10 // add_action( 'admin_init', __NAMESPACE__ . '\\load', 0 ); // Figuren_Theater\Media\Auto_Featured_Image runs on 10 @@ -53,7 +53,6 @@ function bootstrap() :void { // add_action( 'updated_post_meta', __NAMESPACE__ . '\\trigger_autogeneration', 10, 4 ); - } /** @@ -61,12 +60,12 @@ function bootstrap() :void { * * @return void */ -function load() : void { +function load(): void { - //////////////////////////////////// + // // BACKEND | Autogeneration logic // // triggered on 'wp_insert_post' // - //////////////////////////////////// + // // helper (just) to get current post_id // used because it is (one of) the earliest point possible @@ -87,10 +86,10 @@ function load() : void { add_filter( 'sharing_image_autogenerated_poster', __NAMESPACE__ . '\\delete_previous_image', 10, 2 ); - //////////////////////////////////// + // // BACKEND | Autogeneration logic // // triggered on 'updated_post_meta' // - //////////////////////////////////// + // // Re-Enables default func. // of using the featured-image as background-image @@ -99,7 +98,7 @@ function load() : void { add_action( 'updated_post_meta', __NAMESPACE__ . '\\trigger_autogeneration', 10, 4 ); - // add_action( 'delete_post', __NAMESPACE__ . '\\delete_generated_image', 10, 2 ); + // add_action( 'delete_post', __NAMESPACE__ . '\\delete_generated_image', 10, 2 ); // 'delete_post' is too late, because the relevant post_meta is already deleted /** * Fires before a post is deleted, at the start of wp_delete_post(). @@ -130,15 +129,16 @@ function load() : void { * * @return bool */ -function persist_current_post( bool $disable_autogeneration, int $post_id ) : bool { - if ( wp_installing() ) +function persist_current_post( bool $disable_autogeneration, int $post_id ): bool { + if ( wp_installing() ) { return true; + } // Make sure meta is got for the post, not for its revision. if ( wp_is_post_revision( $post_id ) ) { $post_parent = get_post_parent( $post_id ); - if (null !== $post_parent) { + if ( null !== $post_parent ) { $post_id = $post_parent->ID; } } @@ -149,7 +149,6 @@ function persist_current_post( bool $disable_autogeneration, int $post_id ) : bo return $disable_autogeneration; } - // if ( ! post_type_supports( $post_pt, Sharing_Image\POST_TYPE_SUPPORT ) ) { return true; } @@ -159,24 +158,24 @@ function persist_current_post( bool $disable_autogeneration, int $post_id ) : bo return $disable_autogeneration; } -function try_to_get_post() : WP_Post|false { +function try_to_get_post(): WP_Post|false { $post = \get_post(); if ( $post instanceof WP_Post ) { return $post; } - if ( ! isset($_SERVER['REQUEST_URI'] ) || ! \is_string( $_SERVER['REQUEST_URI'] ) ) { + if ( ! isset( $_SERVER['REQUEST_URI'] ) || ! \is_string( $_SERVER['REQUEST_URI'] ) ) { return false; } $url_parts = parse_url( $_SERVER['REQUEST_URI'] ); if ( ! isset( $url_parts['path'] ) ) { return false; - } + } - $is_json_route = ( 0 === strpos($url_parts['path'], '/wp-json/wp/v2/posts/') ); - if (false === $is_json_route ) { + $is_json_route = ( 0 === strpos( $url_parts['path'], '/wp-json/wp/v2/posts/' ) ); + if ( false === $is_json_route ) { return false; } @@ -203,7 +202,7 @@ function try_to_get_post() : WP_Post|false { * * @return array|bool */ -function pre_option_sharing_image_templates( $option ) : array|bool { +function pre_option_sharing_image_templates( $option ): array|bool { // when this original option is set // during SiteSetup or within our weekly cron job @@ -214,62 +213,63 @@ function pre_option_sharing_image_templates( $option ) : array|bool { $post = try_to_get_post(); - if (false === $post ) { + if ( false === $post ) { return false; } -// global $wp, $wp_query; -error_log(var_export([ - __FUNCTION__ . '\\DOING_AJAX', - \wp_debug_backtrace_summary(), - // parse_url( $_SERVER['REQUEST_URI'] ), - // $GLOBALS, - // $wp, - // $wp_query, - // \get_post(), - $option, - 'EXTRACTED :::: ', - $post->ID, + // global $wp, $wp_query; + error_log( + var_export( + [ + __FUNCTION__ . '\\DOING_AJAX', + \wp_debug_backtrace_summary(), + // parse_url( $_SERVER['REQUEST_URI'] ), + // $GLOBALS, + // $wp, + // $wp_query, + // \get_post(), + $option, + 'EXTRACTED :::: ', + $post->ID, -],true)); -if ( defined('DOING_AJAX')) { -} + ], + true + ) + ); + if ( defined( 'DOING_AJAX' ) ) { + } $template = $option[0]; - // - $template = __get_site_logo($template); + $template = __get_site_logo( $template ); - // - $template = __get_theme_color($template); - // $template['fill'] = '#0000ff'; // Testing this filter - // $template['layers'][3]['color'] = '#0000ff'; // Testing this filter // ['layers'][3] == rectangle + $template = __get_theme_color( $template ); + // $template['fill'] = '#0000ff'; // Testing this filter + // $template['layers'][3]['color'] = '#0000ff'; // Testing this filter // ['layers'][3] == rectangle - // - $template = __get_featured_image($template, $post); + $template = __get_featured_image( $template, $post ); - // - $template = __get_shortlink($template, $post); + $template = __get_shortlink( $template, $post ); - // BEWARE - // do this last - // because the order of the layers gets modified - // and so LAYER might break - // + // BEWARE + // do this last + // because the order of the layers gets modified + // and so LAYER might break + // /* // if ('will-trigger-never' === \get_post_type( $this->current_post )) { if ('will-trigger-never' === \get_post_type( __current_post() )) { - // if ( Post_Types\Post_Type__ft_production::NAME === \get_post_type( $this->current_post )) { + // if ( Post_Types\Post_Type__ft_production::NAME === \get_post_type( $this->current_post )) { - $_new_text_layer = array ( + $_new_text_layer = array ( 'type' => 'text', 'dynamic' => 0, 'title' => 'Dauer', @@ -293,7 +293,7 @@ function pre_option_sharing_image_templates( $option ) : array|bool { array_unshift($template['layers'], $_new_text_layer); - $_new_text_layer = array ( + $_new_text_layer = array ( 'type' => 'text', 'dynamic' => 0, 'title' => 'Zielgruppe', @@ -315,16 +315,14 @@ function pre_option_sharing_image_templates( $option ) : array|bool { // $template['layers'][4] = $_new_text_layer; // v2 array_unshift($template['layers'], $_new_text_layer); - } + } */ - // - $option[0] = $template; -/* -error_log(var_export([ + $option[0] = $template; + /* + error_log(var_export([ __FUNCTION__, \wp_debug_backtrace_summary(), - // currentPost::init()->get_id(), $post->ID, $_REQUEST, $_POST, @@ -332,10 +330,9 @@ function pre_option_sharing_image_templates( $option ) : array|bool { $_SERVER, // \get_post(), // $option -],true));*/ + ],true));*/ - // - return $option; + return $option; } /** @@ -349,17 +346,18 @@ function pre_option_sharing_image_templates( $option ) : array|bool { * * @return array */ -function sharing_image_prepare_template( array $template, array $fieldset ) : array { +function sharing_image_prepare_template( array $template, array $fieldset ): array { // error_log( var_export([ - // __FUNCTION__, - // \wp_debug_backtrace_summary(), - // \get_post(), + // __FUNCTION__, + // \wp_debug_backtrace_summary(), + // \get_post(), // ],true) ); // 0. - if ( isset( $template['image'] ) && \is_string( $template['image'] ) && \esc_url( $template['image'] )) + if ( isset( $template['image'] ) && \is_string( $template['image'] ) && \esc_url( $template['image'] ) ) { return $template; + } // 1.'generate_template()' @ plugins\sharing-image\classes\class-generator.php $thumbnail_id = \get_post_thumbnail_id(); @@ -395,18 +393,22 @@ function sharing_image_prepare_template( array $template, array $fieldset ) : ar * * @return array|false */ -function delete_previous_image( array|false $poster, int $post_id ) : array|false { +function delete_previous_image( array|false $poster, int $post_id ): array|false { // return early // if we have no new image if ( false === $poster || ! isset( $poster['poster'] ) || ! \is_string( $poster['poster'] ) || ! esc_url( $poster['poster'] ) ) { return $poster; } -error_log(var_export([ - __FUNCTION__, -# currentPost::init()->get_id(), - $post_id -],true)); + error_log( + var_export( + [ + __FUNCTION__, + $post_id, + ], + true + ) + ); // compress image @@ -421,7 +423,7 @@ function delete_previous_image( array|false $poster, int $post_id ) : array|fals // !! does correct the post_id to point to the post_parent // // if ( \wp_is_post_revision( $post_id ) ) - // return false; + // return false; // grab the last image from post_meta, // before it gets updated @@ -431,16 +433,20 @@ function delete_previous_image( array|false $poster, int $post_id ) : array|fals // Make sure meta is got for the post, not for its revision. if ( wp_is_post_revision( $post_id ) ) { $post_parent = get_post_parent( $post_id ); - if (null !== $post_parent) { + if ( null !== $post_parent ) { $post_id = $post_parent->ID; } } -error_log(var_export([ - __FUNCTION__, - #currentPost::init()->get_id(), - $post_id -],true)); + error_log( + var_export( + [ + __FUNCTION__, + $post_id, + ], + true + ) + ); // get old image data, if any @@ -454,8 +460,9 @@ function delete_previous_image( array|false $poster, int $post_id ) : array|fals // return // if there was no image, yet - if ( ! is_array( $old_poster ) || ! isset( $old_poster['poster'] ) || ! \is_string( $old_poster['poster'] ) || ! esc_url( $old_poster['poster'] ) ) + if ( ! is_array( $old_poster ) || ! isset( $old_poster['poster'] ) || ! \is_string( $old_poster['poster'] ) || ! esc_url( $old_poster['poster'] ) ) { return $poster; + } // here, we know // we already had one image, @@ -463,8 +470,9 @@ function delete_previous_image( array|false $poster, int $post_id ) : array|fals // so find its path // and delete it $old_poster_path = __get_path_from_url( $old_poster['poster'] ); - if ( file_exists( $old_poster_path ) ) + if ( file_exists( $old_poster_path ) ) { unlink( $old_poster_path ); + } // bye bye // and return the un-modified, new image (data) @@ -499,30 +507,37 @@ function delete_previous_image( array|false $poster, int $post_id ) : array|fals * @param string $meta_key Metadata key. * @param mixed $_meta_value Metadata value. */ -function trigger_autogeneration( $meta_id, $object_id, $meta_key, $_meta_value ) : void { - if ('_thumbnail_id' !== $meta_key) +function trigger_autogeneration( $meta_id, $object_id, $meta_key, $_meta_value ): void { + if ( '_thumbnail_id' !== $meta_key ) { return; + } -error_log(var_export([ - __FUNCTION__ . 'BEFORE wp_is_post_revision( $object_id )', - // currentPost::init()->get_id(), - $object_id -],true)); + error_log( + var_export( + [ + __FUNCTION__ . 'BEFORE wp_is_post_revision( $object_id )', + $object_id, + ], + true + ) + ); - if ( wp_is_post_revision( $object_id ) ) + if ( wp_is_post_revision( $object_id ) ) { return; + } -error_log(var_export([ - __FUNCTION__ . 'AFTER wp_is_post_revision( $object_id )', - // currentPost::init()->get_id(), - $object_id -],true)); - -/* - - + error_log( + var_export( + [ + __FUNCTION__ . 'AFTER wp_is_post_revision( $object_id )', + $object_id, + ], + true + ) + ); + /* // helper (just) to get current post_id // used because it is (one of) the earliest point possible // to hook into the autogeneration-process @@ -543,7 +558,7 @@ function trigger_autogeneration( $meta_id, $object_id, $meta_key, $_meta_value ) -*/ + */ @@ -560,19 +575,24 @@ function trigger_autogeneration( $meta_id, $object_id, $meta_key, $_meta_value ) // make sure this is set // __current_post( $object_id ); -# currentPost::init()->get_id( $object_id ); -error_log(var_export([ - __FUNCTION__, -# currentPost::init()->get_id(), - $object_id -],true)); + // ?????????????? + + error_log( + var_export( + [ + __FUNCTION__, + $object_id, + ], + true + ) + ); // Generate new poster data using post data. // ( new Sharing_Image\Meta() )->get_poster( $object_id ); $SIW = new Sharing_Image_Plugin\Widget(); // $source = ( $SIW )->autogenerate_poster( $object_id ); // ?? not existing anymore ?? ... - $source = ( $SIW )->generate_poster( ); + $source = ( $SIW )->generate_poster(); } /** @@ -583,25 +603,27 @@ function trigger_autogeneration( $meta_id, $object_id, $meta_key, $_meta_value ) * * @return void */ -function delete_generated_image( int $post_id, WP_Post $post ) : void { +function delete_generated_image( int $post_id, WP_Post $post ): void { if ( ! post_type_supports( $post->post_type, Sharing_Image\POST_TYPE_SUPPORT ) ) { return; } - error_log(var_export([ - __FUNCTION__, - // currentPost::init()->get_id(), - $post_id -],true)); + error_log( + var_export( + [ + __FUNCTION__, + $post_id, + ], + true + ) + ); - // - $_sharing_image = get_post_meta( $post_id, Sharing_Image_Plugin\Widget::WIDGET_META, true ); + $_sharing_image = get_post_meta( $post_id, Sharing_Image_Plugin\Widget::WIDGET_META, true ); // could be replaced with // $_sharing_image = \sharing_image_poster( $post_id ); - // if ( ! \is_array( $_sharing_image ) || ! isset( $_sharing_image['poster'] ) || ! \is_string( $_sharing_image['poster'] ) || ! esc_url( $_sharing_image['poster'] ) ) { return; } @@ -609,8 +631,9 @@ function delete_generated_image( int $post_id, WP_Post $post ) : void { $_sharing_image_path = __get_path_from_url( $_sharing_image['poster'] ); // $_sharing_image_path = $this->__get_path_from_url($_sharing_image); - if (empty( $_sharing_image_path ) ) + if ( empty( $_sharing_image_path ) ) { return; + } unlink( $_sharing_image_path ); } @@ -648,20 +671,20 @@ function __prepare_text_layer( $layer_name = 'title', $new_text = '' ) { * * @return array>> */ -function __get_site_logo( array $template ) : array { +function __get_site_logo( array $template ): array { // Which index has this layer in the array of saved layers for this template? - $layer_index = (int) LAYER[ 'logo' ]; + $layer_index = (int) LAYER['logo']; // Get site-logo ID. $logo = get_option( 'site_icon' ); if ( empty( $logo ) || ! \is_string( $logo ) ) { // The plugin itself checks if 'attachment' isset(). - unset( $template['layers'][$layer_index]['attachment'] ); + unset( $template['layers'][ $layer_index ]['attachment'] ); return $template; } - $template['layers'][$layer_index]['attachment'] = $logo; + $template['layers'][ $layer_index ]['attachment'] = $logo; return $template; } @@ -673,7 +696,7 @@ function __get_site_logo( array $template ) : array { * * @return array>|string> */ -function __get_theme_color( array $template ) : array { +function __get_theme_color( array $template ): array { // get colors from gutenberg // from mu-plugins\ft_FEATURES__customizer-powered-login.php // @@ -681,9 +704,9 @@ function __get_theme_color( array $template ) : array { extract( ft_get_relevant_colors() ); // prepare color options with site-specific stuff - $template['fill'] = $ft_background; - $template['layers'][ LAYER['title'] ]['color'] = $ft_text; // ['layers'][0] == text - $template['layers'][ LAYER['url'] ]['color'] = $ft_text; // ['layers'][2] == text + $template['fill'] = $ft_background; + $template['layers'][ LAYER['title'] ]['color'] = $ft_text; // ['layers'][0] == text + $template['layers'][ LAYER['url'] ]['color'] = $ft_text; // ['layers'][2] == text $template['layers'][ LAYER['overlay'] ]['color'] = $ft_accent; // ['layers'][3] == rectangle return $template; @@ -697,14 +720,13 @@ function __get_theme_color( array $template ) : array { * * @return array */ -function __get_featured_image( array $template, WP_Post|null $post ) : array { +function __get_featured_image( array $template, WP_Post|null $post ): array { // 0. - if ( isset( $template['image'] ) && \is_string( $template['image'] ) && esc_url( $template['image'] )) + if ( isset( $template['image'] ) && \is_string( $template['image'] ) && esc_url( $template['image'] ) ) { return $template; + } // 1.'generate_template()' @ plugins\sharing-image\classes\class-generator.php - // $thumbnail_id = get_post_thumbnail_id( __current_post() ); - // $thumbnail_id = get_post_thumbnail_id( currentPost::init()->get_id() ); $thumbnail_id = get_post_thumbnail_id( $post ); if ( ! empty( $thumbnail_id ) ) { $template['image'] = get_attached_file( $thumbnail_id ); @@ -722,26 +744,29 @@ function __get_featured_image( array $template, WP_Post|null $post ) : array { * * @return array>> */ -function __get_shortlink( array $template, WP_Post|null $post ) : array { +function __get_shortlink( array $template, WP_Post|null $post ): array { // prepare url $url = null; // get url of current post or site - if ( null !== $post ) + if ( null !== $post ) { $url = wp_get_shortlink( $post->ID ); + } // fallback - if ( empty( $url ) ) + if ( empty( $url ) ) { $url = wp_get_shortlink(); + } // still empty ? - if ( empty( $url ) ) + if ( empty( $url ) ) { $url = site_url(); + } if ( esc_url( $url ) ) { // remove protocoll - $url = str_replace('https://', '', esc_url( $url ) ); + $url = str_replace( 'https://', '', esc_url( $url ) ); // set data $template['layers'][ LAYER['url'] ]['sample'] = $url; @@ -759,9 +784,8 @@ function __get_shortlink( array $template, WP_Post|null $post ) : array { * * @return string */ -function __get_path_from_url( string $url='' ) : string { - // - $wp_upload_dir = wp_get_upload_dir(); +function __get_path_from_url( string $url = '' ): string { + $wp_upload_dir = wp_get_upload_dir(); $path = str_replace( $wp_upload_dir['baseurl'], @@ -769,14 +793,14 @@ function __get_path_from_url( string $url='' ) : string { $url ); - if ( ! file_exists( $path ) ) + if ( ! file_exists( $path ) ) { return ''; + } return $path; } /* - function __current_post( int $post_id=0 ) : int { if ( ! $current_post_id ) { $current_post_id = 0; @@ -792,7 +816,7 @@ function __current_post( int $post_id=0 ) : int { * Ugly helper to persist the post_ID * during one request, but on different * action hooks - */ + class currentPost { public $id = 0; @@ -819,4 +843,4 @@ public static function init() return $instance; } -} +} */ diff --git a/inc/sharing-image/namespace.php b/inc/sharing-image/namespace.php index d292f98..5eae77e 100644 --- a/inc/sharing-image/namespace.php +++ b/inc/sharing-image/namespace.php @@ -58,7 +58,7 @@ function load_plugin(): void { add_post_type_support( 'page', POST_TYPE_SUPPORT ); \array_map( - function ( string $post_type ) : void { + function ( string $post_type ): void { add_post_type_support( $post_type, POST_TYPE_SUPPORT ); }, \get_post_types( @@ -68,7 +68,7 @@ function ( string $post_type ) : void { 'publicly_queryable' => true, ] ) - ); + ); require_once FT_VENDOR_DIR . PLUGINPATH; // phpcs:ignore WordPressVIPMinimum.Files.IncludingFile.UsingCustomConstant diff --git a/inc/sharing-image/options.php b/inc/sharing-image/options.php index 723271c..d2c8209 100644 --- a/inc/sharing-image/options.php +++ b/inc/sharing-image/options.php @@ -37,7 +37,7 @@ function bootstrap(): void { * * @return void */ -function filter_options() :void { +function filter_options(): void { $_options = [ diff --git a/inc/wordpress-seo/admin-ui.php b/inc/wordpress-seo/admin-ui.php index 4de4bc0..f8bee05 100644 --- a/inc/wordpress-seo/admin-ui.php +++ b/inc/wordpress-seo/admin-ui.php @@ -71,7 +71,7 @@ function remove_roles(): void { * * @return void */ -function remove_menus() : void { +function remove_menus(): void { remove_submenu_page( 'wpseo_dashboard', 'wpseo_workouts' ); // Remove the Premium submenu. @@ -130,7 +130,7 @@ function () { * @since 1.0.0 * @return void */ -function remove_bloat() : void { +function remove_bloat(): void { ?>