SQL query for restoring the latest revision on all posts in wordpress - sql

My wordpress site recently got attacked and all the posts seem to have been updated with a blank version. So I want to restore the most latest revision for all posts. (NB: In wordpress the current published version is also stored in the database as a revision. So I think I would need to restore the 2nd revision (when arranged in the descending order))
What would be the SQL query for that?
Any help would be greatly appreciated!

So, all you need is in your database (luckily). The posts themselves can be retrieved like this
global $wpdb;
$query = "SELECT * FROM wp_posts WHERE post_type = 'revision'";
$query_results = $wpdb->get_results($query, ARRAY_A);
foreach ($query_results as $q_key => $q_value) {
$post = array(
'post_content' => $q_value['post_content']
'post_name' => $q_value['post_name']
'post_title' => $q_value['post_title']
'post_status' => $q_value['post_status']
'post_type' => 'post'
'post_author' => $q_value['post_author']
'ping_status' => $q_value['ping_status']
'post_parent' => $q_value['post_parent']
'menu_order' => $q_value['menu_order']
'to_ping' => $q_value['to_ping']
'pinged' => $q_value['pinged']
'post_password' => $q_value['post_password']
'guid' => $q_value['guid']
'post_content_filtered' => $q_value['post_content_filtered']
'post_excerpt' => $q_value['post_excerpt']
'post_date' => $q_value['post_date']
'post_date_gmt' => $q_value['post_date_gmt']
'comment_status' => $q_value['comment_status']
);
wp_insert_post( $post );
}
Read about wp_insert_post() here.
Now, depending on how much posts you have, this can either take a while, or it can break. If it breaks, then you'll need to wrap this in a function that you'll call with AJAX. Something like this should do the trick:
add_action( 'wp_ajax_insert_posts', 'database_ajax_insert_posts' );
add_action( 'wp_ajax_nopriv_insert_posts', 'database_ajax_insert_posts' );
function database_ajax_insert_posts(){
global $wpdb;
$offset = $_POST['offset'];
$query = "SELECT * FROM wp_posts WHERE post_type = 'revision' LIMIT $offset, 1";
$query_results = $wpdb->get_results($query, ARRAY_A);
foreach ($query_results as $q_key => $q_value) {
$post = array(
'post_content' => $q_value['post_content']
'post_name' => $q_value['post_name']
'post_title' => $q_value['post_title']
'post_status' => $q_value['post_status']
'post_type' => 'post'
'post_author' => $q_value['post_author']
'ping_status' => $q_value['ping_status']
'post_parent' => $q_value['post_parent']
'menu_order' => $q_value['menu_order']
'to_ping' => $q_value['to_ping']
'pinged' => $q_value['pinged']
'post_password' => $q_value['post_password']
'guid' => $q_value['guid']
'post_content_filtered' => $q_value['post_content_filtered']
'post_excerpt' => $q_value['post_excerpt']
'post_date' => $q_value['post_date']
'post_date_gmt' => $q_value['post_date_gmt']
'comment_status' => $q_value['comment_status']
);
wp_insert_post( $post );
}
}
And AJAX
jQuery(document).ready(function($) {
"use strict";
var offset = 1;
function ajax_call(){
$.ajax({
type: "POST",
url: ajaxurl,
data: {
'action': 'insert_posts',
'offset': offset,
},
success: function(response) {
if (offset < 100){ //Number of posts go here
offset += 1;
ajax_call();
} else{
$('#wpbody-content').append('<p>All done!</p>' );
}
},
error : function (jqXHR, textStatus, errorThrown) {
$('#wpbody-content').html(jqXHR + ' :: ' + textStatus + ' :: ' + errorThrown);
}
});
}
ajax_call();
});
Also be sure to localize your AJAX url with the handle of the script in which you'll put your AJAX call.
wp_localize_script('your_handle', 'db_from_WP', array(
'ajaxurl' => admin_url( 'admin-ajax.php' )
));
Taxonomy should be in the database, and you could get them in a similar fashion.
This is not foolproof, but should work. The thing that I can see that could be a bit problematic is getting the right taxonomy for the post, but that is doable, although with a bit fiddling around.
Hope this helps.

Related

Issue with post API curl library codeigniter 3

I use this library:
https://github.com/php-curl-class/php-curl-class
Base on documentation I write function for post order:
public function create_order()
{
$curl = new Curl();
$curl->setHeader('CustomerId', 'xxxxxxx');
$curl->setHeader('UserName', 'xxxxxxxxxx');
$curl->setHeader('ActionApiKey', 'xxxxxxxxxxxxxxx');
$order = array(
"createEmpty" => false,
"header" => array(
"comment" => "string",
"country" => "Polska",
"currency" => "PLN",
"isFileRequired" => true,
"actionCustomerId" => "81790",
"payer" => "EndCustomer",
"paymentType" => "CashOnDelivery",
"partnerOrderId" => "81790",
"deliveryAddresType" => "EndCustomer",
"cashOnDeliveryType" => "FullRate",
"cashOnDelivery" => 155,
"deliveryCompanyName" => "TEST API ORDER DO NOT SHIP",
"deliveryCity" => "PoznaƄ",
"deliveryPhone" => "xxxxxxx",
"deliveryStreet" => "xxxxxxxxxx",
"deliveryZipCode" => "xxxxxxx"
),
"items" => array(
array(
"actionProductId" => "MULLOGKAM0087",
"quantity" => 1,
"price" => 122,
"backOrderType" => "BackOrder"
)
)
);
$curl->setHeader('Content-Type', 'application/json');
$curl->post('xxxxxxxxxxxxxxxxxxxxx/v2/Order', json_encode($order));
if ($curl->error) {
echo 'Error: ' . $curl->error_code . ': ' . $curl->error_message;
} else {
echo 'Respone: ' . $curl->response;
}
}
After run controller, I get white page (I not get any error) but order not created. I test in Insomia API client and with this headers and data order created sucess.
Im not sure I correct post headers also with order data ?

Codeigniter post data from declared variable

function index_post() {
$sql = "SELECT id_so FROM so_detail ORDER BY id_so DESC LIMIT 1";
$last_id2 = $this->db->query($sql)->result();
foreach ($last_id2 as $row) {
$last_id = $row->id_so;
}
//echo $last_id;
$data = array(
'id_so' => $this->post($last_id),
'id_product' => $this->post('id_product'),
'harga' => $this->post('harga'),
'harga_dasar'=> $this->post('harga'),
'modal' => $this->post('modal'),
'pajak' => $this->post('pajak'),
'qty' => $this->post('qty'),
'keterangan' => $this->post('keterangan'),
'create_user'=> $this->post('create_user'),
'create_time'=> $this->post('create_time'),
'update_user'=> $this->post('create_user'),
'update_time'=> $this->post('create_time'));
$insert = $this->db->insert('so_detail', $data);
if ($insert) {
$this->response($data, 200);
} else {
$this->response(array('status' => 'fail', 502));
}
}
i got a problem that the posted id_so is "null". when i echo $last_id it show correct id ex: 120. but when i call it to $this->post($last_id), it just be null..
How to post id_so with a string that already declared before ($last_id) ??
there are many array in $last_id2 variable.data will insert multiple time based on your last_id2.try this:
foreach ($last_id2 as $row) {
$data = array(
'id_so' =>$row->id_so,
'id_product' => $this->post('id_product'),
'harga' => $this->post('harga'),
'harga_dasar'=> $this->post('harga'),
'modal' => $this->post('modal'),
'pajak' => $this->post('pajak'),
'qty' => $this->post('qty'),
'keterangan' => $this->post('keterangan'),
'create_user'=> $this->post('create_user'),
'create_time'=> $this->post('create_time'),
'update_user'=> $this->post('create_user'),
'update_time'=> $this->post('create_time'));
}
$this->db->insert_batch('so_detail',$data);

how to use "getresponsestatus" in google api calendar

I'm working in a project and I need to consult the attendees response. I want to create a script that every hours check if some attendee change your response status, but I can't find any documentation how this works, an example of my code when I insert the event:
if (isset($_SESSION['access_token'])) {
$client->setAccessToken($_SESSION['access_token']);
$service = new Google_Service_Calendar($client);
$event = new Google_Service_Calendar_Event(array(
'summary' => $summary,
'location' => $location,
'description' => $descripcion,
'colorId'=> "5",
'start' => array(
'dateTime' => $datetimeini,
'timeZone' => 'America/Caracas'
),
'end' => array(
'dateTime' => $datetimeini,
'timeZone' => 'America/Caracas'
),
'recurrence' => array(
'RRULE:FREQ=DAILY;COUNT=1'
),
'attendees' => array(
array('email' => $paciente,'responseStatus'=> 'needsAction'),
array('email' => $doctor)
),
'reminders' => array(
'useDefault' => FALSE,
'overrides' => array(
array('method' => 'email', 'minutes' => 12 * 60),
array('method' => 'popup', 'minutes' => 60)
),
),
));
$new_event = null;
$new_event_id = "";
$new_event = $service->events->insert($idcalendar, $event);
if($new_event!=null){
$new_event_id= $new_event->getid();
$event = $service->events->get($idcalendar,$new_event_id);
$response = new Google_Service_Calendar_EventAttendee($client);
if($event != null){
$resultado=array(
"respuesta"=>"exitoso",
"id evento"=>$event->getid(),
"sumario"=>$event->getSummary(),
"estado"=>$event->getstatus(),
"response"=>$event->getresponseStatus()
);
}else{
echo 'hay problemas.';
}
If I try this code on response I just get null. I want to get the real response.
You may use the Events: watch method which watches for changes to Events resources.
HTTP request
POST https://www.googleapis.com/calendar/v3/calendars/calendarId/events/watch
In the request body, supply data with the following structure:
{
"id": string,
"token": string,
"type": string,
"address": string,
"params": {
"ttl": string
}
}

Yii select2 - returned data cannot be selected

I have been looking into select2 and yii and have managed to load data via json request/response.
The issue I'm faced with is when I try to select an entry of the returned data, I can not.
Where am I going wrong ? The data returnd by the action is json formatted as CustomerCode and Name
Widget code in form
$this->widget('bootstrap.widgets.TbSelect2', array(
'asDropDownList' => false,
'name' => 'CustomerCode',
'options' => array(
'placeholder' => 'Type a Customer Code',
'minimumInputLength' => '2',
'width' => '40%',
'ajax' => array(
//'url'=> 'http://api.rottentomatoes.com/api/public/v1.0/movies.json',
'url'=> Yii::app()->getBaseUrl(true).'/customer/SearchCustomer',
'dataType' => 'jsonp',
'data' => 'js: function (term,page) {
return {
term: term, // Add all the query string elements here seperated by ,
page_limit: 10,
};
}',
'results' => 'js: function (data,page) {return {results: data};}',
),
'formatResult' => 'js:function(data){
var markup = data.CustomerCode + " - ";
markup += data.Name;
return markup;
}',
'formatSelection' => 'js: function(data) {
return data.CustomerCode;
}',
)));
code snipped from controller action SearchCustomer
Yii::app()->clientScript->scriptMap['jquery.js'] = false;
$this->renderJSON(Customer::model()->searchByCustomer($term));
renderJSON function from base controller class
protected function renderJSON($data)
{
header('Content-type: application/json');
echo $_GET['callback'] . "(";
echo CJSON::encode($data);
echo ")";
foreach (Yii::app()->log->routes as $route) {
if($route instanceof CWebLogRoute) {
$route->enabled = false; // disable any weblogroutes
}
}
Yii::app()->end();
}
Appreciate any help on this
i try.
change
'dataType' => 'jsonp' to 'dataType' => 'json'
and check json format
https://github.com/ivaynberg/select2/issues/920

dynamically adding text boxes in drupal form api

I want to have a add more button on clicking which i can add dynamically, textboxes in a drupal form api.. can someone help on this?
Thanks in advance
Take a look at Adding dynamic form elements using AHAH. It is a good guide to learn AHAH with Drupal's form API.
EDIT: For an example, install the Examples for Developers module, it has an AHAH example you can use to help you learn.
Here is an example how to solve this with Ajax in Drupal 7(If anyone want I can convert it also to Drupal 6 using AHAH(name before it became Ajax)).
<?php
function text_boxes_form($form, &$form_state)
{
$number = 0;
$addTextbox = false;
$form['text_lists'] = array
(
'#tree' => true,
'#theme' => 'my_list_theme',
'#prefix' => '<div id="wrapper">',
'#suffix' => '</div>',
);
if (array_key_exists('triggering_element', $form_state) &&
array_key_exists('#name', $form_state['triggering_element']) &&
$form_state['triggering_element']['#name'] == 'Add'
) {
$addTextbox = true;
}
if (array_key_exists('values', $form_state) && array_key_exists('text_lists', $form_state['values']))
{
foreach ($form_state['values']['text_lists'] as $element) {
$form['text_lists'][$number]['text'] = array(
'#type' => 'textfield',
);
$number++;
}
}
if ($addTextbox) {
$form['text_lists'][$number]['text'] = array(
'#type' => 'textfield',
);
}
$form['add_button'] = array(
'#type' => 'button',
'#name' => 'Add',
'#ajax' => array(
'callback' => 'ajax_add_textbox_callback',
'wrapper' => 'wrapper',
'method' => 'replace',
'effect' => 'fade',
),
'#value' => t('Add'),
);
return $form;
}
function ajax_add_textbox_callback($form, $form_state)
{
return $form['text_lists'];
}
function text_boxes_menu()
{
$items = array();
$items['text_boxes'] = array(
'title' => 'Text Boxes',
'description' => 'Text Boxes',
'page callback' => 'drupal_get_form',
'page arguments' => array('text_boxes_form'),
'access callback' => array(TRUE),
'type' => MENU_CALLBACK,
);
return $items;
}