/** * Display the Geolocator. * * @since 0.1.0 * @param WP_Post $post The post object. */ public function cartopress_geocoder_content($post) { // Add an nonce field so we can check for it later. wp_nonce_field('cartopress_inner_custom_box', 'cartopress_inner_custom_box_nonce'); // Use get_post_meta to retrieve an existing value from the database. $donotsync_value = get_post_meta($post->ID, '_cp_post_donotsync', true); $geodata = get_post_meta($post->ID, '_cp_post_geo_data', true); //redefine $vars if cartodb values are true $cp_post = cartopress_sync::cartodb_select($post->ID); if ($cp_post[1] == true && $donotsync_value != 1) { $cp_values = $cp_post[0]->rows[0]; $cartodb_id = $cp_values->cartodb_id; $cp_geo_displayname = $cp_values->cp_geo_displayname; $cp_geo_lat = $cp_values->cp_geo_lat; $cp_geo_long = $cp_values->cp_geo_long; $cp_geo_streetnumber = $cp_values->cp_geo_streetnumber; $cp_geo_street = $cp_values->cp_geo_street; $cp_geo_postal = $cp_values->cp_geo_postal; $cp_geo_adminlevel4_vill_neigh = $cp_values->cp_geo_adminlevel4_vill_neigh; $cp_geo_adminlevel3_city = $cp_values->cp_geo_adminlevel3_city; $cp_geo_adminlevel2_county = $cp_values->cp_geo_adminlevel2_county; $cp_geo_adminlevel1_st_prov_region = $cp_values->cp_geo_adminlevel1_st_prov_region; $cp_geo_adminlevel0_country = $cp_values->cp_geo_adminlevel0_country; $cp_post_description = $cp_values->cp_post_description; } else { if ($geodata != null) { $cartodb_id = null; $cp_geo_displayname = $geodata['cp_geo_displayname']; $cp_geo_lat = $geodata['cp_geo_lat']; $cp_geo_long = $geodata['cp_geo_long']; $cp_geo_streetnumber = $geodata['cp_geo_streetnumber']; $cp_geo_street = $geodata['cp_geo_street']; $cp_geo_postal = $geodata['cp_geo_postal']; $cp_geo_adminlevel4_vill_neigh = $geodata['cp_geo_adminlevel4_vill_neigh']; $cp_geo_adminlevel3_city = $geodata['cp_geo_adminlevel3_city']; $cp_geo_adminlevel2_county = $geodata['cp_geo_adminlevel2_county']; $cp_geo_adminlevel1_st_prov_region = $geodata['cp_geo_adminlevel1_st_prov_region']; $cp_geo_adminlevel0_country = $geodata['cp_geo_adminlevel0_country']; $cp_post_description = get_post_meta($post->ID, '_cp_post_description', true); } else { //default values $cartodb_id = null; $cp_geo_displayname = ''; $cp_geo_lat = ''; $cp_geo_long = ''; $cp_geo_streetnumber = ''; $cp_geo_street = ''; $cp_geo_postal = ''; $cp_geo_adminlevel4_vill_neigh = ''; $cp_geo_adminlevel3_city = ''; $cp_geo_adminlevel2_county = ''; $cp_geo_adminlevel1_st_prov_region = ''; $cp_geo_adminlevel0_country = ''; $cp_post_description = ''; } } // Display the metabox include CARTOPRESS_ADMIN_DIR . 'cp-geocoderview.php'; }
/** * Handle the custom Bulk Action * * Uses switch/case to either delete from CartoDB, restore to CartoDB, or update postmeta with CartoDB data. Does not effect any WordPress core data. Based on the post http://wordpress.stackexchange.com/questions/29822/custom-bulk-action * * @since 0.1.0 */ function custom_bulk_action() { global $typenow; $pagenow; $post_type = $typenow; if ($post_type == 'post' || $post_type == 'page') { // get the action $wp_list_table = _get_list_table('WP_Posts_List_Table'); // depending on your resource type this could be WP_Users_List_Table, WP_Comments_List_Table, etc $action = $wp_list_table->current_action(); $allowed_actions = array("cartopress_delete", "cartopress_restore", "cartopress_update"); if (!in_array($action, $allowed_actions)) { return; } // security check check_admin_referer('bulk-posts'); // make sure ids are submitted. depending on the resource type, this may be 'media' or 'ids' if (isset($_REQUEST['post'])) { $post_ids = array_map('intval', $_REQUEST['post']); } if (empty($post_ids)) { return; } // this is based on wp-admin/edit.php $sendback = remove_query_arg(array('cartopress_deleted', 'cartopress_restored', 'cartopress_updated', 'untrashed', 'deleted', 'ids'), wp_get_referer()); if (!$sendback) { $sendback = admin_url("edit.php?post_type={$post_type}"); } $pagenum = $wp_list_table->get_pagenum(); $sendback = add_query_arg('paged', $pagenum, $sendback); switch ($action) { case 'cartopress_delete': $sql_distinct = 'SELECT DISTINCT cp_post_id FROM ' . CARTOPRESS_TABLE; $cartopress_ids = cartopress_sync::update_cartodb($sql_distinct, CARTOPRESS_APIKEY, CARTOPRESS_USERNAME, true); $cartopress_ids = $cartopress_ids->rows; $temp = array(); foreach ($cartopress_ids as $key => $value) { array_push($temp, $value->cp_post_id); } $deleted = 0; foreach ($post_ids as $post_id) { if (in_array($post_id, $temp)) { cartopress_sync::cartodb_delete($post_id); $deleted++; } else { return false; } } $sendback = add_query_arg(array('cartopress_deleted' => $deleted, 'ids' => join(',', $post_ids)), $sendback); break; case 'cartopress_restore': $restored = 0; foreach ($post_ids as $post_id) { cartopress_sync::cartodb_sync($post_id); $restored++; } $sendback = add_query_arg(array('cartopress_restored' => $restored, 'ids' => join(',', $post_ids)), $sendback); break; case 'cartopress_update': $updated = 0; foreach ($post_ids as $post_id) { cartopress_sync::cartopress_update_postmeta($post_id); $updated++; } $sendback = add_query_arg(array('cartopress_updated' => $updated, 'ids' => join(',', $post_ids)), $sendback); break; default: return; } $sendback = remove_query_arg(array('action', 'action2', 'tags_input', 'post_author', 'comment_status', 'ping_status', '_status', 'post', 'bulk_edit', 'post_view'), $sendback); wp_redirect($sendback); exit; } //end }
/** * Updates the geo field data stored in wp_postmeta with the values from CartoDB. * * AJAX handler to update the geodata saved in wp_postmeta with the corresponding CartoDB value. * * @since 0.1.0 */ public static function cartopress_update_postmeta($post_id) { $cp_post = cartopress_sync::cartodb_select($post_id); $donotsync_value = get_post_meta($post_id, '_cp_post_donotsync', true); if ($cp_post[1] == true && $donotsync_value != 1) { $cp_values = $cp_post[0]->rows[0]; $geodata = array('cp_geo_displayname' => $cp_values->cp_geo_displayname, 'cp_geo_lat' => $cp_values->cp_geo_lat, 'cp_geo_long' => $cp_values->cp_geo_long, 'cp_geo_streetnumber' => $cp_values->cp_geo_streetnumber, 'cp_geo_street' => $cp_values->cp_geo_street, 'cp_geo_postal' => $cp_values->cp_geo_postal, 'cp_geo_adminlevel4_vill_neigh' => $cp_values->cp_geo_adminlevel4_vill_neigh, 'cp_geo_adminlevel3_city' => $cp_values->cp_geo_adminlevel3_city, 'cp_geo_adminlevel2_county' => $cp_values->cp_geo_adminlevel2_county, 'cp_geo_adminlevel1_st_prov_region' => $cp_values->cp_geo_adminlevel1_st_prov_region, 'cp_geo_adminlevel0_country' => $cp_values->cp_geo_adminlevel0_country); update_post_meta($post_id, '_cp_post_geo_data', $geodata); } else { //default values $geodata = array('cp_geo_displayname' => '', 'cp_geo_lat' => '', 'cp_geo_long' => '', 'cp_geo_streetnumber' => '', 'cp_geo_street' => '', 'cp_geo_postal' => '', 'cp_geo_adminlevel4_vill_neigh' => '', 'cp_geo_adminlevel3_city' => '', 'cp_geo_adminlevel2_county' => '', 'cp_geo_adminlevel1_st_prov_region' => '', 'cp_geo_adminlevel0_country' => ''); } //end if }
/** * Delete function for attachment post type * * @since 0.1.0 * @param int $post_id The global post id. */ function cartopress_delete_attachment($post_id) { $post_type = get_post_type($post_id); if ('attachment' === $post_type) { cartopress_sync::cartodb_delete($post_id); } else { return; } }