Plugin Directory

Changeset 1554845


Ignore:
Timestamp:
12/14/2016 06:07:37 PM (9 years ago)
Author:
fifthestate
Message:

Refactored to reduce nesting and consolidate behavior

Location:
fifthestate/trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • fifthestate/trunk/fifthestate.php

    r1554764 r1554845  
    2727    include_once 'local-config.php';
    2828
    29 if (!defined('FifthEstate\SITE_URL'))
    30     define('FifthEstate\SITE_URL', 'https://fifthestate.com');
     29if (!defined(__NAMESPACE__ . '\\' . 'SITE_URL'))
     30    define(__NAMESPACE__ . '\\' . 'SITE_URL', 'https://fifthestate.com');
    3131
    32 if (!defined('FifthEstate\API_BASE_URL'))
    33     define('FifthEstate\API_BASE_URL', 'https://fifthestate.com/api');
     32if (!defined(__NAMESPACE__ . '\\' . 'API_BASE_URL'))
     33    define(__NAMESPACE__ . '\\' . 'API_BASE_URL', 'https://fifthestate.com/api');
    3434
    3535require_once 'post-handler.php';
    3636require_once 'settings.php';
    3737
    38 $options = array(
     38$default_app_state = array(
    3939    'logged_in' => false,
    4040    'token' => '',
    4141    'email' => '',
    4242    'category' => '');
    43 add_option( 'fifthestate', $options );
     43add_option( 'fifthestate', $default_app_state );
  • fifthestate/trunk/post-handler.php

    r1554071 r1554845  
    117117
    118118//a post is updated
    119 add_action( 'publish_to_publish', 'FifthEstate\post_updated_notification' );
     119add_action( 'publish_to_publish', __NAMESPACE__ . '\\' . 'post_updated_notification' );
    120120
    121121//a post is published
    122 add_action( 'new_to_publish', 'FifthEstate\post_published_notification' );
    123 add_action( 'trash_to_publish', 'FifthEstate\post_published_notification' );
    124 add_action( 'draft_to_publish', 'FifthEstate\post_published_notification' );
    125 add_action( 'pending_to_publish', 'FifthEstate\post_published_notification' );
    126 add_action( 'private_to_publish', 'FifthEstate\post_published_notification' );
    127 add_action( 'future_to_publish', 'FifthEstate\post_published_notification' );
     122add_action( 'new_to_publish', __NAMESPACE__ . '\\' . 'post_published_notification' );
     123add_action( 'trash_to_publish', __NAMESPACE__ . '\\' . 'post_published_notification' );
     124add_action( 'draft_to_publish', __NAMESPACE__ . '\\' . 'post_published_notification' );
     125add_action( 'pending_to_publish', __NAMESPACE__ . '\\' . 'post_published_notification' );
     126add_action( 'private_to_publish', __NAMESPACE__ . '\\' . 'post_published_notification' );
     127add_action( 'future_to_publish', __NAMESPACE__ . '\\' . 'post_published_notification' );
    128128
    129129//a post is 'deleted' (at least in so far as we are concerned)
    130 add_action( 'publish_to_trash', 'FifthEstate\post_deleted_notification' );
    131 add_action( 'publish_to_draft', 'FifthEstate\post_deleted_notification' );
    132 add_action( 'publish_to_pending', 'FifthEstate\post_deleted_notification' );
    133 add_action( 'publish_to_private', 'FifthEstate\post_deleted_notification' );
    134 add_action( 'publish_to_future', 'FifthEstate\post_deleted_notification' );
     130add_action( 'publish_to_trash', __NAMESPACE__ . '\\' . 'post_deleted_notification' );
     131add_action( 'publish_to_draft', __NAMESPACE__ . '\\' . 'post_deleted_notification' );
     132add_action( 'publish_to_pending', __NAMESPACE__ . '\\' . 'post_deleted_notification' );
     133add_action( 'publish_to_private', __NAMESPACE__ . '\\' . 'post_deleted_notification' );
     134add_action( 'publish_to_future', __NAMESPACE__ . '\\' . 'post_deleted_notification' );
  • fifthestate/trunk/settings.php

    r1554802 r1554845  
    1414require_once 'utilities.php';
    1515
     16const FORM_DESCRIPTORS = array(
     17    'log_in' => array(
     18        'nonce_field_action' => 'fifthestate-login',
     19        'nonce_field_name' => 'login_nonce',
     20        'processing_cb' => 'handle_login_form',
     21    ),
     22    'log_out' => array(
     23        'nonce_field_action' => 'fifthestate-logout-update',
     24        'nonce_field_name' => 'logout_update_nonce',
     25        'processing_cb' => 'handle_logout_form',
     26    ),
     27    'update_category' => array(
     28        'nonce_field_action' => 'fifthestate-logout-update',
     29        'nonce_field_name' => 'logout_update_nonce',
     30        'processing_cb' => 'handle_update_form',
     31    ),
     32);
     33
     34function handle_login_form() {
     35    $app_state = get_option('fifthestate');
     36    if (validate_login_form()) {
     37        $data = 'email=' . urlencode($_POST['email']) .
     38            '&password=' . urlencode($_POST['password']) .
     39            '&grant_type=password&scope=ingest';
     40        $response = json_decode($raw_response = curl_post(API_BASE_URL . '/tokens', $data, array('application/x-www-form-urlencoded')), true);
     41
     42        if ( isset( $response['access_token'] ) ) {
     43            if ($app_state['email'] === $_POST['email']) {
     44                $category = $app_state['category'];
     45            } else {
     46                $category = '';
     47            }
     48            $options = array(
     49                'logged_in' => true,
     50                'token' => $response['access_token'],
     51                'email' => $_POST['email'],
     52                'category' => $category);
     53            update_option( 'fifthestate', $options );
     54        } else {
     55            if (isset($response['error'])) {
     56                //server returns an error
     57                _e('<p>' . htmlspecialchars($response['error_description']) . '.</p>');
     58            } else {
     59                _e('<p>Server Error</p>');
     60                if (JSON_ERROR_SYNTAX === json_last_error()) {
     61                    _e('<p>' . htmlspecialchars($raw_response) . '</p>');
     62                }
     63            }
     64        }
     65    }
     66}
     67
     68function handle_logout_form() {
     69    $app_state = get_option('fifthestate');
     70    $authorization_header = 'Authorization: Bearer ' . $app_state['token'];
     71
     72    $response = json_decode($raw_response = curl_post(API_BASE_URL . '/logout', '',
     73        array($authorization_header)), true);
     74
     75    if (isset($response['success']) && $response['success']) {
     76        _e("<p>You've been logged out!</p>");
     77        $new_options = array(
     78            'logged_in' => false,
     79            'token' => '',
     80            'email' => $app_state['email'],
     81            'category' => $app_state['category']);
     82        update_option( 'fifthestate', $new_options );
     83    } else {
     84        if (isset($response['error'])) {
     85            //server returns an error
     86            _e('<p>' . htmlspecialchars($response['error_description']) . '.</p>');
     87        } else {
     88            _e('<p>Server Error</p>');
     89            if (JSON_ERROR_SYNTAX === json_last_error()) {
     90                _e('<p>' . htmlspecialchars($raw_response) . '</p>');
     91            }
     92        }
     93    }
     94}
     95
     96function handle_update_form() {
     97    $app_state = get_option('fifthestate');
     98    //do stuff to update category
     99    $category = $_POST['cat-root'];
     100    while ( isset( $_POST['cat-'.$category] ) ) {
     101        $category = $_POST['cat-'.$category];
     102    }
     103
     104    $new_options = array(
     105        'logged_in' => $app_state['logged_in'],
     106        'token' => $app_state['token'],
     107        'email' => $app_state['email'],
     108        'category' => $category);
     109    update_option( 'fifthestate', $new_options );
     110}
     111
    16112function settings_page() {
    17113    if (!current_user_can( 'manage_options' )) {
    18114        wp_die( __( 'Access not granted. Please log into WordPress again.'));
    19115    }
    20     $initial_options = get_option('fifthestate');
    21     $logged_in = $initial_options['logged_in'];
    22116
    23117    echo '<div class="wrap">';
     
    31125            if (check_admin_referer( 'fifthestate-login', 'login_nonce' )) {
    32126                //the login nonce is verified
    33 
    34                 if (validate_login_form()) {
    35                     $data = 'email=' . urlencode($_POST['email']) .
    36                         '&password=' . urlencode($_POST['password']) .
    37                         '&grant_type=password&scope=ingest';
    38                     $response = json_decode($raw_response = curl_post(API_BASE_URL . '/tokens', $data, array('application/x-www-form-urlencoded')), true);
    39 
    40                     if ( isset( $response['access_token'] ) ) {
    41                         if ($initial_options['email'] === $_POST['email']) {
    42                             $category = $initial_options['category'];
    43                         } else {
    44                             $category = '';
    45                         }
    46                         $options = array(
    47                             'logged_in' => true,
    48                             'token' => $response['access_token'],
    49                             'email' => $_POST['email'],
    50                             'category' => $category);
    51                         update_option( 'fifthestate', $options );
    52                     } else {
    53                         if (isset($response['error'])) {
    54                             //server returns an error
    55                             _e('<p>' . htmlspecialchars($response['error_description']) . '.</p>');
    56                         } else {
    57                             _e('<p>Server Error</p>');
    58                             if (JSON_ERROR_SYNTAX === json_last_error()) {
    59                                 _e('<p>' . htmlspecialchars($raw_response) . '</p>');
    60                             }
    61                         }
    62                     }
    63                 }
    64127            } else {
    65128                _e('<p>I suspect you are up to no good.</p>');
    66129            }
    67130
    68         } elseif (isset($_POST['log_out'])) {
    69             //case 1.2: the form you submitted was the log out form
    70 
    71             if (check_admin_referer('fifthestate-logout-update', 'logout_update_nonce')) {
    72                 //the logout nonce is verified
    73 
    74                 $authorization_header = 'Authorization: Bearer ' . $initial_options['token'];
    75 
    76                 $response = json_decode($raw_response = curl_post(API_BASE_URL . '/logout', '',
    77                     array($authorization_header)), true);
    78 
    79                 if (isset($response['success']) && $response['success']) {
    80                     _e("<p>You've been logged out!</p>");
    81                     $new_options = array(
    82                         'logged_in' => false,
    83                         'token' => '',
    84                         'email' => $initial_options['email'],
    85                         'category' => $initial_options['category']);
    86                     update_option( 'fifthestate', $new_options );
     131        }
     132        // process the submission
     133        foreach (FORM_DESCRIPTORS as $action => $descriptor) {
     134            if(isset($_POST[$action])) {
     135                if(check_admin_referer($descriptor['nonce_field_action'], $descriptor['nonce_field_name'])) {
     136                    call_user_func(__NAMESPACE__ . '\\' . $descriptor['processing_cb']);
    87137                } else {
    88                     if (isset($response['error'])) {
    89                         //server returns an error
    90                         _e('<p>' . htmlspecialchars($response['error_description']) . '.</p>');
    91                     } else {
    92                         _e('<p>Server Error</p>');
    93                         if (JSON_ERROR_SYNTAX === json_last_error()) {
    94                             _e('<p>' . htmlspecialchars($raw_response) . '</p>');
    95                         }
    96                     }
     138                    echo '<p>', _('An error has occurred'), '</p>';
    97139                }
    98             } else {
    99                 _e('<p>Server Error</p>');
    100             }
    101         } elseif (isset($_POST['update_category'])) {
    102             //case 1.3: you want to change your category. you remain logged in.
    103 
    104             if (check_admin_referer('fifthestate-logout-update', 'logout_update_nonce')) {
    105                 //do stuff to update category
    106                 $category = $_POST['cat-root'];
    107                 while ( isset( $_POST['cat-'.$category] ) ) {
    108                     $category = $_POST['cat-'.$category];
    109                 }
    110 
    111                 $new_options = array(
    112                     'logged_in' => $initial_options['logged_in'],
    113                     'token' => $initial_options['token'],
    114                     'email' => $initial_options['email'],
    115                     'category' => $category);
    116                 update_option( 'fifthestate', $new_options );
    117             } else {
    118                 _e('<p>Server Error</p>');
    119140            }
    120141        }
     
    122143
    123144    // create display depending on the current application state
    124     $initial_options = get_option('fifthestate');
    125     $logged_in = $initial_options['logged_in'];
     145    $app_state = get_option('fifthestate');
     146    $logged_in = $app_state['logged_in'];
    126147
    127148    if ($logged_in) {
    128         logged_in_view( $initial_options['email'], $initial_options['category'] );
     149        logged_in_view( $app_state['email'], $app_state['category'] );
    129150    } else {
    130151        logged_out_view();
     
    216237        'manage_options',
    217238        'fifthestate.php',
    218         'FifthEstate\settings_page'
     239        __NAMESPACE__ . '\\' . 'settings_page'
    219240    );
    220241} );
Note: See TracChangeset for help on using the changeset viewer.