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)); }
/** * 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(''', '"', '"', '<', '>'), 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!"); }