<commit_before><?php
if (!function_exists('http_build_query')):
    function http_build_query($query_data, $numeric_prefix='', $arg_separator='&'){
       $arr = array();
       foreach ($query_data as $key=>$val)
         $arr[] = urlencode($numeric_prefix.$key) . '=' . urlencode($val);
       return implode($arr, $arg_separator);
    }
endif;
if (!class_exists('WP_PluginBase')) require_once('lib/wp-plugin-base/wp-plugin-base.php');
if (!class_exists('InfluenceExplorer')) require_once('lib/class.influenceexplorer.php');
if (!class_exists('Snoopy')) include_once('lib/Snoopy-1.2.4/Snoopy.class.php');
if (!class_exists('Politiwidgets')){

    class Politiwidgets extends WP_PluginBase{

        var $connection,
            $influenceexplorer,
            $namespace = 'sunlight_politiwidgets',
            $title = 'Politiwidgets',
            $capability = 'manage_options',
            $settings = array(
                'sunlight_api_key'=>
                    array('name'=>'Sunlight API key',
                          'type'=>'text',
                          'value'=>''),
                'suggest'=>
                    array('name'=>'Suggest widgets',
                          'type'=>'checkbox',
                          'value'=>true),
                // 'always_on'=>
                //     array('name'=>'Include suggested widgets by default',
                //           'type'=>'checkbox',
                //           'value'=>false),
                'color'=>
                    array('name'=>'Base widget color',
                          'type'=>'text',
                          'value'=>'1F83B5'),
            ),
            // $search_base_uri = 'http://politiwidgets.com/search.json?',
            // $widget_base_uri = 'http://politiwidgets.com/embed?',
            $search_base_uri = 'http://widgets.sunlightlabs.com/search.json?',
            $widget_base_uri = 'http://widgets.sunlightlabs.com/embed?',
            $api_default_params = array( 'format' => 'json', ),
            $widget_meta_key,
            $widget_sizes = array(
                'lg' => '400x300',
                'med' => '300x250',
                'sm' => '160x300',
            ),
            $widget_types = array(
                'bio' => 'Business Card',
                'bill' => 'Vote Report',
                'contractors' => 'Top Contractors',
                'sponsorships' => 'Sponsorships',
                'contributions' => 'Campaign Contributions',
                'top_contributions' => 'Top Contributors',
                'earmarks' => 'Earmarks',
                'ratings' => 'Interest Group Ratings',
                'district' => 'District Map',
                'parties' => 'Party Time',
            ),
            $candidate_widgets = array('bio', 'district', 'parties', 'contributions', 'top_contributors',),
            $incumbent_widgets = array('bio', 'bill', 'contractors', 'sponsorships', 'contributions',
                                       'top_contributions', 'earmarks', 'ratings', 'district', 'parties',),
            $entity_cache_meta_key;

        function Politiwidgets(){
            /* call the php5 constructor */
            $this->__construct();
        }

        function __construct(){
            /* set some options */
            $this->connection = new Snoopy;
            $this->widget_meta_key = '_' . $this->namespace . '_widgets';
            $this->entity_cache_meta_key = '_' . $this->namespace . '_entity_cache';

            /* register hooks */
            add_action('admin_enqueue_scripts', array(&$this, 'enqueue_scripts'));
            add_action('admin_init', array(&$this, 'add_meta_box'));
            add_action('admin_notices', array(&$this, 'api_key_notice'));
            add_action('save_post', array(&$this, 'save_post_handler'));
            add_filter('the_content', array(&$this, 'the_content_handler'));
            add_shortcode('politiwidget', array(&$this, 'shortcode_handler'));

            /* super */
            parent::__construct();

            /* after settings are loaded */
            $this->influenceexplorer = new InfluenceExplorer($this->setting('sunlight_api_key'));
        }

        function enqueue_scripts($hook){
            // only need extra css/js for post pages
            if (!in_array($hook, array('edit.php', 'post.php', 'settings_page_sunlight_politiwidgets_settings_page'))) return;

            // css
            wp_enqueue_style('farbtastic');

            wp_register_style('politiwidgets', WP_PLUGIN_URL . '/' . basename(dirname(__FILE__)) . '/css/politiwidgets.css');
            wp_enqueue_style('politiwidgets');

            // js
            wp_enqueue_script('json2');
            wp_enqueue_script('jquery');
            wp_enqueue_script('jquery-ui-core');
            wp_enqueue_script('jquery-ui-sortable');
            wp_enqueue_script('suggest');
            wp_enqueue_script('farbtastic');


            wp_register_script('jquery-template', WP_PLUGIN_URL . '/' . basename(dirname(__FILE__)) . '/js/jquery.tmpl.min.js');
            wp_enqueue_script('jquery-template');

            wp_register_script('politiwidgets', WP_PLUGIN_URL . '/' . basename(dirname(__FILE__)) . '/js/politiwidgets.js');
            wp_enqueue_script('politiwidgets');
        }

        function api_key_notice(){
            if (!$this->setting('sunlight_api_key'))
                echo '<div class="error"><p>You need to set a <a href="' .
                      admin_url() . 'options-general.php?page=' .
                      $this->namespace .
                      '_settings_page">Sunlight API Key</a> before Politiwidgets will work.</p></div>';
        }

        function add_meta_box(){
            add_meta_box($this->namespace,
                         __($this->title, $this->namespace . '_textdomain'),
                         array(&$this, 'meta_box'),
                         'post');
        }

        function meta_box($post){
            $active_widgets = get_post_meta($post->ID, $this->widget_meta_key);
            $active_widgets = array_map(array(&$this, '_decode_widget'), $active_widgets);
            $context = array('active_widgets'=>$active_widgets,
                             'suggested_widgets'=>$suggested_widgets,
                             'namespace'=>$this->namespace,
                             'get_suggestions'=>$this->setting('suggest'),
                             'widget_meta_key'=>$this->widget_meta_key,
                             'widget_types'=>$this->widget_types,
                             'candidate_widgets'=>$this->candidate_widgets,
                             'incumbent_widgets'=>$this->incumbent_widgets,);

            uTemplate::render(dirname(__FILE__) . '/templates/meta_box.php', $context);
        }

        function the_content_handler($content){
            global $post;
            $widgets = get_post_meta($post->ID, $this->widget_meta_key);
            foreach($widgets as $widget):
                $_widget = $widget;
                $widget = $this->_decode_widget($widget);

                // make sure we have the latest color
                if(!preg_match('/'.$this->_get_color().'/', $widget->url)):
                    $widget->url = preg_replace('/color\=[0-9a-fA-F]{6}/', 'color=' . $this->_get_color(), $widget->url);
                    update_post_meta($post->ID, $this->widget_meta_key, $_widget);
                endif;

                $content .= "\n" .
                            '<script type="text/javascript" src="' .
                            $widget->url .
                            '&s=lg"></script>';
            endforeach;
            return $content;
        }

        function save_post_handler($post_id){
            if (!wp_verify_nonce($_POST[$this->namespace . '_nonce'], plugin_basename(dirname(__FILE__)))):
                return false;
            endif;
            if (!current_user_can('edit_post', $post_id)) return false;

            $post = get_post($post_id);

            // Run this block only when we save explicitly
            if (!wp_is_post_revision($post)):

                // empty the entity cache if suggestions are on so it will be
                // reloaded on the next request.
                if ($this->setting('suggest')):
                    update_post_meta($post->ID, $this->entity_cache_meta_key, '');
                endif;

                // get all attached widgets
                $new_widgets = array_map(array(&$this, '_decode_widget'), $_REQUEST[$this->widget_meta_key]);

                // delete old widgets for this post
                delete_post_meta($post->ID, $this->widget_meta_key);

                // add the submitted ones
                $widgets = array_map('json_encode', $new_widgets);
                foreach ($widgets as $widget):
                    add_post_meta($post->ID, $this->widget_meta_key, $widget);<commit_msg>Allow meta key to be missing from POST array<commit_after><?php
if (!function_exists('http_build_query')):
    function http_build_query($query_data, $numeric_prefix='', $arg_separator='&'){
       $arr = array();
       foreach ($query_data as $key=>$val)
         $arr[] = urlencode($numeric_prefix.$key) . '=' . urlencode($val);
       return implode($arr, $arg_separator);
    }
endif;
if (!class_exists('WP_PluginBase')) require_once('lib/wp-plugin-base/wp-plugin-base.php');
if (!class_exists('InfluenceExplorer')) require_once('lib/class.influenceexplorer.php');
if (!class_exists('Snoopy')) include_once('lib/Snoopy-1.2.4/Snoopy.class.php');
if (!class_exists('Politiwidgets')){

    class Politiwidgets extends WP_PluginBase{

        var $connection,
            $influenceexplorer,
            $namespace = 'sunlight_politiwidgets',
            $title = 'Politiwidgets',
            $capability = 'manage_options',
            $settings = array(
                'sunlight_api_key'=>
                    array('name'=>'Sunlight API key',
                          'type'=>'text',
                          'value'=>''),
                'suggest'=>
                    array('name'=>'Suggest widgets',
                          'type'=>'checkbox',
                          'value'=>true),
                // 'always_on'=>
                //     array('name'=>'Include suggested widgets by default',
                //           'type'=>'checkbox',
                //           'value'=>false),
                'color'=>
                    array('name'=>'Base widget color',
                          'type'=>'text',
                          'value'=>'1F83B5'),
            ),
            $search_base_uri = 'http://politiwidgets.com/search.json?',
            $widget_base_uri = 'http://politiwidgets.com/embed?',
            $api_default_params = array( 'format' => 'json', ),
            $widget_meta_key,
            $widget_sizes = array(
                'lg' => '400x300',
                'med' => '300x250',
                'sm' => '160x300',
            ),
            $widget_types = array(
                'bio' => 'Business Card',
                'bill' => 'Vote Report',
                'contractors' => 'Top Contractors',
                'sponsorships' => 'Sponsorships',
                'contributions' => 'Campaign Contributions',
                'top_contributions' => 'Top Contributors',
                'earmarks' => 'Earmarks',
                'ratings' => 'Interest Group Ratings',
                'district' => 'District Map',
                'parties' => 'Party Time',
            ),
            $candidate_widgets = array('bio', 'district', 'parties', 'contributions', 'top_contributors',),
            $incumbent_widgets = array('bio', 'bill', 'contractors', 'sponsorships', 'contributions',
                                       'top_contributions', 'earmarks', 'ratings', 'district', 'parties',),
            $entity_cache_meta_key;

        function Politiwidgets(){
            /* call the php5 constructor */
            $this->__construct();
        }

        function __construct(){
            /* set some options */
            $this->connection = new Snoopy;
            $this->widget_meta_key = '_' . $this->namespace . '_widgets';
            $this->entity_cache_meta_key = '_' . $this->namespace . '_entity_cache';

            /* register hooks */
            add_action('admin_enqueue_scripts', array(&$this, 'enqueue_scripts'));
            add_action('admin_init', array(&$this, 'add_meta_box'));
            add_action('admin_notices', array(&$this, 'api_key_notice'));
            add_action('save_post', array(&$this, 'save_post_handler'));
            add_filter('the_content', array(&$this, 'the_content_handler'));
            add_shortcode('politiwidget', array(&$this, 'shortcode_handler'));

            /* super */
            parent::__construct();

            /* after settings are loaded */
            $this->influenceexplorer = new InfluenceExplorer($this->setting('sunlight_api_key'));
        }

        function enqueue_scripts($hook){
            // only need extra css/js for post pages
            if (!in_array($hook, array('edit.php', 'post.php', 'settings_page_sunlight_politiwidgets_settings_page'))) return;

            // css
            wp_enqueue_style('farbtastic');

            wp_register_style('politiwidgets', WP_PLUGIN_URL . '/' . basename(dirname(__FILE__)) . '/css/politiwidgets.css');
            wp_enqueue_style('politiwidgets');

            // js
            wp_enqueue_script('json2');
            wp_enqueue_script('jquery');
            wp_enqueue_script('jquery-ui-core');
            wp_enqueue_script('jquery-ui-sortable');
            wp_enqueue_script('suggest');
            wp_enqueue_script('farbtastic');


            wp_register_script('jquery-template', WP_PLUGIN_URL . '/' . basename(dirname(__FILE__)) . '/js/jquery.tmpl.min.js');
            wp_enqueue_script('jquery-template');

            wp_register_script('politiwidgets', WP_PLUGIN_URL . '/' . basename(dirname(__FILE__)) . '/js/politiwidgets.js');
            wp_enqueue_script('politiwidgets');
        }

        function api_key_notice(){
            if (!$this->setting('sunlight_api_key'))
                echo '<div class="error"><p>You need to set a <a href="' .
                      admin_url() . 'options-general.php?page=' .
                      $this->namespace .
                      '_settings_page">Sunlight API Key</a> before Politiwidgets will work.</p></div>';
        }

        function add_meta_box(){
            add_meta_box($this->namespace,
                         __($this->title, $this->namespace . '_textdomain'),
                         array(&$this, 'meta_box'),
                         'post');
        }

        function meta_box($post){
            $active_widgets = get_post_meta($post->ID, $this->widget_meta_key);
            $active_widgets = array_map(array(&$this, '_decode_widget'), $active_widgets);
            $context = array('active_widgets'=>$active_widgets,
                             'suggested_widgets'=>$suggested_widgets,
                             'namespace'=>$this->namespace,
                             'get_suggestions'=>$this->setting('suggest'),
                             'widget_meta_key'=>$this->widget_meta_key,
                             'widget_types'=>$this->widget_types,
                             'candidate_widgets'=>$this->candidate_widgets,
                             'incumbent_widgets'=>$this->incumbent_widgets,);

            uTemplate::render(dirname(__FILE__) . '/templates/meta_box.php', $context);
        }

        function the_content_handler($content){
            global $post;
            $widgets = get_post_meta($post->ID, $this->widget_meta_key);
            foreach($widgets as $widget):
                $_widget = $widget;
                $widget = $this->_decode_widget($widget);

                // make sure we have the latest color
                if(!preg_match('/'.$this->_get_color().'/', $widget->url)):
                    $widget->url = preg_replace('/color\=[0-9a-fA-F]{6}/', 'color=' . $this->_get_color(), $widget->url);
                    update_post_meta($post->ID, $this->widget_meta_key, $_widget);
                endif;

                $content .= "\n" .
                            '<script type="text/javascript" src="' .
                            $widget->url .
                            '&s=lg"></script>';
            endforeach;
            return $content;
        }

        function save_post_handler($post_id){
            if (!wp_verify_nonce($_POST[$this->namespace . '_nonce'], plugin_basename(dirname(__FILE__)))):
                return false;
            endif;
            if (!current_user_can('edit_post', $post_id)) return false;

            $post = get_post($post_id);
            $posted_widgets = $_REQUEST[$this->widget_meta_key];
            if (!is_array($posted_widgets))
                $posted_widgets = Array();

            // Run this block only when we save explicitly
            if (!wp_is_post_revision($post)):

                // empty the entity cache if suggestions are on so it will be
                // reloaded on the next request.
                if ($this->setting('suggest')):
                    update_post_meta($post->ID, $this->entity_cache_meta_key, '');
                endif;

                // get all attached widgets
                $new_widgets = array_map(array(&$this, '_decode_widget'), $posted_widgets);

                // delete old widgets for this post
                delete_post_meta($post->ID, $this->widget_meta_key);

                // add the submitted ones
                $widgets = array_map('json_encode', $new_widgets);
                foreach ($widgets as $widget):
                    add_post_meta($post->ID, $this->widget_meta_key, $widget);
{'fraction_non_alphanumeric': 0.14409673627050326, 'fraction_numerical': 0.0033588982813637126, 'mean_word_length': 1.8221169036334912, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 179, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4356161', 'n_tokens_mistral': 5070, 'n_tokens_neox': 4739, 'n_words': 815}