Plugin Directory

Changeset 904962


Ignore:
Timestamp:
04/29/2014 07:04:34 PM (12 years ago)
Author:
Strunker
Message:

tabs to spaces

File:
1 edited

Legend:

Unmodified
Added
Removed
  • polizeipresse/trunk/Polizeipresse.php

    r904959 r904962  
    4040 */
    4141function polizeipresse_uninstall() {
    42     polizeipresse_delete_options();
     42    polizeipresse_delete_options();
    4343}
    4444register_uninstall_hook(__FILE__, 'polizeipresse_uninstall');
     
    5353 */
    5454function 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.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
     55    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
    7878    $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 migration
    93     //
     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    //
    9494}
    9595add_action('init', 'polizeipresse_activate');
     
    9999 */
    100100function polizeipresse_deactivate() {
    101     polizeipresse_deactivate_cron();
     101    polizeipresse_deactivate_cron();
    102102}
    103103register_deactivation_hook(__FILE__, 'polizeipresse_deactivate');
     
    112112 */
    113113function 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    }
    117117}
    118118
     
    121121 */
    122122function polizeipresse_load_stories_hourly() {
    123     polizeipresse_load_stories_and_create_posts();
     123    polizeipresse_load_stories_and_create_posts();
    124124}
    125125add_action('polizeipresse_load_stories_event', 'polizeipresse_load_stories_hourly');
     
    129129 */
    130130function polizeipresse_deactivate_cron() {
    131     wp_clear_scheduled_hook('polizeipresse_load_stories_event');
     131    wp_clear_scheduled_hook('polizeipresse_load_stories_event');
    132132}
    133133
     
    136136 */
    137137function 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    }
    145145}
    146146
     
    159159
    160160    // Localize plugin
    161     load_plugin_textdomain('Polizeipresse', false, dirname(plugin_basename(__FILE__)) . '/i18n/');
     161    load_plugin_textdomain('Polizeipresse', false, dirname(plugin_basename(__FILE__)) . '/i18n/');
    162162}
    163163add_action('init', 'polizeipresse_init');
     
    170170 function polizeipresse_load_stories_and_create_posts() {
    171171
    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);
     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);
    217217}
    218218do_action('polizeipresse_load_stories_and_create_posts');
     
    230230    $filter_negative = polizeipresse_get_option(POLIZEIPRESSE_FILTER_NEGATIVE);
    231231
    232     // Check for required options
    233     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    }
    236236
    237237    // Prepare positive filters
     
    260260
    261261    if ($load_teaser_only) {
    262         $pp->teaser = true;
     262        $pp->teaser = true;
    263263    }
    264264
     
    266266    $response = $pp->get_office_articles($office_id);
    267267
    268     $stories = array();
     268    $stories = array();
    269269    if((!$response->error) && ($response->stories)) {
    270270        foreach($response->stories AS $story) {
     
    272272
    273273            if ($load_teaser_only) {
    274                 $content = $story->teaser;
     274                $content = $story->teaser;
    275275            }
    276276            else {
    277                 $content = $story->body;
     277                $content = $story->body;
    278278            }
    279279
     
    293293
    294294            if ($filter_success) {
    295                 $stories[] = $story;
     295                $stories[] = $story;
    296296            }
    297297        }
     
    300300        usort($stories, create_function('$value1, $value2', 'return ($value1 >= $value2) ? +1 : -1;'));
    301301    } else {
    302         $stories = null;
     302        $stories = null;
    303303    }
    304304
     
    346346    // Categorie. Use special category for office or default if none is defined.
    347347    $post_category = polizeipresse_get_option(POLIZEIPRESSE_OFFICE_CATEGORY_ID . $office);
    348     if (empty($post_category) || $post_category == -1) {
    349         // Use default category
    350         $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    }
    352352
    353353    // User
    354354    $post_user = polizeipresse_get_option(POLIZEIPRESSE_CRON_ADD_USER_ID);
    355355
    356     // Status
    357     $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');
    358358
    359359    // Create new wordpress post
     
    368368    $post_id = wp_insert_post($new_post);
    369369
    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     }
     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    }
    384384}
    385385
Note: See TracChangeset for help on using the changeset viewer.