Changeset 2117295
- Timestamp:
- 07/04/2019 03:15:59 AM (7 years ago)
- Location:
- makemydonation-imo/trunk
- Files:
-
- 1 added
- 4 edited
-
api.php (modified) (4 diffs)
-
js/mmdimo.update-check.js (added)
-
metabox.php (modified) (1 diff)
-
mmdimo.php (modified) (7 diffs)
-
readme.txt (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
makemydonation-imo/trunk/api.php
r2052715 r2117295 49 49 $args = array_merge( $default_args, $args ); 50 50 51 return wp_remote_request( $url . '/' . $path, $args ); 51 $raw_response = wp_remote_request( $url . '/' . $path, $args ); 52 53 if ( !is_wp_error( $raw_response ) ) { 54 return $raw_response; 55 } 52 56 } 53 57 … … 128 132 'internal_id' => '', 129 133 'family_emails' => array(), 130 'family_notify' => '0', 131 'charity' => '', 134 'charities' => array(), 132 135 ); 133 136 $case = array_merge( $default_values, $case ); … … 160 163 'internal_id' => '', 161 164 'family_emails' => array(), 162 'family_notify' => '0', 163 'charity' => '', 165 'charities' => array(), 164 166 ); 165 167 $case = array_merge( $default_values, $case ); … … 177 179 } 178 180 } 181 182 /** 183 * Create or update a case using the API. 184 * 185 * @param $case 186 * Array with the case data as in the API description. 187 * @return 188 * Array with the case data as in the API description. 189 */ 190 function mmdimo_api_case_create_or_update( $case ) { 191 if ( isset( $case['internal_id'] ) && $case['internal_id'] && isset( $case['name'] ) && $case['name'] ) { 192 $default_values = array( 193 'action' => 'create_or_update', 194 'funeral_home' => get_option('mmdimo_fhid'), 195 'family_emails' => array(), 196 'charities' => array(), 197 ); 198 $case = array_merge( $default_values, $case ); 199 200 $response = mmdimo_api_request( 'post', 'case', $case ); 201 202 if ( isset( $response['response'] ) ) { 203 switch ($response['response']['code']) { 204 case 200: 205 return (array) json_decode( $response['body'] ); 206 } 207 } 208 209 return FALSE; 210 } 211 } -
makemydonation-imo/trunk/metabox.php
r2087914 r2117295 1 1 <?php defined( 'ABSPATH' ) or die( 'No direct access allowed.' ); ?> 2 2 3 <?php if ( !$mmdimo_case || ! $mmdimo_case['id']): ?>3 <?php if ( !$mmdimo_case || !isset($mmdimo_case['id']) ): ?> 4 4 <?php wp_nonce_field( 'mmdimo_post_case_create', 'mmdimo_case_nonce' ); ?> 5 5 <?php else: ?> -
makemydonation-imo/trunk/mmdimo.php
r2087916 r2117295 7 7 Plugin URI: https://wordpress.org/plugins/makemydonation-imo 8 8 Description: Integrate your funeral home site with our Make My Donation - In Memory Of Platform and allow donations to over 1.5 million eligible US charities. 9 Version: 1.1 29 Version: 1.13 10 10 Author: Make My Donation 11 11 Author URI: http://makemydonation.org … … 50 50 add_action( 'wp_ajax_mmdimo_load_funeral_homes', 'mmdimo_load_funeral_homes' ); 51 51 add_action( 'wp_ajax_mmdimo_orghunter_csc_ajax', 'mmdimo_orghunter_csc_ajax' ); 52 add_action( 'wp_ajax_mmdimo_check_update', 'mmdimo_check_update_ajax' ); 52 53 } 53 54 … … 73 74 register_setting( 'mmdimo', 'mmdimo_default_state' ); 74 75 register_setting( 'mmdimo', 'mmdimo_case_check_default' ); 76 register_setting( 'mmdimo', 'mmdimo_update' ); 77 register_setting( 'mmdimo', 'mmdimo_install_uuid' ); 75 78 } 76 79 … … 119 122 case 'settings_page_mmdimo': 120 123 wp_enqueue_script( 'mmdimo-options-form', plugin_dir_url( __FILE__ ) . 'js/mmdimo.options-form.js', array(), $data['Version'] ); 124 wp_enqueue_script( 'mmdimo-update-check', plugin_dir_url( __FILE__ ) . 'js/mmdimo.update-check.js', array(), $data['Version'] ); 121 125 wp_enqueue_style( 'mmdimo-options-form', plugin_dir_url( __FILE__ ) . 'css/mmdimo.options-form.css', array(), $data['Version'] ); 122 126 break; … … 179 183 180 184 function mmdimo_meta_box_save( $post_id, $post, $update ) { 181 if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return; 182 183 if( !current_user_can( 'edit_post', $post_id ) ) return; 184 185 if( ( !isset( $_POST['mmdimo_case_create'] ) || !$_POST['mmdimo_case_create'] ) && ( !isset( $_POST['mmdimo_case_update'] ) || !$_POST['mmdimo_case_update'] ) ) return; 186 187 if ( $_POST['mmdimo_case_create'] && ( !isset( $_POST['mmdimo_case_update'] ) || !$_POST['mmdimo_case_update'] ) && ( !isset( $_POST['mmdimo_case_nonce'] ) || !wp_verify_nonce( $_POST['mmdimo_case_nonce'], 'mmdimo_post_case_create' ) ) ) return; 188 189 if ( $_POST['mmdimo_case_update'] && ( !isset( $_POST['mmdimo_case_nonce'] ) || !wp_verify_nonce( $_POST['mmdimo_case_nonce'], 'mmdimo_post_case_update_' . $_POST['mmdimo_case_update'] ) ) ) return; 190 191 require_once( MMDIMO_PLUGIN_DIR . '/api.php' ); 192 193 $current_case = array(); 194 if ( $postmeta_case = get_post_meta( $post_id, 'mmdimo_case', TRUE )) { 195 $current_case = $postmeta_case; 196 } 197 if ( isset( $_POST['mmdimo_case_update'] ) && $_POST['mmdimo_case_update'] ) { 198 $remote_case = mmdimo_api_case_load( $_POST['mmdimo_case_update'] ); 199 $current_case = array_merge($current_case, $remote_case); 200 } 201 $new_case = array( 202 'name' => $post->post_title, 203 'family_email' => '', 204 'family_emails' => explode(',', $_POST['mmdimo_family_emails']), 205 'charity' => '', 206 'charities' => $_POST['mmdimo_charity_select'] == 'select' ? explode(',', $_POST['mmdimo_charities']) : array(), 207 'status' => 1, 208 ); 209 $case = array_merge($current_case, $new_case); 210 211 update_post_meta( $post_id, 'mmdimo_case', $case ); 212 213 if ( !isset( $case['id'] ) || !$case['id'] ) { 214 $saved_case = mmdimo_api_case_create( $case ); 215 } 216 else { 217 $saved_case = mmdimo_api_case_update( $case ); 218 } 219 220 if ( isset( $saved_case['id'] ) ) { 221 update_post_meta( $post_id, 'mmdimo_case', $saved_case ); 222 223 if ( $_POST['mmdimo_case_update'] && ( !isset( $_POST['mmdimo_case_create'] ) || !$_POST['mmdimo_case_create'] )) { 224 $saved_case['status'] = 0; 225 226 update_post_meta( $post_id, 'mmdimo_case', $saved_case ); 227 } 228 185 $request_data = $_POST; 186 $validate = mmdimo_meta_box_save_validate($post_id, $post, $update, $request_data); 187 188 if (!is_wp_error($validate)) { 189 require_once( MMDIMO_PLUGIN_DIR . '/api.php' ); 190 191 $current_case = array(); 192 if ( $postmeta_case = get_post_meta( $post_id, 'mmdimo_case', TRUE )) { 193 $current_case = $postmeta_case; 194 } 195 if ( isset( $_POST['mmdimo_case_update'] ) && $_POST['mmdimo_case_update'] ) { 196 $remote_case = mmdimo_api_case_load( $_POST['mmdimo_case_update'] ); 197 $current_case = array_merge($current_case, $remote_case); 198 } 199 $new_case = array( 200 'internal_id' => mmdimo_case_generate_internal_id($post_id), 201 'name' => $post->post_title, 202 'family_emails' => explode(',', $_POST['mmdimo_family_emails']), 203 'charities' => $_POST['mmdimo_charity_select'] == 'select' ? explode(',', $_POST['mmdimo_charities']) : array(), 204 'status' => 1, 205 'return_url' => get_permalink($post_id) 206 ); 207 $case = array_merge($current_case, $new_case); 208 209 update_post_meta( $post_id, 'mmdimo_case', $case ); 229 210 if ( isset( $_POST['mmdimo_charity_metadata'] ) ) { 230 211 update_post_meta( $post_id, 'mmdimo_charity_metadata', preg_replace( '/charity-ein-/', '', $_POST['mmdimo_charity_metadata'] ) ); 231 212 } 232 } 233 else { 234 mmdimo_error_message(__( 'Error while connecting to the Make My Donation API server. Edit and save the post to try again.', 'mmdimo' )); 235 } 213 214 $saved_case = mmdimo_api_case_create_or_update( $case ); 215 216 if ( isset( $saved_case['id'] ) ) { 217 update_post_meta( $post_id, 'mmdimo_case', $saved_case ); 218 219 if ( ( isset( $_POST['mmdimo_case_update'] ) && $_POST['mmdimo_case_update'] ) && ( !isset( $_POST['mmdimo_case_create'] ) || !$_POST['mmdimo_case_create'] )) { 220 $saved_case['status'] = 0; 221 222 update_post_meta( $post_id, 'mmdimo_case', $saved_case ); 223 } 224 225 if ( isset( $_POST['mmdimo_charity_metadata'] ) ) { 226 update_post_meta( $post_id, 'mmdimo_charity_metadata', preg_replace( '/charity-ein-/', '', $_POST['mmdimo_charity_metadata'] ) ); 227 } 228 } 229 else { 230 mmdimo_error_message(__( 'Error while connecting to the Make My Donation API server. Edit and save the post to try again.', 'mmdimo' )); 231 } 232 } 233 } 234 235 /** 236 * Validate the case data sent from the metabox in a save_post action. 237 * 238 * See save_post action reference for $post_id, $post and $update parameters. 239 * 240 * @param $post_id 241 * @param $post 242 * @param $update 243 * @param $request 244 * The PHP formatted array with the request data. Usually the global $_POST variable. 245 * 246 * @return 247 * TRUE if valid or WP_Error object if invalid. 248 */ 249 function mmdimo_meta_box_save_validate($post_id, $post, $update, $request) { 250 $error = new WP_Error(); 251 252 if (empty($request)) { 253 $error->add('mmdimo_meta_box_save_empty_request', __('Empty request data.', 'mmdimo')); 254 } 255 256 $doing_autosave = defined('DOING_AUTOSAVE') && DOING_AUTOSAVE; 257 if ($doing_autosave) { 258 $error->add('mmdimo_meta_box_save_autosave', __('Not creating a case while doing autosave.', 'mmdimo')); 259 } 260 261 $can_edit = current_user_can('edit_post', $post_id); 262 if (!$can_edit) { 263 $error->add('mmdimo_meta_box_save_not_authorized', __('User cannot edit this post.', 'mmdimo')); 264 } 265 266 $post_type = get_post_type($post_id); 267 $default_invalid_types = array('revision'); 268 $mmdimo_post_type = get_option('mmdimo_post_type'); 269 $default_valid_type = !$mmdimo_post_type && !in_array($post_type, $default_invalid_types); 270 $custom_valid_type = $mmdimo_post_type && $mmdimo_post_type == $post_type; 271 $post_type = $default_valid_type || $custom_valid_type; 272 if (!$post_type) { 273 $error->add('mmdimo_meta_box_save_invalid_post_type', __('Invalid post type.', 'mmdimo')); 274 } 275 276 $action_create = isset($request['mmdimo_case_create']) && $request['mmdimo_case_create']; 277 $action_update = isset($request['mmdimo_case_update']) && $request['mmdimo_case_update']; 278 $nonce_create = isset($request['mmdimo_case_nonce']) && wp_verify_nonce($request['mmdimo_case_nonce'], 'mmdimo_post_case_create'); 279 $nonce_update = isset($request['mmdimo_case_nonce']) && wp_verify_nonce($request['mmdimo_case_nonce'], 'mmdimo_post_case_update'); 280 $doing_create = $action_create && !$action_update && $nonce_create; 281 $doing_update = $action_update && !$action_create && $nonce_update; 282 $create_or_update = $doing_create || $doing_update; 283 if (!$create_or_update) { 284 $error->add('mmdimo_meta_box_save_invalid_create_or_update', __('Invalid create or update nonce.', 'mmdimo')); 285 } 286 287 if (!empty($error->get_error_codes())) { 288 return $error; 289 } 290 291 return TRUE; 236 292 } 237 293 … … 320 376 321 377 return $donations; 378 } 379 380 function mmdimo_check_update_ajax() { 381 $check_update = mmdimo_check_update(); 382 if ($check_update) { 383 die(json_encode($check_update)); 384 } 385 } 386 387 function mmdimo_check_update() { 388 $plugin_info = mmdimo_get_plugin_info(); 389 $current_plugin = current( $plugin_info ); 390 $updated_plugin = get_option( 'mmdimo_update' ); 391 392 if ($updated_plugin && version_compare( $updated_plugin['new_version'], $current_plugin['Version'] ) && $updated_plugin['check_time'] + (60 * 60 * 24) < time()) { 393 return $updated_plugin; 394 } 395 elseif ($updated_plugin && version_compare( $updated_plugin['new_version'], $current_plugin['Version'] ) <= 0) { 396 delete_option( 'mmdimo_update' ); 397 } 398 399 $remote_plugin = mmdimo_check_update_remote( $plugin_info ); 400 401 if ( $remote_plugin ) { 402 $remote_plugin['check_time'] = time(); 403 update_option( 'mmdimo_update', $remote_plugin ); 404 405 $updated_plugin = $remote_plugin; 406 } 407 408 return $updated_plugin; 409 } 410 411 function mmdimo_get_plugin_info() { 412 $plugins = get_plugins(); 413 414 foreach ($plugins as $plugin_path => $plugin) { 415 if (strpos($plugin_path, 'mmdimo.php') !== FALSE) { 416 return array($plugin_path => $plugin); 417 } 418 } 419 420 return NULL; 421 } 422 423 function mmdimo_check_update_remote( $plugin ) { 424 $to_send = array( 425 'plugins' => $plugin, 426 'active' => array_keys($plugin) 427 ); 428 429 // include an unmodified $wp_version 430 include( ABSPATH . WPINC . '/version.php' ); 431 432 $options = array( 433 'timeout' => 5, 434 'body' => array( 435 'plugins' => wp_json_encode( $to_send ), 436 'translations' => wp_json_encode( array() ), 437 'locale' => wp_json_encode( array() ), 438 'all' => wp_json_encode( false ), 439 ), 440 'user-agent' => 'WordPress/' . $wp_version . '; ' . home_url( '/' ), 441 ); 442 443 $url = 'http://api.wordpress.org/plugins/update-check/1.1/'; 444 if ( $ssl = wp_http_supports( array( 'ssl' ) ) ) { 445 $url = set_url_scheme( $url, 'https' ); 446 } 447 448 $raw_response = wp_remote_post( $url, $options ); 449 450 if ( !is_wp_error( $raw_response ) ) { 451 $response = json_decode( wp_remote_retrieve_body( $raw_response ), true ); 452 if ( isset( $response['plugins'] ) ) { 453 return current( $response['plugins'] ); 454 } 455 } 456 457 return FALSE; 322 458 } 323 459 … … 360 496 $response = mmdimo_api_cities_list( $state ); 361 497 } 498 499 /** 500 * Get an unique id for this plugin installation. 501 * 502 * @return 503 * String with the UUID value. 504 */ 505 function mmdimo_get_install_uuid() { 506 $uuid = get_option('mmdimo_install_uuid'); 507 508 if (!$uuid) { 509 $uuid = sprintf( 510 '%04x%04x-%04x-%04x-%04x-%04x%04x%04x', 511 mt_rand( 0, 0xffff ), 512 mt_rand( 0, 0xffff ), 513 mt_rand( 0, 0xffff ), 514 mt_rand( 0, 0x0fff ) | 0x4000, 515 mt_rand( 0, 0x3fff ) | 0x8000, 516 mt_rand( 0, 0xffff ), 517 mt_rand( 0, 0xffff ), 518 mt_rand( 0, 0xffff ) 519 ); 520 update_option('mmdimo_install_uuid', $uuid); 521 } 522 523 return $uuid; 524 } 525 526 /** 527 * Generate an unique internal id for a given post id. 528 * 529 * @return 530 * String with generated internal id. 531 */ 532 function mmdimo_case_generate_internal_id($post_id) { 533 return implode(':', array( 534 'wp', 535 'mmdimo', 536 mmdimo_get_install_uuid(), 537 parse_url(get_option('siteurl'), PHP_URL_HOST), 538 $post_id 539 )); 540 } -
makemydonation-imo/trunk/readme.txt
r2087916 r2117295 3 3 Tags: obituary, funeral home, charity, search, third-party, donation 4 4 Requires at least: 3.7 5 Tested up to: 5.2 6 Stable tag: 1.1 25 Tested up to: 5.2.2 6 Stable tag: 1.13 7 7 License: GPLv2 or later 8 8 License URI: http://www.gnu.org/licenses/gpl-2.0.html
Note: See TracChangeset
for help on using the changeset viewer.