Ejemplo n.º 1
0
 /**
  * Creates the individual test data post.
  *
  * Create individual posts for testing with. Gathers basic information such
  * as title, content, thumbnail, etc. and inserts them with the post. Also
  * adds metaboxes if applicable .
  *
  * @access private
  *
  * @see TestContent, wp_insert_post, add_post_meta, update_post_meta, $this->random_metabox_content
  *
  * @param string $cptslug a custom post type ID.
  * @param array $supports Features that the post type supports.
  * @param array $supports All CMB2 metaboxes attached to the post type.
  * @return void.
  */
 private function create_test_object($cptslug, $supports, $metaboxes)
 {
     // Get a random title
     $title = TestContent::title();
     // First, insert our post
     $post = array('post_name' => sanitize_title($title), 'post_status' => 'publish', 'post_type' => $cptslug, 'ping_status' => 'closed', 'comment_status' => 'closed');
     // Add title if supported
     if ($supports['title'] === true) {
         $post['post_title'] = $title;
     }
     // Add main content if supported
     if ($supports['editor'] === true) {
         $post['post_content'] = TestContent::paragraphs(rand(1, 5));
     }
     // Insert then post object
     $post_id = wp_insert_post($post);
     // Then, set a test content flag on the new post for later deletion
     add_post_meta($post_id, 'otm_test_content', '__test__', true);
     // Add thumbnail if supported
     if ($supports['thumbnail'] === true || in_array($cptslug, array('post', 'page'))) {
         update_post_meta($post_id, '_thumbnail_id', TestContent::image($post_id));
     }
     // Spin up metaboxes
     if (!empty($metaboxes)) {
         foreach ($metaboxes as $cmb) {
             $this->random_metabox_content($post_id, $cmb);
         }
     }
 }
 /**
  * Assigns the proper testing data to a custom metabox.
  *
  * Swaps through the possible types of CMB2 supported fields and
  * insert the appropriate data based on type & id.
  * Some types are not yet supported due to low frequency of use.
  *
  * @access private
  *
  * @see TestContent, add_post_meta
  *
  * @param int $post_id Single post ID.
  * @param array $cmb custom metabox array from CMB2.
  */
 private function random_metabox_content($post_id, $cmb)
 {
     $value = '';
     // First check that our post ID & cmb array aren't empty
     if (empty($cmb) || empty($post_id)) {
         return;
     }
     switch ($cmb['type']) {
         case 'text':
         case 'text_small':
         case 'text_medium':
             // If phone is in the id, fetch a phone #
             if (stripos($cmb['id'], 'phone')) {
                 $value = TestContent::phone();
                 // If email is in the id, fetch an email address
             } elseif (stripos($cmb['id'], 'email')) {
                 $value = TestContent::email();
                 // If time is in the id, fetch a time string
             } elseif (stripos($cmb['id'], 'time')) {
                 $value = TestContent::time();
                 // Otherwise, just a random text string
             } else {
                 $value = TestContent::title(rand(10, 50));
             }
             break;
         case 'text_url':
             $value = TestContent::link();
             break;
         case 'text_email':
             $value = TestContent::email();
             break;
             // case 'text_time': break;
         // case 'text_time': break;
         case 'select_timezone':
             $value = TestContent::timezone();
             break;
         case 'text_date':
             $value = TestContent::date('m/d/Y');
             break;
         case 'text_date_timestamp':
         case 'text_datetime_timestamp':
             $value = TestContent::date('U');
             break;
             // case 'text_datetime_timestamp_timezone': break;
         // case 'text_datetime_timestamp_timezone': break;
         case 'text_money':
             $value = rand(0, 100000);
             break;
         case 'test_colorpicker':
             $value = '#' . str_pad(dechex(mt_rand(0, 0xffffff)), 6, '0', STR_PAD_LEFT);
             break;
         case 'textarea':
         case 'textarea_small':
         case 'textarea_code':
             $value = TestContent::plain_text();
             break;
         case 'select':
         case 'radio_inline':
         case 'radio':
             // Grab a random item out of the array and return the key
             $new_val = array_slice($cmb['options'], rand(0, count($cmb['options'])), 1);
             $value = key($new_val);
             break;
             // case 'taxonomy_radio': break;
             // case 'taxonomy_select': break;
             // case 'taxonomy_multicheck': break;
         // case 'taxonomy_radio': break;
         // case 'taxonomy_select': break;
         // case 'taxonomy_multicheck': break;
         case 'checkbox':
             // 50/50 odds of being turned on
             if (rand(0, 1) == 1) {
                 $value = 'on';
             }
             break;
         case 'multicheck':
             $new_option = array();
             // Loop through each of our options
             foreach ($cmb['options'] as $key => $value) {
                 // 50/50 chance of being included
                 if (rand(0, 1)) {
                     $new_option[] = $key;
                 }
             }
             $value = $new_option;
             break;
         case 'wysiwyg':
             $value = TestContent::paragraphs();
             break;
         case 'file':
             $value = TestContent::image($post_id);
             break;
             // case 'file_list': break;
         // case 'file_list': break;
         case 'oembed':
             $value = TestContent::oembed();
             break;
     }
     // Value must exist to attempt to insert
     if (!empty($value) && !is_wp_error($value)) {
         // Files must be treated separately - they use the attachment ID
         // & url of media for separate cmb values
         if ($cmb['type'] != 'file') {
             add_post_meta($post_id, $cmb['id'], $value, true);
         } else {
             add_post_meta($post_id, $cmb['id'] . '_id', $value, true);
             add_post_meta($post_id, $cmb['id'], wp_get_attachment_url($value), true);
         }
         // If we're dealing with a WP Error object, just return the message for debugging
     } elseif (is_wp_error($value)) {
         return $value->get_error_message();
     }
 }