Changeset 1501047
- Timestamp:
- 09/23/2016 11:57:38 AM (9 years ago)
- Location:
- climate-tagger/trunk
- Files:
-
- 2 added
- 4 edited
-
LICENSE (added)
-
climate-tagger.php (modified) (14 diffs)
-
readme.txt (modified) (2 diffs)
-
tests/mock-option.php (modified) (1 diff)
-
tests/mock-remote-get.php (added)
-
tests/test-plugin.php (modified) (11 diffs)
Legend:
- Unmodified
- Added
- Removed
-
climate-tagger/trunk/climate-tagger.php
r1209707 r1501047 3 3 Plugin Name: Climate Tagger 4 4 Description: Recommends tags in a tag cloud based on Climate Tagger API. 5 Version: 1.0. 25 Version: 1.0.3 6 6 Author: Aptivate 7 7 */ … … 13 13 } 14 14 15 // Documentation: http://api.climatetagger.net/documentation/ 16 define( 'CLIMATE_TAGGER_API_URL', 'http://api.climatetagger.net' ); 17 15 18 class ClimateTagger { 16 19 17 function admin_menu() {20 static function admin_menu() { 18 21 add_options_page( 19 22 'Climate Tagger', … … 21 24 'manage_options', 22 25 '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() { 29 32 self::set_defaults(); 30 33 … … 33 36 'climate_tagger_general_settings' 34 37 ); 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() { 39 47 $options = get_option( 'climate_tagger_general_settings' ); 40 48 … … 42 50 $options, 43 51 array( 44 'token' => '',45 'limit' => '20',52 'token' => '', 53 'limit' => '20', 46 54 'post_types' => 'post', 47 ) ); 55 ) 56 ); 48 57 49 58 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() { 54 71 ?> 55 72 <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() { 126 157 $options = get_option( 'climate_tagger_general_settings' ); 127 158 … … 132 163 'boxid', 133 164 'Suggested Tags (Climate Tagger)', 134 array( ClimateTagger, 'box_routine' ),165 array( 'ClimateTagger', 'box_routine' ), 135 166 trim( $post_type ), 136 167 'side', 137 'low'); 138 } 139 } 140 141 function box_routine() { 168 'low' 169 ); 170 } 171 } 172 173 static function box_routine() { 142 174 $response = self::get_climate_tagger_response(); 143 175 144 176 if ( is_wp_error( $response ) ) { 145 177 echo $response->get_error_message(); 178 146 179 return; 147 180 } … … 149 182 if ( $response['response']['code'] != 200 ) { 150 183 echo $response['body']; 184 151 185 return; 152 186 } … … 155 189 if ( count( $tags_post ) == 0 ) { 156 190 echo "Click 'Save Draft' to refresh tag suggestions."; 191 157 192 return; 158 193 } … … 161 196 } 162 197 163 function print_tag_cloud( $tags_rec ) {198 static function print_tag_cloud( $tags_rec ) { 164 199 arsort( $tags_rec ); 165 200 … … 178 213 179 214 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 184 220 } 185 221 … … 187 223 } 188 224 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() { 190 262 global $post; 191 263 192 $content = $post->post_title . ' ' . $post->post_content;264 $content = $post->post_title . ' ' . $post->post_content; 193 265 194 266 $content = apply_filters( … … 201 273 } 202 274 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' ); 209 277 $language = apply_filters( 'climate-tagger-language', 'en', $post ); 210 278 211 279 $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'], 216 284 'countConcepts' => $options['limit'], 217 285 ); 218 286 287 if ( isset( $options['project'] ) ) { 288 $fields['tagger'] = $options['project']; 289 } 290 219 291 return wp_remote_post( $url, array( 'body' => $fields ) ); 220 292 } 221 293 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 ); 224 296 225 297 $concepts = $result['concepts']; … … 234 306 } 235 307 236 function admin_add_my_script() 237 { 308 static function admin_add_my_script() { 238 309 wp_enqueue_script( 239 310 'climate-tagger-add-tag', … … 247 318 add_action( 'admin_menu', array( 'ClimateTagger', 'admin_menu' ) ); 248 319 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' ) ); 254 322 } 255 323 ?> -
climate-tagger/trunk/readme.txt
r1209707 r1501047 3 3 Tags: tags, tag cloud, suggestion, tag suggestion, climate tagger, api 4 4 Requires at least: 3.7 5 Tested up to: 4. 2.26 Stable tag: 1.0. 25 Tested up to: 4.6.1 6 Stable tag: 1.0.3 7 7 License: GPLv2 or later 8 8 License URI: http://www.gnu.org/licenses/gpl-2.0.html … … 43 43 44 44 == Changelog == 45 46 = 1.0.3 = 47 Props [@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 45 50 46 51 = 1.0.2 = -
climate-tagger/trunk/tests/mock-option.php
r1209707 r1501047 10 10 'limit' => '', 11 11 'token' => '', 12 'project' => '', 12 13 ); 13 14 -
climate-tagger/trunk/tests/test-plugin.php
r1209707 r1501047 3 3 require_once 'climate-tagger.php'; 4 4 require_once 'mock-remote-post.php'; 5 require_once 'mock-remote-get.php'; 5 6 require_once 'mock-option.php'; 6 7 … … 36 37 $tagger = new ClimateTagger(); 37 38 38 $post = $this->get_new_post();39 $post = $this->get_new_post(); 39 40 $post->post_title = 'FEATURE: Three Steps to Decarbonising Development for a Zero-Carbon Future'; 40 41 … … 58 59 $post = $this->get_new_post(); 59 60 60 $content = <<<EOT61 $content = <<<EOT 61 62 A 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. 62 63 EOT; … … 71 72 $this->assertThat( 72 73 $text, 73 $this->stringContains( $content )74 $this->stringContains( $content ) 74 75 ); 75 76 } … … 174 175 } 175 176 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 176 197 public function test_api_url() { 177 198 $tagger = new ClimateTagger(); … … 185 206 $this->assertThat( 186 207 $_CLIMATE_TAGGER_MOCK_URL, 187 $this->equalTo( 'http://api. reegle.info/service/extract' )208 $this->equalTo( 'http://api.climatetagger.net/service/extract' ) 188 209 ); 189 210 } … … 197 218 198 219 $_CLIMATE_TAGGER_MOCK_RESPONSE = array( 199 'body' => json_encode(array(220 'body' => json_encode( array( 200 221 'concepts' => array( 201 222 array( 202 223 'prefLabel' => 'climate change', 203 'score' => 20,224 'score' => 20, 204 225 ), 205 226 array( 206 227 'prefLabel' => 'IPPC', 207 'score' => 1,228 'score' => 1, 208 229 ), 209 230 array( 210 231 'prefLabel' => 'energy', 211 'score' => 10,232 'score' => 10, 212 233 ), 213 ))), 234 ) 235 ) ), 214 236 'response' => array( 215 237 'code' => 200, … … 224 246 $links = $this->get_html_elements_from_output( $output, 'a' ); 225 247 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' ) ); 229 251 } 230 252 … … 257 279 258 280 $_CLIMATE_TAGGER_MOCK_RESPONSE = array( 259 'body' => 'Unrecognized API key',281 'body' => 'Unrecognized API key', 260 282 'response' => array( 261 283 'code' => 403, … … 279 301 280 302 $_CLIMATE_TAGGER_MOCK_RESPONSE = array( 281 'body' => json_encode(303 'body' => json_encode( 282 304 array( 283 305 'concepts' => array(), … … 303 325 private function get_new_post() { 304 326 global $post; 305 $post = new StdClass();306 $post->post_title = '';327 $post = new StdClass(); 328 $post->post_title = ''; 307 329 $post->post_content = ''; 308 330
Note: See TracChangeset
for help on using the changeset viewer.