Created
September 11, 2013 21:26
-
-
Save matthewsimo/6530009 to your computer and use it in GitHub Desktop.
Beginnings of a class for building out meta tags for twitter cards. https://dev.twitter.com/docs/cards
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /* | |
| Plugin Name: Twitter Card Generator | |
| Description: This plugin provides a class to generate the meta tag properties for a player twitter card. | |
| Version: 0.1 | |
| Author: Matthew Simo | |
| */ | |
| if(!class_exists('Twitter_Card')){ | |
| class Twitter_Card{ | |
| private static $types = array( | |
| 'summary', | |
| 'summary_large_image', | |
| 'photo', | |
| 'gallery', | |
| 'app', | |
| 'player', | |
| 'product' | |
| ); | |
| private static $reqsByType = array( | |
| 'summary' => array('title', 'description'), // https://dev.twitter.com/docs/cards/types/summary-card | |
| 'summary_large_image' => array('title', 'description'), // https://dev.twitter.com/docs/cards/large-image-summary-card | |
| 'photo' => array('image'), // https://dev.twitter.com/docs/cards/types/photo-card | |
| 'gallery' => array('image0', 'image1', 'image2', 'image3'), // https://dev.twitter.com/docs/cards/types/gallery-card | |
| 'app' => array('app:id:iphone', 'app:id:ipad', 'app:id:googleplay'), // https://dev.twitter.com/docs/cards/types/app-card | |
| 'player' => array('title', 'description', 'player', 'player:width', 'player:height', 'image'), // https://dev.twitter.com/docs/cards/types/player-card | |
| 'product' => array('title', 'description', 'image', 'data1', 'label1', 'data2', 'label2'), // https://dev.twitter.com/docs/cards/types/product-card | |
| ); | |
| public function __construct(){ | |
| } | |
| /* | |
| * Generate Player Twitter Card Tags | |
| * | |
| * Returns the html for the meta tags based on passed in settings array. | |
| * https://dev.twitter.com/docs/cards/types/player-card | |
| * | |
| * @param Array $settings Key/Value Settings, see twitter card for keys & valid values based on type. | |
| * @return String The chunk of twitter card meta tags | |
| */ | |
| public function generate($settings = array()){ | |
| // Set some defaults | |
| $default_settings = array( | |
| 'type' => 'player', | |
| 'title' => '', | |
| 'description' => '', | |
| 'player' => '', | |
| 'player:width' => '', | |
| 'player:height' => '', | |
| 'image' => '', | |
| 'player:stream' => '', | |
| 'player:stream:content_type' => '' | |
| ); | |
| // Combine passed in settings with defaults | |
| $settings = array_merge($default_settings, $settings); | |
| // Exit if some required settings aren't set and it's type player, this is ugly, replace with class method | |
| if( ( $setting['type'] == 'player' ) && ( !isset($settings['title']) || | |
| !isset($settings['description']) || | |
| !isset($settings['player']) || | |
| !isset($settings['player:width']) || | |
| !isset($settings['player:height']) || | |
| !isset($settings['image']) ) ) return; | |
| $html = ''; | |
| foreach($settings as $k => $v) { | |
| $html .= $this->build_tag($k, $v); | |
| } | |
| return $html; | |
| } | |
| /* | |
| * Build Tag | |
| * This will build the meta tag for the passed in key/value pair. | |
| * | |
| * @param String $key Settings key, we'll prepend this with 'twitter:'. | |
| * @param String $value The Value to be used for the tag. | |
| * | |
| * @return String Markup meta tag | |
| */ | |
| private function build_tag($key, $value) { | |
| return '<meta name="twitter:' . $key . '" content="' . $value . '">'."\n"; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment