<commit_before><?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * ExpressionEngine - by EllisLab
 *
 * @package     ExpressionEngine
 * @author      ExpressionEngine Dev Team
 * @copyright   Copyright (c) 2003 - 2016, EllisLab, Inc.
 * @license     http://expressionengine.com/user_guide/license.html
 * @link        http://expressionengine.com
 * @since       Version 2.0
 * @filesource
 */

/**
 * Good at Shortcodes Extension
 *
 * @package    ExpressionEngine
 * @subpackage Addons
 * @category   Extension
 * @author     <NAME>
 * @link       https://github.com/panchesco
 */
 
/**
 * We're handling Twitter authentication with Twitter oAuth
 *
 * https://twitteroauth.com/	
 */

require_once("libraries/twitteroauth/autoload.php");
use Abraham\TwitterOAuth\TwitterOAuth;
//////////////////////////////////////////////////////
	
class Gdtshortcodes_ext {

    var $settings     = array();
    var $version			= '2.1.1';
    var $name       	= 'Good at Shortcodes';
    var $description	= 'Render embedded content via shortcodes saved in a channel entry.';
    var $settings_exist = 'y';
    var $docs_url = 'https://github.com/Panchesco/gdtshortcodes';
    var $shortcode_options = array(
    	'ig' => 'Instagram',
    	'youtube' => 'YouTube',
    	'tweet' => 'Twitter',
    	'vimeo' => 'Vimeo');
    var $connection;

    /**
     * Constructor
     *
     * @param   mixed   Settings array or empty string if none exist.
     */
    function __construct()
    {
         // Get extension settings.
				 ee()->db->select('settings');
				 ee()->db->where('class',__CLASS__);
				 $query = ee()->db->get('extensions');
				 
				 if($query->num_rows()==1)
				 {
					 $row = $query->row();
					 $this->settings = unserialize($row->settings);
				 	
				 	} else {
					 
					 $this->settings = array(
		    		'enabled_shortcodes' => array(),
		    		'twitter_consumer_key' => '',
		    		'twitter_consumer_secret' => '',
		    		'twitter_access_token' => '',
		    		'twitter_access_secret' => ''
		    	);
				 } 
				 
				 // Trim settings values. 
				 foreach($this->settings as $key => $row)
				 {
					 
					 if(is_array($row))
					 {
						 $this->settings[$key] = array_map('trim',$row);
					 } else {
						 $this->settings[$key] = trim($row);
					 }
					 
				 }
				 
    }
    
    //-----------------------------------------------------------------------------
    
		/**
		 * Activate Extension
		 *
		 * This function enters the extension into the exp_extensions table
		 *
		 * @see https://ellislab.com/codeigniter/user-guide/database/index.html for
		 * more information on the db class.
		 *
		 * @return void
		 */
		public function activate_extension()
		{

		    $data = array(
		        'class'     => __CLASS__,
		        'method'    => 'render_shortcodes',
		        'hook'      => 'template_post_parse',
		        'settings'  => serialize($this->settings),
		        'priority'  => 10,
		        'version'   => $this->version,
		        'enabled'   => 'y'
		    );
		
		    ee()->db->insert('extensions', $data);
		}
	
	//-----------------------------------------------------------------------------
	

	function settings()
	{

		   	$addons = array();
		    $all_addons = ee('Addon')->all();
		    foreach($all_addons as $name => $info)
		    {
			    $info = ee('Addon')->get($name);
			    if($info->isInstalled() && $info->get('settings_exist'))
			    {
				    $addons[$name] = $info->getName();		    
			    }
		    }
			ksort($addons);
			
			
			$settings['enabled_shortcodes'] = array('c',$this->shortcode_options,$this->settings['enabled_shortcodes']);
			$settings['twitter_consumer_key'] = array('i','',$this->settings['twitter_consumer_key']);
			$settings['twitter_consumer_secret'] = array('i','',$this->settings['twitter_consumer_secret']);
			$settings['twitter_access_token'] = array('i','',$this->settings['twitter_access_token']);
			$settings['twitter_access_secret'] = array('i','',$this->settings['twitter_access_secret']);
			

	  return $settings;
	}
	
	//-----------------------------------------------------------------------------

/**
 * Update Extension
 *
 * This function performs any necessary db updates when the extension
 * page is visited
 *
 * @return  mixed   void on update / false if none
 */
public function update_extension($current = '')
{
    if ($current == '' OR $current == $this->version)
    {
        return FALSE;
    }

    if ($current < '2.1.0')
    {
        // Update to version 2.1.0
    }

    ee()->db->where('class', __CLASS__);
    ee()->db->update(
                'extensions',
                array('version' => $this->version)
    );
 }

	//-----------------------------------------------------------------------------

/**
 * Disable Extension
 *
 * This method removes information from the exp_extensions table
 *
 * @return void
 */
public function disable_extension()
{
    ee()->db->where('class', __CLASS__);
    ee()->db->delete('extensions');
}

/**
 * Render shortcodes in parsed template.
 *
 * @return string
 */
	public function render_shortcodes()
	{
			
		$parsed_template = ee()->TMPL->template;
		

		// Instagram 
		if(in_array('ig',$this->settings['enabled_shortcodes']))
		{
				$pattern = "/\[ig .*]{1}/";
				$parsed_template = preg_replace_callback($pattern,"self::embed_instagram",$parsed_template);
		}
		
		// Twitter
		if(in_array('tweet',$this->settings['enabled_shortcodes']))
		{

			$this->connection = new TwitterOAuth($this->settings['twitter_consumer_key'],
																						$this->settings['twitter_consumer_secret'],
																						$this->settings['twitter_access_token'],
																						$this->settings['twitter_access_secret']);
			
			$pattern = "/\[tweet .*]{1}/";
			$parsed_template = preg_replace_callback($pattern,"self::embed_tweet",$parsed_template);
		
		}
		
		// YouTube 
		if(in_array('youtube',$this->settings['enabled_shortcodes']))
		{
				$pattern = "/\[youtube .*]{1}/";
				$parsed_template = preg_replace_callback($pattern,"self::embed_youtube",$parsed_template);
		}
		
		// Vimeo 
		if(in_array('vimeo',$this->settings['enabled_shortcodes']))
		{
				$pattern = "/\[vimeo .*]{1}/";
				$parsed_template = preg_replace_callback($pattern,"self::embed_vimeo",$parsed_template);
		}
	
	
		 return $parsed_template;
	
	}
	
	//-----------------------------------------------------------------------------

	/**
	 * Embed a youtube video from parsed shortcode.
	 * 
	 * @return string.
	 */
	private function embed_youtube($matches)
	{	 
		 if($matches[0])
		 {
			 
			 $url = preg_replace("/\[youtube|\s|\]/","",$matches[0]);
			 
			 $endpoint = 'https://www.youtube.com/oembed?url=' . urlencode($url . '&format=json');
			 
			 $response = $this->curl_get($endpoint);
			 
			 $obj = json_decode($response);

			 if(is_object($obj))
			 {
				 	if(isset($obj->html))
				 	{
					 return $obj->html;
				 	}
				 
				 } elseif(ee()->config->item('debug') == 1 && ee()->session->userdata('group_id') == 1) {
			 	
			 		return $response;
		 		}

		 }
		 
		 return '';
	 
	 }
	<commit_msg>Making sense of YouTube oembed handling<commit_after><?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * ExpressionEngine - by EllisLab
 *
 * @package     ExpressionEngine
 * @author      ExpressionEngine Dev Team
 * @copyright   Copyright (c) 2003 - 2016, EllisLab, Inc.
 * @license     http://expressionengine.com/user_guide/license.html
 * @link        http://expressionengine.com
 * @since       Version 2.0
 * @filesource
 */

/**
 * Good at Shortcodes Extension
 *
 * @package    ExpressionEngine
 * @subpackage Addons
 * @category   Extension
 * @author     <NAME>
 * @link       https://github.com/panchesco
 */
 
/**
 * We're handling Twitter authentication with Twitter oAuth
 *
 * https://twitteroauth.com/	
 */

require_once("libraries/twitteroauth/autoload.php");
use Abraham\TwitterOAuth\TwitterOAuth;
//////////////////////////////////////////////////////
	
class Gdtshortcodes_ext {

    var $settings     = array();
    var $version			= '2.1.1';
    var $name       	= 'Good at Shortcodes';
    var $description	= 'Render embedded content via shortcodes saved in a channel entry.';
    var $settings_exist = 'y';
    var $docs_url = 'https://github.com/Panchesco/gdtshortcodes';
    var $shortcode_options = array(
    	'ig' => 'Instagram',
    	'youtube' => 'YouTube',
    	'tweet' => 'Twitter',
    	'vimeo' => 'Vimeo');
    var $connection;

    /**
     * Constructor
     *
     * @param   mixed   Settings array or empty string if none exist.
     */
    function __construct()
    {
         // Get extension settings.
				 ee()->db->select('settings');
				 ee()->db->where('class',__CLASS__);
				 $query = ee()->db->get('extensions');
				 
				 if($query->num_rows()==1)
				 {
					 $row = $query->row();
					 $this->settings = unserialize($row->settings);
				 	
				 	} else {
					 
					 $this->settings = array(
		    		'enabled_shortcodes' => array(),
		    		'twitter_consumer_key' => '',
		    		'twitter_consumer_secret' => '',
		    		'twitter_access_token' => '',
		    		'twitter_access_secret' => ''
		    	);
				 } 
				 
				 // Trim settings values. 
				 foreach($this->settings as $key => $row)
				 {
					 
					 if(is_array($row))
					 {
						 $this->settings[$key] = array_map('trim',$row);
					 } else {
						 $this->settings[$key] = trim($row);
					 }
					 
				 }
				 
    }
    
    //-----------------------------------------------------------------------------
    
		/**
		 * Activate Extension
		 *
		 * This function enters the extension into the exp_extensions table
		 *
		 * @see https://ellislab.com/codeigniter/user-guide/database/index.html for
		 * more information on the db class.
		 *
		 * @return void
		 */
		public function activate_extension()
		{

		    $data = array(
		        'class'     => __CLASS__,
		        'method'    => 'render_shortcodes',
		        'hook'      => 'template_post_parse',
		        'settings'  => serialize($this->settings),
		        'priority'  => 10,
		        'version'   => $this->version,
		        'enabled'   => 'y'
		    );
		
		    ee()->db->insert('extensions', $data);
		}
	
	//-----------------------------------------------------------------------------
	

	function settings()
	{

		   	$addons = array();
		    $all_addons = ee('Addon')->all();
		    foreach($all_addons as $name => $info)
		    {
			    $info = ee('Addon')->get($name);
			    if($info->isInstalled() && $info->get('settings_exist'))
			    {
				    $addons[$name] = $info->getName();		    
			    }
		    }
			ksort($addons);
			
			
			$settings['enabled_shortcodes'] = array('c',$this->shortcode_options,$this->settings['enabled_shortcodes']);
			$settings['twitter_consumer_key'] = array('i','',$this->settings['twitter_consumer_key']);
			$settings['twitter_consumer_secret'] = array('i','',$this->settings['twitter_consumer_secret']);
			$settings['twitter_access_token'] = array('i','',$this->settings['twitter_access_token']);
			$settings['twitter_access_secret'] = array('i','',$this->settings['twitter_access_secret']);
			

	  return $settings;
	}
	
	//-----------------------------------------------------------------------------

/**
 * Update Extension
 *
 * This function performs any necessary db updates when the extension
 * page is visited
 *
 * @return  mixed   void on update / false if none
 */
public function update_extension($current = '')
{
    if ($current == '' OR $current == $this->version)
    {
        return FALSE;
    }

    if ($current < '2.1.0')
    {
        // Update to version 2.1.0
    }

    ee()->db->where('class', __CLASS__);
    ee()->db->update(
                'extensions',
                array('version' => $this->version)
    );
 }

	//-----------------------------------------------------------------------------

/**
 * Disable Extension
 *
 * This method removes information from the exp_extensions table
 *
 * @return void
 */
public function disable_extension()
{
    ee()->db->where('class', __CLASS__);
    ee()->db->delete('extensions');
}

/**
 * Render shortcodes in parsed template.
 *
 * @return string
 */
	public function render_shortcodes()
	{
			
		$parsed_template = ee()->TMPL->template;
		

		// Instagram 
		if(in_array('ig',$this->settings['enabled_shortcodes']))
		{
				$pattern = "/\[ig .*]{1}/";
				$parsed_template = preg_replace_callback($pattern,"self::embed_instagram",$parsed_template);
		}
		
		// Twitter
		if(in_array('tweet',$this->settings['enabled_shortcodes']))
		{

			$this->connection = new TwitterOAuth($this->settings['twitter_consumer_key'],
																						$this->settings['twitter_consumer_secret'],
																						$this->settings['twitter_access_token'],
																						$this->settings['twitter_access_secret']);
			
			$pattern = "/\[tweet .*]{1}/";
			$parsed_template = preg_replace_callback($pattern,"self::embed_tweet",$parsed_template);
		
		}
		
		// YouTube 
		if(in_array('youtube',$this->settings['enabled_shortcodes']))
		{
				$pattern = "/\[youtube .*]{1}/";
				$parsed_template = preg_replace_callback($pattern,"self::embed_youtube",$parsed_template);
		}
		
		// Vimeo 
		if(in_array('vimeo',$this->settings['enabled_shortcodes']))
		{
				$pattern = "/\[vimeo .*]{1}/";
				$parsed_template = preg_replace_callback($pattern,"self::embed_vimeo",$parsed_template);
		}
	
		 return $parsed_template;
	
	}
	
	//-----------------------------------------------------------------------------

	/**
	 * Embed a youtube video from parsed shortcode.
	 * 
	 * @return string.
	 */
	private function embed_youtube($matches)
	{	 
		 if($matches[0])
		 {
			 
			 $url = preg_replace("/\[youtube|\s|\]/","",$matches[0]);
			 
			 $endpoint = 'https://www.youtube.com/oembed?url=' . $url . '&format=json';
			 
			 $response = $this->curl_get($endpoint);
			 
			 $obj = json_decode($response);

			 if(is_object($obj))
			 {
				 	if(isset($obj->html))
				 	{
					 return $obj->html;
				 	}
				 
				 } elseif(ee()->config->item('debug') == 1 && ee()->session->userdata('group_id') == 1) {
			 	
			 		return $response;
		 		}
		 }
		 
		 return '';
	 
	 }
	
{'fraction_non_alphanumeric': 0.22326955543050084, 'fraction_numerical': 0.004220596510973551, 'mean_word_length': 2.0886378448837712, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 157, 'https://': 10, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7447706', 'n_tokens_mistral': 5009, 'n_tokens_neox': 4146, 'n_words': 737}