Plugin Directory

Changeset 1501047


Ignore:
Timestamp:
09/23/2016 11:57:38 AM (9 years ago)
Author:
Aptivate
Message:

v1.0.3

Location:
climate-tagger/trunk
Files:
2 added
4 edited

Legend:

Unmodified
Added
Removed
  • climate-tagger/trunk/climate-tagger.php

    r1209707 r1501047  
    33  Plugin Name: Climate Tagger
    44  Description: Recommends tags in a tag cloud based on Climate Tagger API.
    5   Version: 1.0.2
     5  Version: 1.0.3
    66  Author: Aptivate
    77*/
     
    1313}
    1414
     15// Documentation: http://api.climatetagger.net/documentation/
     16define( 'CLIMATE_TAGGER_API_URL', 'http://api.climatetagger.net' );
     17
    1518class ClimateTagger {
    1619
    17     function admin_menu() {
     20    static function admin_menu() {
    1821        add_options_page(
    1922            'Climate Tagger',
     
    2124            'manage_options',
    2225            'climate-tagger',
    23             array( 'ClimateTagger', 'add_options_page_callback' ));
    24 
    25     }
    26 
    27     function admin_init()
    28     {
     26            array( 'ClimateTagger', 'add_options_page_callback' )
     27        );
     28
     29    }
     30
     31    static function admin_init() {
    2932        self::set_defaults();
    3033
     
    3336            'climate_tagger_general_settings'
    3437        );
    35     }
    36 
    37     function set_defaults() {
    38 
     38
     39        // Add an action link pointing to the settings page.
     40        add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), array(
     41            'ClimateTagger',
     42            'add_action_links',
     43        ) );
     44    }
     45
     46    static function set_defaults() {
    3947        $options = get_option( 'climate_tagger_general_settings' );
    4048
     
    4250            $options,
    4351            array(
    44                 'token' => '',
    45                 'limit' => '20',
     52                'token'      => '',
     53                'limit'      => '20',
    4654                'post_types' => 'post',
    47             ) );
     55            )
     56        );
    4857
    4958        update_option( 'climate_tagger_general_settings', $options );
    50 
    51     }
    52 
    53     function add_options_page_callback() {
     59    }
     60
     61    static function add_action_links( $links ) {
     62        return array_merge(
     63            array(
     64                'settings' => '<a href="' . admin_url( 'options-general.php?page=climate-tagger' ) . '">Settings</a>',
     65            ),
     66            $links
     67        );
     68    }
     69
     70    static function add_options_page_callback() {
    5471        ?>
    5572        <div class="wrap">
    56         <h2>Climate Tagger by Aptivate</h2>
    57 
    58         <div>
    59 
    60         <form method="post" action="options.php">
    61 
    62 <?php
    63         settings_fields( 'climate_tagger_general_settings' );
    64         $options = get_option( 'climate_tagger_general_settings' );
    65 
    66         ?>
    67         <h3>General Settings</h3>
    68 
    69         <table class="form-table">
    70         <tr valign="top">
    71         <th scope="row">Authentication token:</th>
    72         <td>
    73 <?php
    74         printf(
    75             '<input type="text" id="climate-tagger-token" name="climate_tagger_general_settings[token]" value="%s" size="50" />',
    76             esc_attr( $options['token'] )
    77         );
    78         echo '<br /><span class="description">A valid authentication token that has been generated in the Climate Tagger API dashboard. <a href="http://api.climatetagger.net/register/" target="_blank">http://api.climatetagger.net/register</a></span>';
    79         ?>
    80         </td>
    81         </tr>
    82 
    83         <tr valign="top">
    84         <th scope="row">Post types:</th>
    85         <td>
    86 <?php
    87         printf(
    88             '<input type="text" id="climate-tagger-post-types" name="climate_tagger_general_settings[post_types]" value="%s" />',
    89             esc_attr( $options['post_types'] )
    90         );
    91         echo '<br /><span class="description">Supported post types, separated by commas.</span>';
    92         ?>
    93         </td>
    94         </tr>
    95 
    96         <tr valign="top">
    97         <th scope="row">Maximum number of tags:</th>
    98         <td>
    99 <?php
    100         printf(
    101             '<input type="text" id="climate-tagger-limit" name="climate_tagger_general_settings[limit]" value="%s" size="5" />',
    102             esc_attr( $options['limit'] )
    103         );
    104         echo '<br /><span class="description">Maximum number of tags to retrieve from the Climate Tagger API and display in the word cloud.</span>';
    105         ?>
    106         </td>
    107         </tr>
    108 
    109         </table>
    110 
    111 
    112 <?php
    113         submit_button();
    114         ?>
    115 
    116         </form>
    117 
    118               </div>
    119 
    120                                             </div>
    121 <?php
    122 
    123     }
    124 
    125     function add_box() {
     73            <h2>Climate Tagger by Aptivate</h2>
     74
     75            <div>
     76                <form method="post" action="options.php">
     77                    <?php
     78                    settings_fields( 'climate_tagger_general_settings' );
     79                    $options = get_option( 'climate_tagger_general_settings' );
     80                    ?>
     81
     82                    <h3>General Settings</h3>
     83
     84                    <table class="form-table">
     85                        <tr valign="top">
     86                            <th scope="row">Authentication token:</th>
     87                            <td>
     88                                <?php
     89                                printf(
     90                                    '<input type="text" id="climate-tagger-token" name="climate_tagger_general_settings[token]" value="%s" size="50" />',
     91                                    esc_attr( $options['token'] )
     92                                );
     93                                echo '<br /><span class="description">A valid authentication token that has been generated in the Climate Tagger API dashboard. <a href="http://api.climatetagger.net/register/" target="_blank">http://api.climatetagger.net/register</a></span>';
     94                                ?>
     95                            </td>
     96                        </tr>
     97
     98                        <tr valign="top">
     99                            <th scope="row">Climate Thesaurus:</th>
     100                            <td>
     101                                <?php
     102                                $projects = self::get_climate_tagger_projects();
     103                                if ( empty( $projects ) ) {
     104                                    echo 'Please check your authentication token above and click the "Save Changes" button before you can select a Climate Thesaurus.';
     105                                } else {
     106                                    echo '<select id="climate-tagger-project" name="climate_tagger_general_settings[project]">';
     107                                    foreach ( $projects as $project ) {
     108                                        $selected = ( $project == $options['project'] ) ? 'selected="selected"' : '';
     109                                        echo '<option value="' . $project . '" ' . $selected . '>' . $project . '</option>';
     110                                    }
     111                                    echo '</select>';
     112                                    echo '<br/><span class="description">Depending on the focus of your resources, you can now select a specific sector of the Climate Tagger or use all sectors by selecting "Select all (Climate Tagger)".<br />This new feature allows even more targeted tagging of your clean energy and climate resources.</span>';
     113                                }
     114                                ?>
     115                            </td>
     116                        </tr>
     117
     118                        <tr valign="top">
     119                            <th scope="row">Post types:</th>
     120                            <td>
     121                                <?php
     122                                printf(
     123                                    '<input type="text" id="climate-tagger-post-types" name="climate_tagger_general_settings[post_types]" value="%s" />',
     124                                    esc_attr( $options['post_types'] )
     125                                );
     126                                echo '<br /><span class="description">Supported post types, separated by commas.</span>';
     127                                ?>
     128                            </td>
     129                        </tr>
     130
     131                        <tr valign="top">
     132                            <th scope="row">Maximum number of tags:</th>
     133                            <td>
     134                                <?php
     135                                printf(
     136                                    '<input type="text" id="climate-tagger-limit" name="climate_tagger_general_settings[limit]" value="%s" size="5" />',
     137                                    esc_attr( $options['limit'] )
     138                                );
     139                                echo '<br /><span class="description">Maximum number of tags to retrieve from the Climate Tagger API and display in the word cloud.</span>';
     140                                ?>
     141                            </td>
     142                        </tr>
     143
     144                    </table>
     145
     146                    <?php
     147                    submit_button();
     148                    ?>
     149
     150                </form>
     151            </div>
     152        </div>
     153        <?php
     154    }
     155
     156    static function add_box() {
    126157        $options = get_option( 'climate_tagger_general_settings' );
    127158
     
    132163                'boxid',
    133164                'Suggested Tags (Climate Tagger)',
    134                 array( ClimateTagger, 'box_routine' ),
     165                array( 'ClimateTagger', 'box_routine' ),
    135166                trim( $post_type ),
    136167                'side',
    137                 'low');
    138         }
    139     }
    140 
    141     function box_routine() {
     168                'low'
     169            );
     170        }
     171    }
     172
     173    static function box_routine() {
    142174        $response = self::get_climate_tagger_response();
    143175
    144176        if ( is_wp_error( $response ) ) {
    145177            echo $response->get_error_message();
     178
    146179            return;
    147180        }
     
    149182        if ( $response['response']['code'] != 200 ) {
    150183            echo $response['body'];
     184
    151185            return;
    152186        }
     
    155189        if ( count( $tags_post ) == 0 ) {
    156190            echo "Click 'Save Draft' to refresh tag suggestions.";
     191
    157192            return;
    158193        }
     
    161196    }
    162197
    163     function print_tag_cloud( $tags_rec ) {
     198    static function print_tag_cloud( $tags_rec ) {
    164199        arsort( $tags_rec );
    165200
     
    178213
    179214        foreach ( $tags_rec as $tag_name => $tag_strength ) {
    180             $size = $min_size + ($tag_strength - $minimum_strength) * $step;
    181                 ?>
    182                 <a href="#" style="font-size: <?php echo "$size"?>pt;" onClick="tag_add('<?php echo $tag_name; ?>');return false;"><?php echo "$tag_name"?></a>
    183 <?php
     215            $size = $min_size + ( $tag_strength - $minimum_strength ) * $step;
     216            ?>
     217            <a href="#" style="font-size: <?php echo "$size" ?>pt;"
     218               onClick="tag_add('<?php echo $tag_name; ?>');return false;"><?php echo "$tag_name" ?></a>
     219            <?php
    184220        }
    185221
     
    187223    }
    188224
    189     function get_climate_tagger_response() {
     225    static function get_climate_tagger_projects() {
     226        $options = get_option( 'climate_tagger_general_settings' );
     227        $url     = CLIMATE_TAGGER_API_URL . '/service/projects';
     228        $url     = $url . '?token=' . $options['token'];
     229
     230        $response = wp_remote_get( $url );
     231
     232        if ( is_wp_error( $response ) ) {
     233            return array();
     234        }
     235
     236        if ( $response['response']['code'] != 200 ) {
     237            return array();
     238        }
     239
     240        $projects = self::get_projects_from_response( $response );
     241        if ( count( $projects ) == 0 ) {
     242            return array();
     243        }
     244
     245        return $projects;
     246    }
     247
     248    static function get_projects_from_response( $response ) {
     249        $result   = json_decode( $response['body'], TRUE );
     250        $projects = array();
     251
     252        if ( ! empty( $result ) ) {
     253            foreach ( $result as $project ) {
     254                $projects[] = trim( $project );
     255            }
     256        }
     257
     258        return $projects;
     259    }
     260
     261    static function get_climate_tagger_response() {
    190262        global $post;
    191263
    192         $content = $post->post_title .  ' ' . $post->post_content;
     264        $content = $post->post_title . ' ' . $post->post_content;
    193265
    194266        $content = apply_filters(
     
    201273        }
    202274
    203         // http://api.climatetagger.net/documentation/
    204 
    205         $url = 'http://api.reegle.info/service/extract';
    206 
    207         $options = get_option( 'climate_tagger_general_settings' );
    208 
     275        $url      = CLIMATE_TAGGER_API_URL . '/service/extract';
     276        $options  = get_option( 'climate_tagger_general_settings' );
    209277        $language = apply_filters( 'climate-tagger-language', 'en', $post );
    210278
    211279        $fields = array(
    212             'text' => $content,
    213             'locale' => $language,
    214             'format' => 'json',
    215             'token' => $options['token'],
     280            'text'          => $content,
     281            'locale'        => $language,
     282            'format'        => 'json',
     283            'token'         => $options['token'],
    216284            'countConcepts' => $options['limit'],
    217285        );
    218286
     287        if ( isset( $options['project'] ) ) {
     288            $fields['tagger'] = $options['project'];
     289        }
     290
    219291        return wp_remote_post( $url, array( 'body' => $fields ) );
    220292    }
    221293
    222     function get_tags_from_response( $response ) {
    223         $result = json_decode( $response['body'], true );
     294    static function get_tags_from_response( $response ) {
     295        $result = json_decode( $response['body'], TRUE );
    224296
    225297        $concepts = $result['concepts'];
     
    234306    }
    235307
    236     function admin_add_my_script()
    237     {
     308    static function admin_add_my_script() {
    238309        wp_enqueue_script(
    239310            'climate-tagger-add-tag',
     
    247318    add_action( 'admin_menu', array( 'ClimateTagger', 'admin_menu' ) );
    248319    add_action( 'admin_init', array( 'ClimateTagger', 'admin_init' ) );
    249 
    250     add_action( 'admin_menu',
    251                 array( 'ClimateTagger', 'add_box' ) );
    252     add_action( 'admin_print_scripts',
    253                 array( 'ClimateTagger', 'admin_add_my_script' ) );
     320    add_action( 'admin_menu', array( 'ClimateTagger', 'add_box' ) );
     321    add_action( 'admin_print_scripts', array( 'ClimateTagger', 'admin_add_my_script' ) );
    254322}
    255323?>
  • climate-tagger/trunk/readme.txt

    r1209707 r1501047  
    33Tags: tags, tag cloud, suggestion, tag suggestion, climate tagger, api
    44Requires at least: 3.7
    5 Tested up to: 4.2.2
    6 Stable tag: 1.0.2
     5Tested up to: 4.6.1
     6Stable tag: 1.0.3
    77License: GPLv2 or later
    88License URI: http://www.gnu.org/licenses/gpl-2.0.html
     
    4343
    4444== Changelog ==
     45
     46= 1.0.3 =
     47Props [@swc-pdi](https://github.com/swc-pdi)
     48* Added the possibility to select a certain project in the Climate Tagger configuration
     49* Adapted methods after ClimateTagger API changes
    4550
    4651= 1.0.2 =
  • climate-tagger/trunk/tests/mock-option.php

    r1209707 r1501047  
    1010                'limit' => '',
    1111                'token' => '',
     12                'project' => '',
    1213            );
    1314
  • climate-tagger/trunk/tests/test-plugin.php

    r1209707 r1501047  
    33require_once 'climate-tagger.php';
    44require_once 'mock-remote-post.php';
     5require_once 'mock-remote-get.php';
    56require_once 'mock-option.php';
    67
     
    3637        $tagger = new ClimateTagger();
    3738
    38         $post = $this->get_new_post();
     39        $post             = $this->get_new_post();
    3940        $post->post_title = 'FEATURE: Three Steps to Decarbonising Development for a Zero-Carbon Future';
    4041
     
    5859        $post = $this->get_new_post();
    5960
    60         $content = <<<EOT
     61        $content            = <<<EOT
    6162A new World Bank report lays out three steps for a smooth transition to a zero-carbon future. Through data, examples and policy advice, it aims to help countries makes the shift. It tells us that to prevent temperatures from rising more than 2 degrees Celsius, the world will need to transform its energy uses and electricity from clean energy sources will play an important role.
    6263EOT;
     
    7172        $this->assertThat(
    7273            $text,
    73             $this->stringContains( $content )
     74            $this->stringContains( $content )
    7475        );
    7576    }
     
    174175    }
    175176
     177    public function test_project_selection_retrieved_from_options() {
     178        $tagger = new ClimateTagger();
     179
     180        $this->set_option( 'project',
     181            'Climate Change Adaptation' );
     182
     183        $this->get_new_post();
     184
     185        $tagger->get_climate_tagger_response();
     186
     187        global $_CLIMATE_TAGGER_MOCK_POST;
     188
     189        $project = $_CLIMATE_TAGGER_MOCK_POST['tagger'];
     190
     191        $this->assertThat(
     192            $project,
     193            $this->equalTo( 'Climate Change Adaptation' )
     194        );
     195    }
     196
    176197    public function test_api_url() {
    177198        $tagger = new ClimateTagger();
     
    185206        $this->assertThat(
    186207            $_CLIMATE_TAGGER_MOCK_URL,
    187             $this->equalTo( 'http://api.reegle.info/service/extract' )
     208            $this->equalTo( 'http://api.climatetagger.net/service/extract' )
    188209        );
    189210    }
     
    197218
    198219        $_CLIMATE_TAGGER_MOCK_RESPONSE = array(
    199             'body' => json_encode(array(
     220            'body'     => json_encode( array(
    200221                'concepts' => array(
    201222                    array(
    202223                        'prefLabel' => 'climate change',
    203                         'score' => 20,
     224                        'score'     => 20,
    204225                    ),
    205226                    array(
    206227                        'prefLabel' => 'IPPC',
    207                         'score' => 1,
     228                        'score'     => 1,
    208229                    ),
    209230                    array(
    210231                        'prefLabel' => 'energy',
    211                         'score' => 10,
     232                        'score'     => 10,
    212233                    ),
    213                 ))),
     234                )
     235            ) ),
    214236            'response' => array(
    215237                'code' => 200,
     
    224246        $links = $this->get_html_elements_from_output( $output, 'a' );
    225247
    226         $this->assertThat( (string)$links[0], $this->equalTo( 'climate change' ) );
    227         $this->assertThat( (string)$links[1], $this->equalTo( 'energy' ) );
    228         $this->assertThat( (string)$links[2], $this->equalTo( 'IPPC' ) );
     248        $this->assertThat( (string) $links[0], $this->equalTo( 'climate change' ) );
     249        $this->assertThat( (string) $links[1], $this->equalTo( 'energy' ) );
     250        $this->assertThat( (string) $links[2], $this->equalTo( 'IPPC' ) );
    229251    }
    230252
     
    257279
    258280        $_CLIMATE_TAGGER_MOCK_RESPONSE = array(
    259             'body' => 'Unrecognized API key',
     281            'body'     => 'Unrecognized API key',
    260282            'response' => array(
    261283                'code' => 403,
     
    279301
    280302        $_CLIMATE_TAGGER_MOCK_RESPONSE = array(
    281             'body' => json_encode(
     303            'body'     => json_encode(
    282304                array(
    283305                    'concepts' => array(),
     
    303325    private function get_new_post() {
    304326        global $post;
    305         $post = new StdClass();
    306         $post->post_title = '';
     327        $post               = new StdClass();
     328        $post->post_title   = '';
    307329        $post->post_content = '';
    308330
Note: See TracChangeset for help on using the changeset viewer.