Plugin Directory

Changeset 788210


Ignore:
Timestamp:
10/15/2013 03:15:51 PM (12 years ago)
Author:
Strunker
Message:

Allows multiple police offices to be selected

Location:
polizeipresse/trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • polizeipresse/trunk/Polizeipresse.php

    r514735 r788210  
    44 Plugin URI: http://wordpress.org/extend/plugins/polizeipresse/
    55 Description: The plugin loads police news from German police offices and shows them in your blog.
    6  Version: 0.2
     6 Version: 0.3
    77 Author: Karsten Strunk
    88 Author URI: http://www.strunk.eu/wordpress
     
    6464        polizeipresse_activate_cron();
    6565    }
     66
     67    //
     68    // Do database migration. Needed for upgrade 0.2 -> 0.3
     69    //
     70
     71    // Copy office_id to office_id0.
     72    $office_id_old = polizeipresse_get_option(POLIZEIPRESSE_OFFICE_ID);
     73    if ($office_id_old) {
     74        polizeipresse_update_option(POLIZEIPRESSE_OFFICE_ID0, $office_id_old);
     75        polizeipresse_delete_options(POLIZEIPRESSE_OFFICE_ID);
     76    }
     77
     78    // Copy last_story_id to last_story_id0
     79    $last_story_id_old = polizeipresse_get_option(POLIZEIPRESSE_CRON_LAST_STORY_ID);
     80    if ($last_story_id_old) {
     81        polizeipresse_update_option(POLIZEIPRESSE_CRON_LAST_STORY_ID0, $last_story_id_old);
     82        polizeipresse_delete_options(POLIZEIPRESSE_CRON_LAST_STORY_ID);
     83    }
     84
     85    //
     86    // End migration
     87    //
    6688}
    6789register_activation_hook(__FILE__, 'polizeipresse_activate');
     
    145167    polizeipresse_update_option(POLIZEIPRESSE_CRON_LAST_STATUS, null);
    146168
    147     // Get id of last retrieved story
    148     $last_story_id = polizeipresse_get_option(POLIZEIPRESSE_CRON_LAST_STORY_ID);
    149 
    150     // Load stories
    151     $stories = polizeipresse_load_stories();
    152 
    153     if ($stories != null) {
    154         // Add new posts for new stories
    155         $max_story_id = $last_story_id;
    156         foreach($stories AS $story) {
    157             $story_id = $story->id;
    158 
    159             // Do we need to create a new post for this story?
    160             if ($story_id > $last_story_id) {
    161                 polizeipresse_add_story($story);
    162                 $max_story_id = $story_id;
     169    // Load stories for each office
     170    $success = true;
     171    for($office = 0; $office < POLIZEIPRESSE_MAX_OFFICE_COUNT && $success = true; $office++) {
     172        // Get id of office to load stories for
     173        $office_id = polizeipresse_get_option(POLIZEIPRESSE_OFFICE_ID . $office);
     174
     175        // Do not execute empty configs
     176        if (empty($office_id)) {
     177            continue;
     178        }
     179
     180        // Get id of last retrieved story
     181        $last_story_id = polizeipresse_get_option(POLIZEIPRESSE_CRON_LAST_STORY_ID . $office);
     182
     183        // Do load stories for current office
     184        $stories = polizeipresse_load_stories($office_id);
     185
     186        // Check for valid response
     187        if (isSet($stories)) {
     188            $success = true;
     189
     190            // Add new posts for new stories
     191            $max_story_id = $last_story_id;
     192            foreach($stories AS $story) {
     193                $story_id = $story->id;
     194
     195                // Do we need to create a new post for this story?
     196                if ($story_id > $last_story_id) {
     197                    polizeipresse_add_story($story);
     198                    $max_story_id = $story_id;
     199                }
    163200            }
     201
     202            // Remember the highest story id so it don't get added again.
     203            polizeipresse_update_option(POLIZEIPRESSE_CRON_LAST_STORY_ID . $office, $max_story_id);
     204        } else {
     205            // No data received. Something has gone wrong.
     206            $success = false;
    164207        }
    165 
    166         // Remember the highest story id so it don't get added again.
    167         polizeipresse_update_option(POLIZEIPRESSE_CRON_LAST_STORY_ID, $max_story_id);
    168         polizeipresse_update_option(POLIZEIPRESSE_CRON_LAST_STATUS, true);
    169     }
    170     else {
    171         polizeipresse_update_option(POLIZEIPRESSE_CRON_LAST_STATUS, false);
    172     }
     208    }
     209
     210    polizeipresse_update_option(POLIZEIPRESSE_CRON_LAST_STATUS, $success);
    173211}
    174212do_action('polizeipresse_load_stories_and_create_posts');
     
    178216 * The stories are returned in chronological order.
    179217 */
    180 function polizeipresse_load_stories($load_teaser_only = false) {
     218function polizeipresse_load_stories($office_id, $load_teaser_only = false) {
    181219    require_once(dirname(__FILE__) . '/Presseportal.class.php');
    182220
    183221    // Get options
    184222    $api_key = polizeipresse_get_option(POLIZEIPRESSE_API_KEY);
    185     $office_id = polizeipresse_get_option(POLIZEIPRESSE_OFFICE_ID);
    186223    $filter_positive = polizeipresse_get_option(POLIZEIPRESSE_FILTER_POSITIVE);
    187224    $filter_negative = polizeipresse_get_option(POLIZEIPRESSE_FILTER_NEGATIVE);
     
    224261
    225262    $stories = array();
    226     if(!$response->error) {
     263    if((!$response->error) && ($response->stories)) {
    227264        foreach($response->stories AS $story) {
    228265            $title = $story->title;
  • polizeipresse/trunk/admin.php

    r511342 r788210  
    99    if ($pagenow == 'plugins.php') {
    1010        $api_key = polizeipresse_get_option(POLIZEIPRESSE_API_KEY);
    11         $office_id = polizeipresse_get_option(POLIZEIPRESSE_OFFICE_ID);
    12 
    13         if (empty($api_key) || empty($office_id)) {
     11
     12        $office_id_count = 0;
     13        for($office = 0; $office < POLIZEIPRESSE_MAX_OFFICE_COUNT; $office++) {
     14            $office_id = polizeipresse_get_option(POLIZEIPRESSE_OFFICE_ID . $office);
     15            if (!empty($office_id)) {
     16                $office_id_count++;;
     17            }
     18        }
     19
     20        if (empty($api_key) || ($office_id_count == 0)) {
    1421            print ('<div id="message" class="error">' . __('Polizeipresse plugin: Please enter API key select police office', 'Polizeipresse') . '</div>');
    1522        }
     
    3946        }
    4047
    41         if (isset ($_POST['office_id'])) {
    42             $options[POLIZEIPRESSE_OFFICE_ID] = strip_tags($_POST['office_id']);
    43         }
    44 
    45         if (isset ($_POST['office_name'])) {
    46             $options[POLIZEIPRESSE_OFFICE_NAME] = strip_tags($_POST['office_name']);
    47         }
    48 
     48        for($office = 0; $office < POLIZEIPRESSE_MAX_OFFICE_COUNT; $office++) {
     49            $office_id = strip_tags($_POST['office_id' . $office]);
     50            if (!empty($office_id)) {
     51                $options[POLIZEIPRESSE_OFFICE_ID . $office] = $office_id;
     52            } else {
     53                $options[POLIZEIPRESSE_OFFICE_ID . $office] = '';
     54                $options[POLIZEIPRESSE_OFFICE_NAME . $office] = '';
     55                $options[POLIZEIPRESSE_CRON_LAST_STORY_ID . $office] = '';
     56            }
     57
     58            $office_name = strip_tags($_POST['office_name' . $office]);
     59            if (!empty($office_name)) {
     60                $options[POLIZEIPRESSE_OFFICE_NAME . $office] = $office_name;
     61            } else {
     62                $options[POLIZEIPRESSE_OFFICE_ID . $office] = '';
     63                $options[POLIZEIPRESSE_OFFICE_NAME . $office] = '';
     64                $options[POLIZEIPRESSE_CRON_LAST_STORY_ID . $office] = '';
     65            }
     66        }
    4967
    5068        // Filter options
     
    96114
    97115        // Verify that api_key and office_id are set
     116        $office_id_count = 0;
     117        for($office = 0; $office < POLIZEIPRESSE_MAX_OFFICE_COUNT; $office++) {
     118            $office_id = polizeipresse_get_option(POLIZEIPRESSE_OFFICE_ID . $office);
     119            if (!empty($office_id)) {
     120                $office_id_count++;;
     121            }
     122        }
    98123        $api_key = polizeipresse_get_option(POLIZEIPRESSE_API_KEY);
    99         $office_id = polizeipresse_get_option(POLIZEIPRESSE_OFFICE_ID);
    100         if (empty ($api_key) || empty ($office_id)) {
     124        if (empty ($api_key) || ($office_id_count == 0)) {
    101125            polizeipresse_update_option(POLIZEIPRESSE_CRON_ENABLED, false);
    102126            polizeipresse_deactivate_cron();
     
    121145        polizeipresse_update_option(POLIZEIPRESSE_CRON_LAST_DATE, '');
    122146        polizeipresse_update_option(POLIZEIPRESSE_CRON_LAST_STATUS, '');
    123         polizeipresse_update_option(POLIZEIPRESSE_CRON_LAST_STORY_ID, '');
     147
     148        for($office = 0; $office < POLIZEIPRESSE_MAX_OFFICE_COUNT; $office++) {
     149            polizeipresse_update_option(POLIZEIPRESSE_CRON_LAST_STORY_ID . $office, '');
     150        }
    124151
    125152        $action_message = __('Last processed story id has been reset.', 'Polizeipresse');
     
    176203                </tr>
    177204
    178                 <tr valign="top">
    179                     <th scope="row">
    180                         <label for="office_name"><?php _e('Police office', 'Polizeipresse'); ?></lable>
    181                     </th>
    182                     <td>
    183                         <input name="office_id" id="office_id" size="50" value="<?php echo polizeipresse_get_option(POLIZEIPRESSE_OFFICE_ID); ?>"
    184                             type="hidden" class="regular-text" />
    185                         <input name="office_name" id="office_name" size="50" value="<?php echo polizeipresse_get_option(POLIZEIPRESSE_OFFICE_NAME); ?>"
    186                             type="text" class="regular-text" readonly="readonly" />
    187                         <button id="searchOfficeDialogButton" class="button-secondary" <?php echo (empty($api_key) ? ' disabled="disabled" ' : '') ?>>
    188                             <?php _e('Search for offices', 'Polizeipresse'); ?>
    189                         </button>
    190                     </td>
    191                 </tr>
     205<?php
     206                for($office = 0; $office < POLIZEIPRESSE_MAX_OFFICE_COUNT; $office++) {
     207?>
     208                    <tr valign="top">
     209                        <th scope="row">
     210                            <label for="office_name0"><?php _e('Police office', 'Polizeipresse'); ?></lable>
     211                        </th>
     212                        <td>
     213                            <input name="office_id<?php echo $office;?>"
     214                                id="office_id<?php echo $office;?>"
     215                                size="50"
     216                                value="<?php echo polizeipresse_get_option(POLIZEIPRESSE_OFFICE_ID . $office); ?>"
     217                                type="hidden" class="regular-text" />
     218                            <input name="office_name<?php echo $office;?>"
     219                                id="office_name<?php echo $office;?>"
     220                                class="office_name"
     221                                size="50"
     222                                value="<?php echo polizeipresse_get_option(POLIZEIPRESSE_OFFICE_NAME . $office); ?>"
     223                                type="text" class="regular-text" readonly="readonly" />
     224                            <button id="searchOfficeDialogButton<?php echo $office;?>"
     225                                class="searchOfficeDialogButton button-secondary" <?php echo (empty($api_key) ? ' disabled="disabled" ' : '') ?>>
     226                                <?php _e('Search for offices', 'Polizeipresse'); ?>
     227                            </button>
     228                            <button id="removeOfficeButton<?php echo $office;?>"
     229                                class="removeOfficeButton button-secondary">
     230                                <?php _e('Remove office', 'Polizeipresse'); ?>
     231                            </button>
     232                        </td>
     233                    </tr>
     234<?php
     235                }
     236?>
    192237
    193238            </table>
     
    290335                </tr>
    291336
    292                 <tr valign="top">
    293                     <th scope="row">
    294                         <label for="cron_last_story_id"><?php _e('Last processed story id', 'Polizeipresse'); ?>:</lable>
    295                     </th>
    296                     <td>
    297                         <input name="cron_last_story_id" size="10" value="<?php echo polizeipresse_get_option(POLIZEIPRESSE_CRON_LAST_STORY_ID); ?>" type="text" readonly="readonly" />
    298 <?php
    299                         $last_load_date = polizeipresse_get_option(POLIZEIPRESSE_CRON_LAST_DATE);
    300                         if (!empty ($last_load_date)) {
    301                             $last_load_date_time = date_i18n(get_option('date_format') . " " . get_option('time_format'), $last_load_date);
    302                         } else {
    303                             $last_load_date_time = __('unknown', 'Polizeipresse');
    304                         }
    305 
    306                         $last_load_status = polizeipresse_get_option(POLIZEIPRESSE_CRON_LAST_STATUS);
    307                         if (empty ($last_load_status)) {
    308                             $last_load_status = false;
    309                         }
    310 
    311                         echo '(' . __('Date/time', 'Polizeipresse') . ': ' . $last_load_date_time . ', ' . __('State', 'Polizeipresse') . ': ' . ($last_load_status == true ? __('successfull', 'Polizeipresse') : __('unknown', 'Polizeipresse')) . ")";
    312 ?>
    313                     </td>
    314                 </tr>
     337<?php
     338                for($office = 0; $office < POLIZEIPRESSE_MAX_OFFICE_COUNT; $office++) {
     339?>
     340                    <tr valign="top">
     341                        <th scope="row">
     342                            <label for="cron_last_story_id<?php echo $office;?>">
     343<?php
     344                                _e('Last processed story id', 'Polizeipresse');
     345                                $office_name = polizeipresse_get_option(POLIZEIPRESSE_OFFICE_NAME . $office);
     346                                if (empty ($office_name)) {
     347                                    $office_name = __('unknown', 'Polizeipresse');
     348                                }
     349                                echo "(" . $office_name . ")";
     350?>:</lable>
     351                        </th>
     352                        <td>
     353                            <input name="cron_last_story_id<?php echo $office;?>" size="10"
     354                                value="<?php echo polizeipresse_get_option(POLIZEIPRESSE_CRON_LAST_STORY_ID . $office); ?>"
     355                                type="text"
     356                                readonly="readonly" />
     357                    </tr>
     358<?php
     359                }
     360?>
     361                    <tr valign="top">
     362                        <th scope="row">
     363                            <label"><?php _e('Last cron execution time', 'Polizeipresse'); ?>:</lable>
     364                        </th>
     365                        <td>
     366<?php
     367                            $last_load_date = polizeipresse_get_option(POLIZEIPRESSE_CRON_LAST_DATE);
     368                            if (!empty ($last_load_date)) {
     369                                $last_load_date_time = date_i18n(get_option('date_format') . " " . get_option('time_format'), $last_load_date);
     370                            } else {
     371                                $last_load_date_time = __('unknown', 'Polizeipresse');
     372                            }
     373
     374                            $last_load_status = polizeipresse_get_option(POLIZEIPRESSE_CRON_LAST_STATUS);
     375                            if (empty ($last_load_status)) {
     376                                $last_load_status = false;
     377                            }
     378
     379                            echo '(' . __('Date/time', 'Polizeipresse') . ': ' . $last_load_date_time . ', ' . __('State', 'Polizeipresse') . ': ' . ($last_load_status == true ? __('successfull', 'Polizeipresse') : __('unknown', 'Polizeipresse')) . ")";
     380?>
     381                        </td>
    315382
    316383                <tr>
     
    378445
    379446}
     447
    380448
    381449/**
  • polizeipresse/trunk/options.php

    r509517 r788210  
    22
    33// Name of wordpress option
    4 define('POLIZEIPRESSE_OPTIONS_KEY', 'polizeipresse_options');
     4define('POLIZEIPRESSE_OPTIONS_KEY',     'polizeipresse_options');
     5define('POLIZEIPRESSE_MAX_OFFICE_COUNT', 10);
    56
    67// List of options for this plugin
    78define('POLIZEIPRESSE_API_KEY',               'api_key');
    8 define('POLIZEIPRESSE_OFFICE_ID',             'office_id');
    9 define('POLIZEIPRESSE_OFFICE_NAME',           'office_name');
     9define('POLIZEIPRESSE_OFFICE_ID',             'office_id');                // Old option. Needed for version 0.2 and below. Will be migrated.
     10define('POLIZEIPRESSE_OFFICE_NAME',           'office_name');              // Old option. Needed for version 0.2 and below. Will be migrated.
     11
     12for($office = 0; $office < POLIZEIPRESSE_MAX_OFFICE_COUNT; $office++) {
     13    define('POLIZEIPRESSE_OFFICE_ID' . $office,          'office_id' . $office);
     14    define('POLIZEIPRESSE_OFFICE_NAME' . $office,        'office_name' . $office);
     15    define('POLIZEIPRESSE_CRON_LAST_STORY_ID' . $office, 'cron_last_story_id' . $office);
     16}
     17
    1018define('POLIZEIPRESSE_FILTER_POSITIVE',       'filter_positive');
    1119define('POLIZEIPRESSE_FILTER_NEGATIVE',       'filter_negative');
     
    1321define('POLIZEIPRESSE_CRON_LAST_DATE',        'cron_last_date');
    1422define('POLIZEIPRESSE_CRON_LAST_STATUS',      'cron_last_status');
    15 define('POLIZEIPRESSE_CRON_LAST_STORY_ID',    'cron_last_story_id');
     23define('POLIZEIPRESSE_CRON_LAST_STORY_ID',    'cron_last_story_id');       // Old option. Needed for version 0.2 and below. Will be migrated.
    1624define('POLIZEIPRESSE_CRON_ADD_PUBLISH',      'cron_add_publish');
    1725define('POLIZEIPRESSE_CRON_NOTIFY',           'cron_add_notify');
     
    2533function polizeipresse_set_default_options() {
    2634    polizeipresse_update_option(POLIZEIPRESSE_API_KEY,               '');
    27     polizeipresse_update_option(POLIZEIPRESSE_OFFICE_ID,             '');
    28     polizeipresse_update_option(POLIZEIPRESSE_OFFICE_NAME,           '');
     35    polizeipresse_update_option(POLIZEIPRESSE_OFFICE_ID,             '');  // Old option. Needed for version 0.2 and below. Will be migrated.
     36    polizeipresse_update_option(POLIZEIPRESSE_OFFICE_NAME,           '');  // Old option. Needed for version 0.2 and below. Will be migrated.
    2937    polizeipresse_update_option(POLIZEIPRESSE_FILTER_POSITIVE,       '');
    3038    polizeipresse_update_option(POLIZEIPRESSE_FILTER_NEGATIVE,       '');
     
    3240    polizeipresse_update_option(POLIZEIPRESSE_CRON_LAST_DATE,        '');
    3341    polizeipresse_update_option(POLIZEIPRESSE_CRON_LAST_STATUS,      '');
    34     polizeipresse_update_option(POLIZEIPRESSE_CRON_LAST_STORY_ID,    '');
     42    polizeipresse_update_option(POLIZEIPRESSE_CRON_LAST_STORY_ID,    ''); // Old option. Needed for version 0.2 and below. Will be migrated.
    3543    polizeipresse_update_option(POLIZEIPRESSE_CRON_ADD_PUBLISH,      false);
    3644    polizeipresse_update_option(POLIZEIPRESSE_CRON_NOTIFY,           '');
    3745    polizeipresse_update_option(POLIZEIPRESSE_CRON_ADD_USER_ID,      '');
    3846    polizeipresse_update_option(POLIZEIPRESSE_CRON_ADD_CATEGORIE_ID, '');
     47
     48    for($office = 0; $office < POLIZEIPRESSE_MAX_OFFICE_COUNT; $office++) {
     49        polizeipresse_update_option(POLIZEIPRESSE_OFFICE_ID . $office,          '');
     50        polizeipresse_update_option(POLIZEIPRESSE_OFFICE_NAME . $office,        '');
     51        polizeipresse_update_option(POLIZEIPRESSE_CRON_LAST_STORY_ID . $office, '');
     52    }
    3953}
    4054
Note: See TracChangeset for help on using the changeset viewer.