Making WordPress.org

Ticket #2303: social_meta_data.4.diff

File social_meta_data.4.diff, 2.1 KB (added by joostdevalk, 8 years ago)

Catch it when banners do not exist

  • class-plugin-directory.php

     
    4141                add_filter( 'query_vars', array( $this, 'filter_query_vars' ) );
    4242                add_filter( 'single_term_title', array( $this, 'filter_single_term_title' ) );
    4343                add_filter( 'the_content', array( $this, 'filter_rel_nofollow' ) );
     44                add_action( 'wp_head', array( $this, 'social_meta_data' ) );
    4445
    4546                // Cron tasks.
    4647                add_action( 'admin_init', array( $this, 'register_cron_tasks' ) );
     
    527528        }
    528529
    529530        /**
     531         * Output social metadata
     532         */
     533        public function social_meta_data() {
     534                if ( ! is_singular( 'plugin' ) ) {
     535                        return;
     536                }
     537
     538                $post_id = get_the_ID();
     539                $banner = Template::get_plugin_banner( $post_id, 'raw' );
     540                if ( ! isset ( $banner['banner_2x'] ) ) {
     541                        $banner['banner_2x'] = isset( $banner['banner'] ) ? $banner['banner'] : false;
     542                }
     543                $icon = Template::get_plugin_icon( $post_id, 'raw' );
     544                if ( ! isset ( $icon['icon_2x'] ) ) {
     545                        $icon['icon_2x'] = isset( $icon['icon'] ) ? $icon['icon'] : false;
     546                }
     547
     548                printf( '<meta property="og:title" content="%s" />', get_the_title( $post_id ) );
     549                printf( '<meta property="og:description" content="%s" />', get_the_excerpt( $post_id ) );
     550                printf( '<meta property="og:site_name" content="WordPress.org" />' );
     551                printf( '<meta property="og:type" content="website" />' );
     552                printf( '<meta property="og:url" content="%s" />', esc_url( get_permalink( $post_id ) ) );
     553                printf( '<meta name="twitter:card" content="summary_large_image">' );
     554                printf( '<meta name="twitter:site" content="@WordPress">' );
     555                if ( $banner['banner_2x'] ) {
     556                        printf( '<meta name="twitter:image" content="%s" />', $banner['banner_2x'] );
     557                }
     558                if ( isset( $banner['banner'] ) ) {
     559                        printf( '<meta property="og:image" content="%s" />', $banner['banner'] );
     560                }
     561                if ( $icon['icon_2x'] ) {
     562                        printf( '<meta name="thumbnail" content="%s" />', $icon['icon_2x'] );
     563                }
     564        }
     565
     566        /**
    530567         * @param \WP_Query $wp_query The WordPress Query object.
    531568         */
    532569        public function pre_get_posts( $wp_query ) {