/**
  * Sync a single post (on creation or update)
  *
  * @todo if post should not be added, it's deleted (to account for unpublishing, etc). Make that more elegant.
  *
  * @param int $post_id
  * @return void
  */
 public function sync_post($post_id)
 {
     $post = new SP_Post(get_post($post_id));
     if ($post->should_be_indexed()) {
         $response = SP_API()->index_post($post);
         if (!$this->parse_error($response, array(200, 201))) {
             do_action('sp_debug', '[SP_Sync_Manager] Indexed Post', $response);
         } else {
             do_action('sp_debug', '[SP_Sync_Manager] Error Indexing Post', $response);
         }
     } else {
         # This is excessive, figure out a better way around it
         $this->delete_post($post_id);
     }
 }
 function test_sppost1()
 {
     $SP = new SP_Post();
     $post = $SP->get(21);
     $this->assertTrue($post['post_title'] == 'Page C-1');
     $post['post_title'] = 'Page C-1-test';
     $SP->update($post, 21);
     $post = $SP->get(21);
     $this->assertTrue($post['post_title'] == 'Page C-1-test');
     $post['post_title'] = 'Page C-1';
     $SP->update($post, 21);
 }
 function test_insert3()
 {
     global $wpdb;
     $P = new SP_Post();
     $post = array();
     $post['post_title'] = 'Test Meta';
     $post['a'] = 'Apple';
     $post['b'] = 'Banana';
     $post['c'] = 'Carrot';
     $this->post_id = $P->insert($post);
     $this->assertTrue($this->post_id);
     // Make sure each custom field corresponds to one row in wp_postmeta
     $sql = $wpdb->prepare("SELECT meta_key, count(*) as cnt FROM {$wpdb->postmeta} WHERE post_id=%s GROUP BY meta_key", $this->post_id);
     $results = $wpdb->get_results($sql, ARRAY_A);
     $this->assertTrue($results);
     foreach ($results as $r) {
         $this->assertTrue($r['cnt'] == 1);
     }
     // Nothing changed: make sure additional rows weren't created.
     $result = $P->update($post, $this->post_id);
     $this->assertTrue($result == $this->post_id);
     // Make sure each custom field corresponds to one row in wp_postmeta
     $sql = $wpdb->prepare("SELECT meta_key, count(*) as cnt FROM {$wpdb->postmeta} WHERE post_id=%s GROUP BY meta_key", $this->post_id);
     $results = $wpdb->get_results($sql, ARRAY_A);
     $this->assertTrue($results);
     foreach ($results as $r) {
         $this->assertTrue($r['cnt'] == 1);
     }
     // Try nuking a custom field
     unset($post['c']);
     // Nothing changed: make sure additional rows weren't created.
     $result = $P->update($post, $this->post_id, true);
     $this->assertTrue($result == $this->post_id);
     $post = $P->get($this->post_id);
     $this->assertTrue(!isset($post['c']));
     // Make sure each custom field corresponds to one row in wp_postmeta
     $sql = $wpdb->prepare("SELECT meta_key, count(*) as cnt FROM {$wpdb->postmeta} WHERE post_id=%s GROUP BY meta_key", $this->post_id);
     $results = $wpdb->get_results($sql, ARRAY_A);
     $this->assertTrue($results);
     foreach ($results as $r) {
         $this->assertTrue($r['cnt'] == 1);
     }
     // Cleanup: Make sure it's gone
     $P->delete($this->post_id);
     $post = $P->get($this->post_id);
     $this->assertTrue(empty($post));
 }
Example #4
0
 /**
  * Callback function used by the cctm_post_form() function.  This is what gets 
  * called 
  *
  * @param	array $args: parameters from the shortcode and posted data
  * @return string (printed)	 
  */
 public static function post_form_handler($args)
 {
     //return print_r($args,true);
     // Strip out the control stuff (keys begin with underscore)
     $vals = array();
     foreach ($args as $k => $v) {
         if ($k[0] == '_') {
             continue;
         }
         $vals[$k] = $v;
     }
     // Insert into Database
     $email_only = CCTM::get_value($args, '_email_only');
     if (!$email_only) {
         require_once CCTM_PATH . '/includes/SP_Post.php';
         $P = new SP_Post();
         CCTM::$post_id = $P->insert($vals);
     }
     // Email stuff
     if (isset($args['_email_to']) && !empty($args['_email_to']) && isset($args['_email_tpl']) && !empty($args['_email_tpl'])) {
         $Q = new GetPostsQuery();
         $P = $Q->get_post($args['_email_tpl']);
         //return print_r($P, true);
         $subject = $P['post_title'];
         $message_tpl = wpautop($P['post_content']);
         // If the 'My User' <*****@*****.**> format is used, we have to manipulate the string
         // to keep WP from tripping over itself
         $from = CCTM::get_value($args, '_email_from', get_bloginfo('admin_email'));
         $from = str_replace(array('&#039', '&#034;', '&quot;', '&lt;', '&gt;'), array("'", '"', '"', '<', '>'), $from);
         // die(print_r($args,true));
         $subject = CCTM::get_value($args, '_email_subject', $subject);
         $headers = 'From: ' . $from . "\r\n";
         $headers .= 'content-type: text/html' . "\r\n";
         $message = CCTM::parse($message_tpl, $vals);
         if (!wp_mail($args['_email_to'], $subject, $message, $headers)) {
             return "There was a problem sending the email.";
         }
     }
     // Redirect or show a simple message.
     $redirect = CCTM::get_value($args, '_redirect');
     if ($redirect) {
         $url = get_permalink($redirect);
         CCTM::redirect($url, true);
     }
     // Else, return message:
     return CCTM::get_value($args, '_msg', "Thanks for submitting the form!");
 }