Example #1
0
function cp_default_ad()
{
    global $wpdb;
    $posts = get_posts(array('posts_per_page' => 1, 'post_type' => APP_POST_TYPE, 'no_found_rows' => true));
    if (!empty($posts)) {
        return;
    }
    $cat = appthemes_maybe_insert_term('Misc', APP_TAX_CAT);
    $description = '<p>This is your first ClassiPress ad listing. It is a placeholder ad just so you can see how it works. Delete this before launching your new classified ads site.</p>Duis arcu turpis, varius nec sagittis id, ultricies ac arcu. Etiam sagittis rutrum nunc nec viverra. Etiam egestas congue mi vel sollicitudin.</p><p>Vivamus ac libero massa. Cras pellentesque volutpat dictum. Ut blandit dapibus augue, lobortis cursus mi blandit sed. Fusce vulputate hendrerit sapien id aliquet.</p>';
    $default_ad = array('post_title' => 'My First Classified Ad', 'post_name' => 'my-first-classified-ad', 'post_content' => $description, 'post_status' => 'publish', 'post_type' => APP_POST_TYPE, 'post_author' => 1);
    // insert the default ad
    $post_id = wp_insert_post($default_ad);
    //set the custom post type categories
    wp_set_post_terms($post_id, $cat['term_id'], APP_TAX_CAT, false);
    //set the custom post type tags
    $new_tags = array('ad tag1', 'ad tag2', 'ad tag3');
    wp_set_post_terms($post_id, $new_tags, APP_TAX_TAG, false);
    // set some default meta values
    $ad_expire_date = appthemes_mysql_date(current_time('mysql'), 30);
    $advals['cp_sys_expire_date'] = $ad_expire_date;
    $advals['cp_sys_ad_duration'] = '30';
    $advals['cp_sys_ad_conf_id'] = '3624e0d2963459d2';
    $advals['cp_sys_userIP'] = '153.247.194.375';
    $advals['cp_daily_count'] = '0';
    $advals['cp_total_count'] = '0';
    $advals['cp_price'] = '250';
    $advals['cp_street'] = '153 Townsend St';
    $advals['cp_city'] = 'San Francisco';
    $advals['cp_state'] = 'California';
    $advals['cp_country'] = 'United States';
    $advals['cp_zipcode'] = '94107';
    $advals['cp_sys_total_ad_cost'] = '5.00';
    // now add the custom fields into WP post meta fields
    foreach ($advals as $meta_key => $meta_value) {
        add_post_meta($post_id, $meta_key, $meta_value, true);
    }
    // set coordinates of new ad
    cp_update_geocode($post_id, '', '37.779633', '-122.391762');
}
Example #2
0
 private function import_row($row)
 {
     $post = array('post_type' => $this->post_type, 'post_status' => 'publish');
     $post_meta = array();
     $tax_input = array();
     $tax_meta = array();
     foreach ($this->fields as $col => $field) {
         if (isset($row[$col])) {
             $post[$field] = $row[$col];
         }
     }
     foreach ($this->custom_fields as $col => $data) {
         if (isset($row[$col])) {
             $val = $row[$col];
         } elseif ('' !== $data['default']) {
             $val = $data['default'];
         } else {
             continue;
         }
         $post_meta[$data['internal_key']] = $val;
     }
     foreach ($this->taxonomies as $col) {
         if (isset($row[$col])) {
             $tax_input[$col] = array_filter(array_map('trim', explode(',', $row[$col])));
         }
     }
     foreach ($this->tax_meta as $tax => $fields) {
         foreach ($fields as $col => $key) {
             if (isset($row[$col])) {
                 $term = $tax_input[$tax][0];
                 $tax_meta[$tax][$term][$key] = $row[$col];
             }
         }
     }
     foreach ($tax_meta as $tax => $terms) {
         foreach ($terms as $term => $meta_data) {
             if (empty($term)) {
                 continue;
             }
             $t = appthemes_maybe_insert_term($term, $tax);
             if (is_wp_error($t)) {
                 continue;
             }
             foreach ($meta_data as $meta_key => $meta_value) {
                 if ('desc' == substr($meta_key, -4)) {
                     wp_update_term($t['term_id'], $tax, array('description' => sanitize_text_field($meta_value)));
                 } else {
                     if (function_exists('update_metadata')) {
                         update_metadata($tax, $t['term_id'], $meta_key, $meta_value);
                     }
                 }
             }
         }
     }
     foreach ($tax_input as $tax => $terms) {
         $_terms = array();
         foreach ($terms as $term) {
             if (empty($term)) {
                 continue;
             }
             $t = appthemes_maybe_insert_term($term, $tax);
             if (!is_wp_error($t)) {
                 $_terms[] = (int) $t['term_id'];
             }
         }
         $post['tax_input'][$tax] = $_terms;
     }
     if (!empty($post['post_author'])) {
         $user = get_user_by('login', $post['post_author']);
         if ($user) {
             $post['post_author'] = $user->ID;
         }
     }
     if (!empty($post['post_date'])) {
         $post['post_date'] = date('Y-m-d H:i:s', strtotime($post['post_date']));
     }
     $post = apply_filters('app_importer_import_row_post', $post);
     $post_id = wp_insert_post($post, true);
     if (is_wp_error($post_id)) {
         return false;
     }
     $post_meta = apply_filters('app_importer_import_row_post_meta', $post_meta);
     foreach ($post_meta as $meta_key => $meta_value) {
         add_post_meta($post_id, $meta_key, $meta_value, true);
     }
     if ($this->geodata) {
         appthemes_set_coordinates($post_id, $row['lat'], $row['lng']);
     }
     if ($this->attachments) {
         if (isset($row['attachments'])) {
             $attachments = explode(',', $row['attachments']);
             if (empty($attachments)) {
                 continue;
             }
             $imported_attachments = array();
             foreach ($attachments as $attachment_url) {
                 $attachment_url = trim($attachment_url);
                 $attachment_id = $this->process_attachment($attachment_url, $post_id);
                 $imported_attachments[$attachment_url] = $attachment_id;
             }
             if (isset($row['featured_attachment'])) {
                 if (!empty($imported_attachments[$row['featured_attachment']])) {
                     update_post_meta($post_id, '_thumbnail_id', $imported_attachments[$row['featured_attachment']]);
                 }
             }
         }
     }
     do_action('app_importer_import_row_after', $post_id, $row);
     return true;
 }