Changeset 904962
- Timestamp:
- 04/29/2014 07:04:34 PM (12 years ago)
- File:
-
- 1 edited
-
polizeipresse/trunk/Polizeipresse.php (modified) (17 diffs)
Legend:
- Unmodified
- Added
- Removed
-
polizeipresse/trunk/Polizeipresse.php
r904959 r904962 40 40 */ 41 41 function polizeipresse_uninstall() { 42 polizeipresse_delete_options();42 polizeipresse_delete_options(); 43 43 } 44 44 register_uninstall_hook(__FILE__, 'polizeipresse_uninstall'); … … 53 53 */ 54 54 function polizeipresse_activate() { 55 if (polizeipresse_is_cron_enabled()) {56 polizeipresse_activate_cron();57 }58 59 //60 // Do database migration. Needed for upgrade 0.2 -> 0.361 //62 63 // Copy office_id to office_id0.64 $office_id_old = polizeipresse_get_option(POLIZEIPRESSE_OFFICE_ID);65 if ($office_id_old) {66 polizeipresse_update_option(POLIZEIPRESSE_OFFICE_ID0, $office_id_old);67 polizeipresse_delete_option(POLIZEIPRESSE_OFFICE_ID);68 }69 70 // Copy office_name to office_name0.71 $office_name_old = polizeipresse_get_option(POLIZEIPRESSE_OFFICE_NAME);72 if ($office_name_old) {73 polizeipresse_update_option(POLIZEIPRESSE_OFFICE_NAME0, $office_name_old);74 polizeipresse_delete_option(POLIZEIPRESSE_OFFICE_NAME);75 }76 77 // Copy last_story_id to last_story_id055 if (polizeipresse_is_cron_enabled()) { 56 polizeipresse_activate_cron(); 57 } 58 59 // 60 // Do database migration. Needed for upgrade 0.2 -> 0.3 61 // 62 63 // Copy office_id to office_id0. 64 $office_id_old = polizeipresse_get_option(POLIZEIPRESSE_OFFICE_ID); 65 if ($office_id_old) { 66 polizeipresse_update_option(POLIZEIPRESSE_OFFICE_ID0, $office_id_old); 67 polizeipresse_delete_option(POLIZEIPRESSE_OFFICE_ID); 68 } 69 70 // Copy office_name to office_name0. 71 $office_name_old = polizeipresse_get_option(POLIZEIPRESSE_OFFICE_NAME); 72 if ($office_name_old) { 73 polizeipresse_update_option(POLIZEIPRESSE_OFFICE_NAME0, $office_name_old); 74 polizeipresse_delete_option(POLIZEIPRESSE_OFFICE_NAME); 75 } 76 77 // Copy last_story_id to last_story_id0 78 78 $last_story_id_old = polizeipresse_get_option(POLIZEIPRESSE_CRON_LAST_STORY_ID); 79 if ($last_story_id_old) {80 polizeipresse_update_option(POLIZEIPRESSE_CRON_LAST_STORY_ID0, $last_story_id_old);81 polizeipresse_delete_option(POLIZEIPRESSE_CRON_LAST_STORY_ID);82 }83 84 // Copy cron_add_categorie_id to default_category_id.85 $category_id_old = polizeipresse_get_option(POLIZEIPRESSE_CRON_ADD_CATEGORIE_ID);86 if ($category_id_old) {87 polizeipresse_update_option(POLIZEIPRESSE_DEFAULT_CATEGORY_ID, $category_id_old);88 polizeipresse_delete_option(POLIZEIPRESSE_CRON_ADD_CATEGORIE_ID);89 }90 91 //92 // End migration93 //79 if ($last_story_id_old) { 80 polizeipresse_update_option(POLIZEIPRESSE_CRON_LAST_STORY_ID0, $last_story_id_old); 81 polizeipresse_delete_option(POLIZEIPRESSE_CRON_LAST_STORY_ID); 82 } 83 84 // Copy cron_add_categorie_id to default_category_id. 85 $category_id_old = polizeipresse_get_option(POLIZEIPRESSE_CRON_ADD_CATEGORIE_ID); 86 if ($category_id_old) { 87 polizeipresse_update_option(POLIZEIPRESSE_DEFAULT_CATEGORY_ID, $category_id_old); 88 polizeipresse_delete_option(POLIZEIPRESSE_CRON_ADD_CATEGORIE_ID); 89 } 90 91 // 92 // End migration 93 // 94 94 } 95 95 add_action('init', 'polizeipresse_activate'); … … 99 99 */ 100 100 function polizeipresse_deactivate() { 101 polizeipresse_deactivate_cron();101 polizeipresse_deactivate_cron(); 102 102 } 103 103 register_deactivation_hook(__FILE__, 'polizeipresse_deactivate'); … … 112 112 */ 113 113 function polizeipresse_activate_cron() { 114 if (!wp_next_scheduled('polizeipresse_load_stories_event')) {115 wp_schedule_event(time(), 'hourly', 'polizeipresse_load_stories_event');116 }114 if (!wp_next_scheduled('polizeipresse_load_stories_event')) { 115 wp_schedule_event(time(), 'hourly', 'polizeipresse_load_stories_event'); 116 } 117 117 } 118 118 … … 121 121 */ 122 122 function polizeipresse_load_stories_hourly() { 123 polizeipresse_load_stories_and_create_posts();123 polizeipresse_load_stories_and_create_posts(); 124 124 } 125 125 add_action('polizeipresse_load_stories_event', 'polizeipresse_load_stories_hourly'); … … 129 129 */ 130 130 function polizeipresse_deactivate_cron() { 131 wp_clear_scheduled_hook('polizeipresse_load_stories_event');131 wp_clear_scheduled_hook('polizeipresse_load_stories_event'); 132 132 } 133 133 … … 136 136 */ 137 137 function polizeipresse_is_cron_enabled() { 138 $cron_enabled = polizeipresse_get_option(POLIZEIPRESSE_CRON_ENABLED);139 if ($cron_enabled == true) {140 return true;141 }142 else {143 return false;144 }138 $cron_enabled = polizeipresse_get_option(POLIZEIPRESSE_CRON_ENABLED); 139 if ($cron_enabled == true) { 140 return true; 141 } 142 else { 143 return false; 144 } 145 145 } 146 146 … … 159 159 160 160 // Localize plugin 161 load_plugin_textdomain('Polizeipresse', false, dirname(plugin_basename(__FILE__)) . '/i18n/');161 load_plugin_textdomain('Polizeipresse', false, dirname(plugin_basename(__FILE__)) . '/i18n/'); 162 162 } 163 163 add_action('init', 'polizeipresse_init'); … … 170 170 function polizeipresse_load_stories_and_create_posts() { 171 171 172 polizeipresse_update_option(POLIZEIPRESSE_CRON_LAST_DATE, current_time('timestamp'));173 polizeipresse_update_option(POLIZEIPRESSE_CRON_LAST_STATUS, null);174 175 // Load stories for each office176 $success = true;177 for($office = 0; $office < POLIZEIPRESSE_MAX_OFFICE_COUNT && $success = true; $office++) {178 // Get id of office to load stories for179 $office_id = polizeipresse_get_option(POLIZEIPRESSE_OFFICE_ID . $office);180 181 // Do not execute empty configs182 if (empty($office_id)) {183 continue;184 }185 186 // Get id of last retrieved story187 $last_story_id = polizeipresse_get_option(POLIZEIPRESSE_CRON_LAST_STORY_ID . $office);188 189 // Do load stories for current office190 $stories = polizeipresse_load_stories($office_id);191 192 // Check for valid response193 if (isSet($stories)) {194 $success = true;195 196 // Add new posts for new stories197 $max_story_id = $last_story_id;198 foreach($stories AS $story) {199 $story_id = $story->id;200 201 // Do we need to create a new post for this story?202 if ($story_id > $last_story_id) {203 polizeipresse_add_story($story, $office);204 $max_story_id = $story_id;205 }206 }207 208 // Remember the highest story id so it don't get added again.209 polizeipresse_update_option(POLIZEIPRESSE_CRON_LAST_STORY_ID . $office, $max_story_id);210 } else {211 // No data received. Something has gone wrong.212 $success = false;213 }214 }215 216 polizeipresse_update_option(POLIZEIPRESSE_CRON_LAST_STATUS, $success);172 polizeipresse_update_option(POLIZEIPRESSE_CRON_LAST_DATE, current_time('timestamp')); 173 polizeipresse_update_option(POLIZEIPRESSE_CRON_LAST_STATUS, null); 174 175 // Load stories for each office 176 $success = true; 177 for($office = 0; $office < POLIZEIPRESSE_MAX_OFFICE_COUNT && $success = true; $office++) { 178 // Get id of office to load stories for 179 $office_id = polizeipresse_get_option(POLIZEIPRESSE_OFFICE_ID . $office); 180 181 // Do not execute empty configs 182 if (empty($office_id)) { 183 continue; 184 } 185 186 // Get id of last retrieved story 187 $last_story_id = polizeipresse_get_option(POLIZEIPRESSE_CRON_LAST_STORY_ID . $office); 188 189 // Do load stories for current office 190 $stories = polizeipresse_load_stories($office_id); 191 192 // Check for valid response 193 if (isSet($stories)) { 194 $success = true; 195 196 // Add new posts for new stories 197 $max_story_id = $last_story_id; 198 foreach($stories AS $story) { 199 $story_id = $story->id; 200 201 // Do we need to create a new post for this story? 202 if ($story_id > $last_story_id) { 203 polizeipresse_add_story($story, $office); 204 $max_story_id = $story_id; 205 } 206 } 207 208 // Remember the highest story id so it don't get added again. 209 polizeipresse_update_option(POLIZEIPRESSE_CRON_LAST_STORY_ID . $office, $max_story_id); 210 } else { 211 // No data received. Something has gone wrong. 212 $success = false; 213 } 214 } 215 216 polizeipresse_update_option(POLIZEIPRESSE_CRON_LAST_STATUS, $success); 217 217 } 218 218 do_action('polizeipresse_load_stories_and_create_posts'); … … 230 230 $filter_negative = polizeipresse_get_option(POLIZEIPRESSE_FILTER_NEGATIVE); 231 231 232 // Check for required options233 if (empty ($api_key) || empty ($office_id)) {234 return null;235 }232 // Check for required options 233 if (empty ($api_key) || empty ($office_id)) { 234 return null; 235 } 236 236 237 237 // Prepare positive filters … … 260 260 261 261 if ($load_teaser_only) { 262 $pp->teaser = true;262 $pp->teaser = true; 263 263 } 264 264 … … 266 266 $response = $pp->get_office_articles($office_id); 267 267 268 $stories = array();268 $stories = array(); 269 269 if((!$response->error) && ($response->stories)) { 270 270 foreach($response->stories AS $story) { … … 272 272 273 273 if ($load_teaser_only) { 274 $content = $story->teaser;274 $content = $story->teaser; 275 275 } 276 276 else { 277 $content = $story->body;277 $content = $story->body; 278 278 } 279 279 … … 293 293 294 294 if ($filter_success) { 295 $stories[] = $story;295 $stories[] = $story; 296 296 } 297 297 } … … 300 300 usort($stories, create_function('$value1, $value2', 'return ($value1 >= $value2) ? +1 : -1;')); 301 301 } else { 302 $stories = null;302 $stories = null; 303 303 } 304 304 … … 346 346 // Categorie. Use special category for office or default if none is defined. 347 347 $post_category = polizeipresse_get_option(POLIZEIPRESSE_OFFICE_CATEGORY_ID . $office); 348 if (empty($post_category) || $post_category == -1) {349 // Use default category350 $post_category = polizeipresse_get_option(POLIZEIPRESSE_DEFAULT_CATEGORY_ID);351 }348 if (empty($post_category) || $post_category == -1) { 349 // Use default category 350 $post_category = polizeipresse_get_option(POLIZEIPRESSE_DEFAULT_CATEGORY_ID); 351 } 352 352 353 353 // User 354 354 $post_user = polizeipresse_get_option(POLIZEIPRESSE_CRON_ADD_USER_ID); 355 355 356 // Status357 $post_status = (polizeipresse_get_option(POLIZEIPRESSE_CRON_ADD_PUBLISH) ? 'publish' : 'draft');356 // Status 357 $post_status = (polizeipresse_get_option(POLIZEIPRESSE_CRON_ADD_PUBLISH) ? 'publish' : 'draft'); 358 358 359 359 // Create new wordpress post … … 368 368 $post_id = wp_insert_post($new_post); 369 369 370 // Send notification email if configured371 $notify_email = polizeipresse_get_option(POLIZEIPRESSE_CRON_NOTIFY);372 if ($notify_email) {373 $user_data = get_userdata($post_user);374 375 $email_title = __('Polizeipresse: New post added', 'Polizeipresse') . ':' . $post_title;376 $email_message = $story->body . "\r\n\r\n" . __('URL to admin page', 'Polizeipresse') . ': ' . site_url('/wp-admin/edit.php');377 $email_sender = get_bloginfo('admin_email');378 379 $email_header = 'From: ' . $email_sender . "\r\n";380 $email_header .= "Content-type: text/plain; charset=UTF-8\r\n";381 382 mail($user_data->user_email, $email_title, $email_message, $email_header);383 }370 // Send notification email if configured 371 $notify_email = polizeipresse_get_option(POLIZEIPRESSE_CRON_NOTIFY); 372 if ($notify_email) { 373 $user_data = get_userdata($post_user); 374 375 $email_title = __('Polizeipresse: New post added', 'Polizeipresse') . ':' . $post_title; 376 $email_message = $story->body . "\r\n\r\n" . __('URL to admin page', 'Polizeipresse') . ': ' . site_url('/wp-admin/edit.php'); 377 $email_sender = get_bloginfo('admin_email'); 378 379 $email_header = 'From: ' . $email_sender . "\r\n"; 380 $email_header .= "Content-type: text/plain; charset=UTF-8\r\n"; 381 382 mail($user_data->user_email, $email_title, $email_message, $email_header); 383 } 384 384 } 385 385
Note: See TracChangeset
for help on using the changeset viewer.