<commit_before><?php namespace Carbontwelve\Widgets\SocialFeeds;

/*
Plugin Name:        Social Feeds Widget
Plugin URI:         http://www.photogabble.co.uk
Version:            1.0.0
Description:        Various social feeds for you to template
Author:             <NAME>
Author URI:         http://www.photogabble.co.uk

License:            MIT
License URI:        http://opensource.org/licenses/MIT
*/

/** @noinspection PhpIncludeInspection */
require_once(__DIR__ . DIRECTORY_SEPARATOR . 'feeds' . DIRECTORY_SEPARATOR . 'FeedFactory.php');
/** @noinspection PhpIncludeInspection */
require_once(__DIR__ . DIRECTORY_SEPARATOR . 'libs' . DIRECTORY_SEPARATOR . 'View.php');

use Carbontwelve\Widgets\SocialFeeds\Feeds\FeedFactory;
use Carbontwelve\Widgets\SocialFeeds\Libs\View;

/**
 * Class PinterestFeedWidget
 * @package Carbontwelve\Widgets\SocialFeeds
 */
class PinterestFeedWidget extends \WP_Widget {

    /**
     * Plugin version
     *
     * @var string
     */
    const VERSION = '1.0.0';

    /**
     * Plugin Slug, used for caching
     *
     * @var string
     */
    private $slug = 'Carbontwelve_PinterestFeed_Widget';

    /**
     * Path where we are to look for views
     *
     * @var string
     */
    private $viewsPath;

    /**
     * @var string
     */
    private $defaultTemplate;

    /**
     * @var FeedFactory
     */
    private $feedFactory;

    /**
     * Register widget with WordPress
     */
    public function __construct(){
        parent::__construct(
            $this->slug,
            __('Pinterest Feed', 'carbontwelve'),
            array(
                'description' => __('Display your pinterest feed.','carbontwelve')
            )
        );

        $this->feedFactory     = new FeedFactory();
        $this->viewsPath       = __DIR__ . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR;
        $this->defaultTemplate = base64_encode( $this->viewsPath . 'frontend.php');

        // Enqueue Plugin Styles and scripts for admin pages
        add_action( 'admin_enqueue_scripts', array( $this, 'enqueueAdminScripts' ) );
    }

    /**
     * Front-end display of widget.
     *
     * @see WP_Widget::widget()
     *
     * @param array $args     Widget arguments.
     * @param array $instance Saved values from database.
     */
    public function widget( $args, $instance )
    {

        //delete_transient($this->slug);

        if ( ! ( $cache = get_transient( $this->slug ) ) ) {
            $cache = array();
            set_transient( $this->slug, $cache, 1 * HOUR_IN_SECONDS );
        }

        if ( ! isset($cache[$args['widget_id']]) ) {

            $dataProvider = $this->feedFactory->getFeed($instance['feed']);
            $dataProvider->setNumberOfItems($instance['numberOfPins']);
            $dataProvider->hydrate($instance['metaFields'][$instance['feed']]);

            $view = new View(base64_decode($instance['template']));

            // If the loaded view does not exist then roll back to a sane default
            if (!$view->exists()) {
                $view = new View(base64_decode($this->defaultTemplate));
            }

            $output = $args['before_widget'];
            $output .= $view->render(array(
                'widget' => $this,
                'title' => $args['before_title'] . apply_filters('widget_title', $instance['title']) . $args['after_title'],
                'items' => $dataProvider->execute(),
                'followSrc' => $dataProvider->getFollowSrc(),
                'feed_type' => $instance['feed'],
                'fields' => $instance['metaFields'][$instance['feed']]
            ));
            $output .= $args['after_widget'];

            $cache[ $args['widget_id'] ] = base64_encode($output);
            set_transient( $this->slug, $cache, 1 * HOUR_IN_SECONDS );

        }else{
            $output = base64_decode( $cache[$args['widget_id']] ) . '<!-- From cache -->';
        }

        echo $output;
    }

    /**
     * Back-end widget form.
     *
     * @see WP_Widget::form()
     *
     * @param array $instance Previously saved values from database.
     * @return void
     */
    public function form ( $instance )
    {

        $view = new View( $this->viewsPath . 'backend.php');
        echo $view->render(array(
            'widget'             => $this,
            'title'              => ( isset( $instance[ 'title' ] ) ) ? $instance[ 'title' ] : __( 'Title', 'carbontwelve' ),<commit_msg>Amended widget so that any exceptions thrown do not crash the page that the widget is on<commit_after><?php namespace Carbontwelve\Widgets\SocialFeeds;

/*
Plugin Name:        Social Feeds Widget
Plugin URI:         http://www.photogabble.co.uk
Version:            1.0.0
Description:        Various social feeds for you to template
Author:             <NAME>
Author URI:         http://www.photogabble.co.uk

License:            MIT
License URI:        http://opensource.org/licenses/MIT
*/

/** @noinspection PhpIncludeInspection */
require_once(__DIR__ . DIRECTORY_SEPARATOR . 'feeds' . DIRECTORY_SEPARATOR . 'FeedFactory.php');
/** @noinspection PhpIncludeInspection */
require_once(__DIR__ . DIRECTORY_SEPARATOR . 'libs' . DIRECTORY_SEPARATOR . 'View.php');

use Carbontwelve\Widgets\SocialFeeds\Feeds\FeedFactory;
use Carbontwelve\Widgets\SocialFeeds\Libs\View;

/**
 * Class PinterestFeedWidget
 * @package Carbontwelve\Widgets\SocialFeeds
 */
class PinterestFeedWidget extends \WP_Widget {

    /**
     * Plugin version
     *
     * @var string
     */
    const VERSION = '1.0.0';

    /**
     * Plugin Slug, used for caching
     *
     * @var string
     */
    private $slug = 'Carbontwelve_PinterestFeed_Widget';

    /**
     * Path where we are to look for views
     *
     * @var string
     */
    private $viewsPath;

    /**
     * @var string
     */
    private $defaultTemplate;

    /**
     * @var FeedFactory
     */
    private $feedFactory;

    /**
     * Register widget with WordPress
     */
    public function __construct(){
        parent::__construct(
            $this->slug,
            __('Pinterest Feed', 'carbontwelve'),
            array(
                'description' => __('Display your pinterest feed.','carbontwelve')
            )
        );

        $this->feedFactory     = new FeedFactory();
        $this->viewsPath       = __DIR__ . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR;
        $this->defaultTemplate = base64_encode( $this->viewsPath . 'frontend.php');

        // Enqueue Plugin Styles and scripts for admin pages
        add_action( 'admin_enqueue_scripts', array( $this, 'enqueueAdminScripts' ) );
    }

    /**
     * Front-end display of widget.
     *
     * @see WP_Widget::widget()
     *
     * @param array $args Widget arguments.
     * @param array $instance Saved values from database.
     * @return bool
     */
    public function widget( $args, $instance )
    {

        if ( ! ( $cache = get_transient( $this->slug ) ) ) {
            $cache = array();
            set_transient( $this->slug, $cache, 1 * HOUR_IN_SECONDS );
        }

        if ( ! isset($cache[$args['widget_id']]) ) {

            try {
                $dataProvider = $this->feedFactory->getFeed($instance['feed']);
                $dataProvider->setNumberOfItems($instance['numberOfPins']);
                $dataProvider->hydrate($instance['metaFields'][$instance['feed']]);

                $view = new View(base64_decode($instance['template']));

                // If the loaded view does not exist then roll back to a sane default
                if (!$view->exists()) {
                    $view = new View(base64_decode($this->defaultTemplate));
                }

                $output = $args['before_widget'];
                $output .= $view->render(array(
                    'widget' => $this,
                    'title' => $args['before_title'] . apply_filters('widget_title', $instance['title']) . $args['after_title'],
                    'items' => $dataProvider->execute(),
                    'followSrc' => $dataProvider->getFollowSrc(),
                    'feed_type' => $instance['feed'],
                    'fields' => $instance['metaFields'][$instance['feed']]
                ));
                $output .= $args['after_widget'];

                $cache[$args['widget_id']] = base64_encode($output);
                set_transient($this->slug, $cache, 1 * HOUR_IN_SECONDS);
            }
            catch( \Exception $e )
            {
                // @todo Possibly show a default view, or disable widget entirely as we do not want it breaking websites
                return false;
            }

        }else{
            $output = base64_decode( $cache[$args['widget_id']] ) . '<!-- From cache -->';
        }

        echo $output;
        return true;
    }

    /**
     * Back-end widget form.
     *
     * @see WP_Widget::form()
     *
     * @param array $instance Previously saved values from database.
     * @return void
     */
    public function form ( $instance )
    {

        $view = new View( $this->viewsPath . 'backend.php');
        echo $view->render(array(
            'widget'             => $this,
            'title'              => ( isset( $instance[ 'title' ] ) ) ? $instance[ 'title' ] : __( 'Title', 'carbontwelve' ),
{'fraction_non_alphanumeric': 0.1384053298946916, 'fraction_numerical': 0.0038684719535783366, 'mean_word_length': 2.0325839035516453, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 64, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '3057549', 'n_tokens_mistral': 2789, 'n_tokens_neox': 2663, 'n_words': 587}