Skip to content

Instantly share code, notes, and snippets.

@wpmudev-sls
Last active November 9, 2019 08:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wpmudev-sls/8757a59f01a69de4643bb619749c86be to your computer and use it in GitHub Desktop.
Save wpmudev-sls/8757a59f01a69de4643bb619749c86be to your computer and use it in GitHub Desktop.
[SmartCrawl] - Twitter Cards Full Size Images
<?php
/**
* Plugin Name: [SmartCrawl] - Twitter Cards Full Size Images
* Plugin URI: https://premium.wpmudev.org/
* Description: Forces full size images on Smartcrawl's Twitter cards
* Author: Panos Lyrakis @ WPMUDEV
* Author URI: https://premium.wpmudev.org/
* License: GPLv2 or later
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'WPMUDEV_SmartCrawl_Twitter_Cards_Full_Imgs' ) ) {
class WPMUDEV_SmartCrawl_Twitter_Cards_Full_Imgs {
private $size = 'full'; // or 'large'
private static $_instance = null;
private $twitter_printer = null;
private $_is_running = false;
private $_is_done = false;
/**
* Holds resolver instance
*
* @var Smartcrawl_Endpoint_Resolver instance
*/
private $_resolver;
public static function get_instance() {
if( is_null( self::$_instance ) ){
self::$_instance = new WPMUDEV_SmartCrawl_Twitter_Cards_Full_Imgs();
}
return self::$_instance;
}
private function __construct() {
if ( ! class_exists( 'Smartcrawl_Twitter_Printer' ) ) {
return;
}
add_action( 'init', array( $this, 'rm_default_hooks' ), 99 );
add_action( 'wp_head', array( $this, 'dispatch_tags_injection' ), 50 );
add_action( 'wds_head-after_output', array( $this, 'dispatch_tags_injection' ) );
}
public function rm_default_hooks() {
$this->twitter_printer = Smartcrawl_Twitter_Printer::get();
remove_action( 'wp_head', array( $this->twitter_printer, 'dispatch_tags_injection' ), 50 );
remove_action( 'wds_head-after_output', array( $this->twitter_printer, 'dispatch_tags_injection' ) );
}
public function dispatch_tags_injection() {
if ( ! ! $this->_is_done ) {
return false;
}
$this->_is_done = true;
if ( ! $this->is_enabled() ) {
return false;
}
$card = $this->twitter_printer->get_card_content();
$this->print_html_tag( 'card', $card );
$this->_resolver = Smartcrawl_Endpoint_Resolver::resolve();
$site = $this->twitter_printer->get_site_content();
if ( ! empty( $site ) ) {
$this->print_html_tag( 'site', $site );
}
$title = $this->twitter_printer->get_title_content();
if ( ! empty( $title ) ) {
$this->print_html_tag( 'title', $title );
}
$desc = $this->twitter_printer->get_description_content();
if ( ! empty( $desc ) ) {
$this->print_html_tag( 'description', $desc );
}
$img = $this->get_image_content();
if ( ! empty( $img ) ) {
$this->print_html_tag( 'image', $img );
}
}
public function get_image_content() {
$url = '';
if ( is_singular() && has_post_thumbnail() ) {
$url = get_the_post_thumbnail_url( null, $this->size );
}
$images = $this->get_tag_content( 'images', array( $url ) );
return empty( $images ) ? '' : $images[0];
}
private function get_tag_content( $key, $default ) {
// Check the object meta for required value
$value_from_meta = $this->get_twitter_meta( $key );
if ( ! empty( $value_from_meta ) ) {
return Smartcrawl_Replacement_Helper::replace( $value_from_meta );
}
// Check the plugin settings for required value
$value_from_settings = $this->get_twitter_setting( $key );
if ( ! empty( $value_from_settings ) ) {
return Smartcrawl_Replacement_Helper::replace( $value_from_settings );
}
return $default;
}
private function is_enabled() {
$disabled_for_object = (bool) $this->get_twitter_meta( 'disabled' );
$enabled_for_type = (bool) $this->get_twitter_setting( 'active' );
return $this->is_globally_enabled()
&& $enabled_for_type
&& ! $disabled_for_object;
}
private function get_twitter_meta( $key ) {
$meta = array();
$queried_object = $this->get_queried_object();
if ( is_a( $queried_object, 'WP_Post' ) ) {
$meta = smartcrawl_get_value( 'twitter', $queried_object->ID );
} elseif ( is_a( $queried_object, 'WP_Term' ) ) {
$meta = smartcrawl_get_term_meta( $queried_object, $queried_object->taxonomy, 'twitter' );
}
return isset( $meta[ $key ] ) ? $meta[ $key ] : '';
}
private function get_twitter_setting( $key ) {
$settings = Smartcrawl_Settings::get_options();
$resolver = Smartcrawl_Endpoint_Resolver::resolve();
$type_string = $this->get_type_string( $resolver->get_location() );
$setting_key = sprintf( 'twitter-%s-%s', $key, $type_string );
return isset( $settings[ $setting_key ] ) ? $settings[ $setting_key ] : '';
}
private function get_type_string( $location ) {
// @todo: make sure are location types from Smartcrawl_Endpoint_Resolver are handled
$mapping = array(
Smartcrawl_Endpoint_Resolver::L_BLOG_HOME => 'home',
Smartcrawl_Endpoint_Resolver::L_STATIC_HOME => 'page',
Smartcrawl_Endpoint_Resolver::L_SEARCH => 'search',
Smartcrawl_Endpoint_Resolver::L_404 => '404',
Smartcrawl_Endpoint_Resolver::L_AUTHOR_ARCHIVE => 'author',
Smartcrawl_Endpoint_Resolver::L_BP_GROUPS => 'bp_groups',
Smartcrawl_Endpoint_Resolver::L_BP_PROFILE => 'bp_profile',
Smartcrawl_Endpoint_Resolver::L_DATE_ARCHIVE => 'date',
);
$queried_object = $this->get_queried_object();
if ( is_a( $queried_object, 'WP_Post' ) ) {
$mapping[ Smartcrawl_Endpoint_Resolver::L_SINGULAR ] = get_post_type( $queried_object );
} elseif ( is_a( $queried_object, 'WP_Term' ) ) {
$mapping[ Smartcrawl_Endpoint_Resolver::L_TAX_ARCHIVE ] = $queried_object->taxonomy;
} elseif ( is_a( $queried_object, 'WP_Post_Type' ) ) {
$mapping[ Smartcrawl_Endpoint_Resolver::L_PT_ARCHIVE ] = Smartcrawl_Onpage_Settings::PT_ARCHIVE_PREFIX . $queried_object->name;
}
return isset( $mapping[ $location ] ) ? $mapping[ $location ] : '';
}
private function get_queried_object() {
$resolver = Smartcrawl_Endpoint_Resolver::resolve();
$query = $resolver->get_query_context();
return ! empty( $query ) ? $query->get_queried_object() : null;
}
private function is_globally_enabled() {
$settings = Smartcrawl_Settings::get_options();
return ! empty( $settings['twitter-card-enable'] );
}
private function get_allowed_tags() {
$allowed_tags = array(
'meta' => array(
'name' => array(),
'content' => array(),
),
);
return $allowed_tags;
}
private function print_html_tag( $type, $content ) {
echo wp_kses( $this->twitter_printer->get_html_tag( $type, $content ), $this->get_allowed_tags() );
}
}
if ( ! function_exists( 'wpmudev_smartcrawl_twitter_cards_full_imgs' ) ) {
function wpmudev_smartcrawl_twitter_cards_full_imgs(){
return WPMUDEV_SmartCrawl_Twitter_Cards_Full_Imgs::get_instance();
};
add_action( 'plugins_loaded', 'wpmudev_smartcrawl_twitter_cards_full_imgs', 10 );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment