Changeset 1554845
- Timestamp:
- 12/14/2016 06:07:37 PM (9 years ago)
- Location:
- fifthestate/trunk
- Files:
-
- 3 edited
-
fifthestate.php (modified) (1 diff)
-
post-handler.php (modified) (1 diff)
-
settings.php (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
-
fifthestate/trunk/fifthestate.php
r1554764 r1554845 27 27 include_once 'local-config.php'; 28 28 29 if (!defined( 'FifthEstate\SITE_URL'))30 define( 'FifthEstate\SITE_URL', 'https://fifthestate.com');29 if (!defined(__NAMESPACE__ . '\\' . 'SITE_URL')) 30 define(__NAMESPACE__ . '\\' . 'SITE_URL', 'https://fifthestate.com'); 31 31 32 if (!defined( 'FifthEstate\API_BASE_URL'))33 define( 'FifthEstate\API_BASE_URL', 'https://fifthestate.com/api');32 if (!defined(__NAMESPACE__ . '\\' . 'API_BASE_URL')) 33 define(__NAMESPACE__ . '\\' . 'API_BASE_URL', 'https://fifthestate.com/api'); 34 34 35 35 require_once 'post-handler.php'; 36 36 require_once 'settings.php'; 37 37 38 $ options= array(38 $default_app_state = array( 39 39 'logged_in' => false, 40 40 'token' => '', 41 41 'email' => '', 42 42 'category' => ''); 43 add_option( 'fifthestate', $ options);43 add_option( 'fifthestate', $default_app_state ); -
fifthestate/trunk/post-handler.php
r1554071 r1554845 117 117 118 118 //a post is updated 119 add_action( 'publish_to_publish', 'FifthEstate\post_updated_notification' );119 add_action( 'publish_to_publish', __NAMESPACE__ . '\\' . 'post_updated_notification' ); 120 120 121 121 //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' );122 add_action( 'new_to_publish', __NAMESPACE__ . '\\' . 'post_published_notification' ); 123 add_action( 'trash_to_publish', __NAMESPACE__ . '\\' . 'post_published_notification' ); 124 add_action( 'draft_to_publish', __NAMESPACE__ . '\\' . 'post_published_notification' ); 125 add_action( 'pending_to_publish', __NAMESPACE__ . '\\' . 'post_published_notification' ); 126 add_action( 'private_to_publish', __NAMESPACE__ . '\\' . 'post_published_notification' ); 127 add_action( 'future_to_publish', __NAMESPACE__ . '\\' . 'post_published_notification' ); 128 128 129 129 //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' );130 add_action( 'publish_to_trash', __NAMESPACE__ . '\\' . 'post_deleted_notification' ); 131 add_action( 'publish_to_draft', __NAMESPACE__ . '\\' . 'post_deleted_notification' ); 132 add_action( 'publish_to_pending', __NAMESPACE__ . '\\' . 'post_deleted_notification' ); 133 add_action( 'publish_to_private', __NAMESPACE__ . '\\' . 'post_deleted_notification' ); 134 add_action( 'publish_to_future', __NAMESPACE__ . '\\' . 'post_deleted_notification' ); -
fifthestate/trunk/settings.php
r1554802 r1554845 14 14 require_once 'utilities.php'; 15 15 16 const 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 34 function 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 68 function 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 96 function 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 16 112 function settings_page() { 17 113 if (!current_user_can( 'manage_options' )) { 18 114 wp_die( __( 'Access not granted. Please log into WordPress again.')); 19 115 } 20 $initial_options = get_option('fifthestate');21 $logged_in = $initial_options['logged_in'];22 116 23 117 echo '<div class="wrap">'; … … 31 125 if (check_admin_referer( 'fifthestate-login', 'login_nonce' )) { 32 126 //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 error55 _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 }64 127 } else { 65 128 _e('<p>I suspect you are up to no good.</p>'); 66 129 } 67 130 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']); 87 137 } 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>'; 97 139 } 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 category106 $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>');119 140 } 120 141 } … … 122 143 123 144 // 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']; 126 147 127 148 if ($logged_in) { 128 logged_in_view( $ initial_options['email'], $initial_options['category'] );149 logged_in_view( $app_state['email'], $app_state['category'] ); 129 150 } else { 130 151 logged_out_view(); … … 216 237 'manage_options', 217 238 'fifthestate.php', 218 'FifthEstate\settings_page'239 __NAMESPACE__ . '\\' . 'settings_page' 219 240 ); 220 241 } );
Note: See TracChangeset
for help on using the changeset viewer.