Example #1
0
/**
 * JobRoller Relist Job Process
 * Processes a job edit/relist.
 *
 *
 * @version 1.3
 * @author AppThemes
 * @package JobRoller
 * @copyright 2010 all rights reserved
 *
 */
function jr_process_relist_job_form()
{
    global $user_ID, $post, $posted, $job_details, $wpdb, $jr_log;
    $errors = new WP_Error();
    if (isset($_POST['job_submit']) && $_POST['job_submit']) {
        include_once ABSPATH . 'wp-admin/includes/file.php';
        include_once ABSPATH . 'wp-admin/includes/image.php';
        include_once ABSPATH . 'wp-admin/includes/media.php';
        // Get (and clean) data
        $fields = array('your_name', 'website', 'job_title', 'job_term_type', 'job_term_cat', 'job_pack', 'jr_geo_latitude', 'jr_geo_longitude', 'jr_address', 'details', 'apply', 'tags');
        foreach ($fields as $field) {
            $posted[$field] = stripslashes(trim($_POST[$field]));
        }
        if (isset($_POST['featureit']) && $_POST['featureit']) {
            $posted['featureit'] = 'yes';
        } else {
            $posted['featureit'] = '';
        }
        ### Strip html
        if (get_option('jr_html_allowed') == 'no') {
            $posted['details'] = strip_tags($posted['details']);
            $posted['apply'] = strip_tags($posted['apply']);
        }
        ### End strip
        // Check required fields
        $required = array('job_title' => __('Job title', 'appthemes'), 'job_term_type' => __('Job type', 'appthemes'), 'details' => __('Job description', 'appthemes'));
        if (get_option('jr_submit_cat_required') == 'yes') {
            $submit_cat = array('job_term_cat' => __('Job category', 'appthemes'));
            $required = array_merge($required, $submit_cat);
        }
        foreach ($required as $field => $name) {
            if (empty($posted[$field])) {
                $errors->add('submit_error', __('<strong>ERROR</strong>: &ldquo;', 'appthemes') . $name . __('&rdquo; is a required field.', 'appthemes'));
            }
        }
        if ($errors && sizeof($errors) > 0 && $errors->get_error_code()) {
            // Do nothing - edit has failed
        } else {
            // So far, so good. Upload logo if set.
            if (isset($_FILES['company-logo']) && !empty($_FILES['company-logo']['name'])) {
                $posted['company-logo-name'] = $_FILES['company-logo']['name'];
                // Check valid extension
                $allowed = array('png', 'gif', 'jpg', 'jpeg');
                //$extension = strtolower(pathinfo($_FILES['company-logo']['name'], PATHINFO_EXTENSION));
                $extension = strtolower(substr(strrchr($_FILES['company-logo']['name'], "."), 1));
                if (!in_array($extension, $allowed)) {
                    $errors->add('submit_error', __('<strong>ERROR</strong>: Only jpg, gif, and png images are allowed.', 'appthemes'));
                }
                function company_logo_upload_dir($pathdata)
                {
                    $subdir = '/company_logos' . $pathdata['subdir'];
                    $pathdata['path'] = str_replace($pathdata['subdir'], $subdir, $pathdata['path']);
                    $pathdata['url'] = str_replace($pathdata['subdir'], $subdir, $pathdata['url']);
                    $pathdata['subdir'] = str_replace($pathdata['subdir'], $subdir, $pathdata['subdir']);
                    return $pathdata;
                }
                add_filter('upload_dir', 'company_logo_upload_dir');
                $time = current_time('mysql');
                $overrides = array('test_form' => false);
                $file = wp_handle_upload($_FILES['company-logo'], $overrides, $time);
                remove_filter('upload_dir', 'company_logo_upload_dir');
                if (!isset($file['error'])) {
                    $posted['company-logo'] = $file['url'];
                    $posted['company-logo-type'] = $file['type'];
                    $posted['company-logo-file'] = $file['file'];
                } else {
                    $errors->add('submit_error', __('<strong>ERROR</strong>: ', 'appthemes') . $file['error'] . '');
                }
            }
        }
        if ($errors && sizeof($errors) > 0 && $errors->get_error_code()) {
            // Do nothing - edit has failed
        } else {
            // Good to go, lets save this bad boy and show a confirmation message
            // Calc costs/get packs
            $cost = 0;
            $job_pack = '';
            $user_pack = '';
            $jobs_last = '';
            // Get Pack from previous step
            if (isset($posted['job_pack']) && !empty($posted['job_pack'])) {
                if (strstr($posted['job_pack'], 'user_')) {
                    $user_pack_id = (int) str_replace('user_', '', $posted['job_pack']);
                    $user_pack = new jr_user_pack($user_pack_id);
                    if (!$user_pack->get_valid_pack()) {
                        wp_die(__('Error: Invalid Pack.', 'appthemes'));
                    }
                    $jobs_last = $user_pack->job_duration;
                } else {
                    $job_pack = new jr_pack($posted['job_pack']);
                    $cost += $job_pack->pack_cost;
                    $jobs_last = $job_pack->job_duration;
                }
            } else {
                // No Packs
                $listing_cost = get_option('jr_jobs_relisting_cost');
                $cost += $listing_cost;
            }
            // Caculate expirey date from today
            if (!$jobs_last) {
                $jobs_last = 30;
            }
            // 30 day default
            $date = strtotime('+' . $jobs_last . ' day', current_time('timestamp'));
            // Get Featured from previous step
            if ($posted['featureit'] == 'yes') {
                $featured_cost = get_option('jr_cost_to_feature');
                $cost += $featured_cost;
            }
            ### Approval needed?
            $status = 'publish';
            if ($cost > 0 && !$user_pack) {
                $status = 'pending';
            }
            ### Update Post
            $data = array('ID' => $job_details->ID, 'post_content' => $wpdb->escape($posted['details']), 'post_title' => $wpdb->escape($posted['job_title']), 'post_status' => $status, 'post_date' => date('Y-m-d H:i:s'), 'post_date_gmt' => get_gmt_from_date(date('Y-m-d H:i:s')));
            wp_update_post($data);
            ### Update meta data and category
            update_post_meta($job_details->ID, '_Company', $posted['your_name']);
            update_post_meta($job_details->ID, '_CompanyURL', $posted['website']);
            update_post_meta($job_details->ID, '_how_to_apply', $posted['apply']);
            update_post_meta($job_details->ID, '_expires', $date);
            $post_into_cats = array();
            $post_into_types = array();
            if ($posted['job_term_cat'] > 0) {
                $post_into_cats[] = get_term_by('id', $posted['job_term_cat'], 'job_cat')->slug;
            }
            ### Set Categories
            if ($posted['featureit'] == 'yes') {
                global $featured_job_cat_id;
                $featured_job_cat_name = get_term_by('id', $featured_job_cat_id, 'job_cat')->name;
                $post_into_cats[] = sanitize_title($featured_job_cat_name);
            }
            if (sizeof($post_into_cats) > 0) {
                wp_set_object_terms($job_details->ID, $post_into_cats, 'job_cat');
            }
            $post_into_types[] = get_term_by('slug', sanitize_title($posted['job_term_type']), 'job_type')->slug;
            if (sizeof($post_into_types) > 0) {
                wp_set_object_terms($job_details->ID, $post_into_types, 'job_type');
            }
            ### Salary
            $salary = array();
            if ($posted['job_term_salary'] > 0) {
                $salary[] = get_term_by('id', $posted['job_term_salary'], 'job_salary')->slug;
            }
            wp_set_object_terms($job_details->ID, $salary, 'job_salary');
            ### Tags
            if ($posted['tags']) {
                $thetags = explode(',', $posted['tags']);
                $thetags = array_map('trim', $thetags);
                $thetags = array_map('strtolower', $thetags);
                if (sizeof($thetags) > 0) {
                    wp_set_object_terms($job_details->ID, $thetags, 'job_tag');
                }
            }
            ### GEO
            if (!empty($posted['jr_address'])) {
                $latitude = jr_clean_coordinate($posted['jr_geo_latitude']);
                $longitude = jr_clean_coordinate($posted['jr_geo_longitude']);
                update_post_meta($job_details->ID, '_jr_geo_latitude', $latitude);
                update_post_meta($job_details->ID, '_jr_geo_longitude', $longitude);
                if ($latitude && $longitude) {
                    $address = jr_reverse_geocode($latitude, $longitude);
                    update_post_meta($job_details->ID, 'geo_address', $address['address']);
                    update_post_meta($job_details->ID, 'geo_country', $address['country']);
                    update_post_meta($job_details->ID, 'geo_short_address', $address['short_address']);
                    update_post_meta($job_details->ID, 'geo_short_address_country', $address['short_address_country']);
                }
            } else {
                // They left the field blank so we assume the job is for 'anywhere'
                delete_post_meta($job_details->ID, '_jr_geo_latitude');
                delete_post_meta($job_details->ID, '_jr_geo_longitude');
                delete_post_meta($job_details->ID, 'geo_address');
                delete_post_meta($job_details->ID, 'geo_country');
                delete_post_meta($job_details->ID, 'geo_short_address');
                delete_post_meta($job_details->ID, 'geo_short_address_country');
            }
            ## Link to company image
            if (isset($posted['company-logo']) && $posted['company-logo']) {
                $name_parts = pathinfo($posted['company-logo-name']);
                $name = trim(substr($name, 0, -(1 + strlen($name_parts['extension']))));
                $url = $posted['company-logo'];
                $type = $posted['company-logo-type'];
                $file = $posted['company-logo-file'];
                $title = $posted['company-logo-name'];
                $content = '';
                // use image exif/iptc data for title and caption defaults if possible
                if ($image_meta = @wp_read_image_metadata($file)) {
                    if (trim($image_meta['title'])) {
                        $title = $image_meta['title'];
                    }
                    if (trim($image_meta['caption'])) {
                        $content = $image_meta['caption'];
                    }
                }
                // Construct the attachment array
                $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $job_details->ID, 'post_title' => $title, 'post_content' => $content), array());
                // Save the data
                $id = wp_insert_attachment($attachment, $file, $job_details->ID);
                if (!is_wp_error($id)) {
                    wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file));
                }
                update_post_meta($job_details->ID, '_thumbnail_id', $id);
            }
            ### If paying with pack, update the customers pack totals
            if ($user_pack) {
                $inspack = '';
                $user_pack->inc_job_count();
            } elseif (!empty($posted['job_pack'])) {
                $inspack = $posted['job_pack'];
            }
            if ($posted['featureit'] == 'yes') {
                $insfeatured = 1;
            } else {
                $insfeatured = 0;
            }
            ### Create the order in the database so it can be confirmed by user/IPN before going live
            if ($cost > 0) {
                $jr_order = new jr_order(0, $user_ID, $cost, $job_details->ID, $inspack, $insfeatured);
                $jr_order->insert_order();
                ### Redirect to paypal payment page	(if paid listing)
                $name = urlencode(__('Relisting ', 'appthemes') . $posted['job_title'] . __(' w/ Job Pack ', 'appthemes') . $job_pack->pack_name);
                $link = $jr_order->generate_paypal_link($name);
                header('Location: ' . $link);
                exit;
            } else {
                ### Relisting was free
                wp_mail(get_option('admin_email'), __('Job Re-Listed ', 'appthemes') . '[' . get_bloginfo('name') . ']', __('A job has been re-listed called  ', 'appthemes') . '"' . $posted['job_title'] . '" (' . __('ID', 'appthemes') . ': ' . $job_details->ID . ")\n\nEdit post link: " . admin_url("post.php?action=edit&post=" . $job_details->ID . "") . "\nView Post: " . get_permalink($job_details->ID));
                ### Redirect to my jobs
                $args = array('message' => urlencode('Job relisted successfully'));
                redirect_myjobs($args);
            }
        }
        // endif errors
    }
    $form_results = array('errors' => $errors, 'posted' => $posted);
    return $form_results;
}
Example #2
0
/**
 * JobRoller Confirm Job Process
 * Once a job has been confirmed, this takes the posted data and adds it to WordPress.
 *
 *
 * @version 1.3
 * @author AppThemes
 * @package JobRoller
 * @copyright 2010 all rights reserved
 *
 */
function jr_process_confirm_job_form()
{
    global $user_ID, $post, $posted, $wpdb, $jr_log;
    $posted = json_decode(stripslashes($_POST['posted']), true);
    // Calc costs/get packs
    $cost = 0;
    $job_pack = '';
    $user_pack = '';
    $jobs_last = null;
    // Get Pack from previous step
    if (isset($posted['job_pack']) && !empty($posted['job_pack'])) {
        if (strstr($posted['job_pack'], 'user_')) {
            $user_pack_id = (int) str_replace('user_', '', $posted['job_pack']);
            $user_pack = new jr_user_pack($user_pack_id);
            if (!$user_pack->get_valid_pack()) {
                wp_die(__('Error: Invalid Pack.', 'appthemes'));
            }
            $jobs_last = $user_pack->job_duration;
        } else {
            $job_pack = new jr_pack($posted['job_pack']);
            $cost += $job_pack->pack_cost;
            $jobs_last = $job_pack->job_duration;
        }
    } else {
        // No Packs
        $listing_cost = get_option('jr_jobs_listing_cost');
        $cost += $listing_cost;
    }
    // Caculate expirey date from today
    if (!isset($jobs_last)) {
        $jobs_last = get_option('jr_jobs_default_expires');
    }
    // 30 day default
    $date = strtotime('+' . $jobs_last . ' day', current_time('timestamp'));
    // Get Featured from previous step
    if ($posted['featureit'] == 'yes') {
        $featured_cost = get_option('jr_cost_to_feature');
        $cost += $featured_cost;
    }
    ### Validate required again - just in case
    $required = array('job_title' => __('Job title', 'appthemes'), 'job_term_type' => __('Job type', 'appthemes'), 'details' => __('Job description', 'appthemes'));
    foreach ($required as $field => $name) {
        if (empty($posted[$field])) {
            wp_die(__('Error: Unable to create entry.', 'appthemes'));
        }
    }
    ### Approval needed?
    $status = 'publish';
    if ($cost > 0) {
        $status = 'pending';
    } else {
        // Free listing
        if (get_option('jr_jobs_require_moderation') == 'yes') {
            $status = 'pending';
        }
    }
    if ($user_pack) {
        $status = 'publish';
    }
    ### Create Post
    $data = array('post_content' => $wpdb->escape($posted['details']), 'post_title' => $wpdb->escape(strip_tags($posted['job_title'])), 'post_status' => $status, 'post_author' => $user_ID, 'post_type' => 'job_listing');
    jr_before_insert_job($data);
    // do_action hook
    $post_id = wp_insert_post($data);
    jr_after_insert_job($post_id);
    // do_action hook
    // Was the post created?
    if ($post_id == 0 || is_wp_error($post_id)) {
        wp_die(__('Error: Unable to create entry.', 'appthemes'));
    }
    ### Add meta data and category
    add_post_meta($post_id, '_Company', strip_tags($posted['your_name']), true);
    add_post_meta($post_id, '_CompanyURL', $posted['website'], true);
    add_post_meta($post_id, '_how_to_apply', $posted['apply'], true);
    add_post_meta($post_id, 'user_IP', jr_getIP(), true);
    add_post_meta($post_id, '_expires', $date);
    ### GEO
    if (!empty($posted['jr_address'])) {
        $latitude = jr_clean_coordinate($posted['jr_geo_latitude']);
        $longitude = jr_clean_coordinate($posted['jr_geo_longitude']);
        add_post_meta($post_id, '_jr_geo_latitude', $posted['jr_geo_latitude'], true);
        add_post_meta($post_id, '_jr_geo_longitude', $posted['jr_geo_longitude'], true);
        if ($latitude && $longitude) {
            // If we don't have address data, do a look-up
            if ($posted['jr_geo_short_address'] && $posted['jr_geo_country'] && $posted['jr_geo_short_address'] && $posted['jr_geo_short_address_country']) {
                add_post_meta($post_id, 'geo_address', $posted['jr_geo_short_address'], true);
                add_post_meta($post_id, 'geo_country', $posted['jr_geo_country'], true);
                add_post_meta($post_id, 'geo_short_address', $posted['jr_geo_short_address'], true);
                add_post_meta($post_id, 'geo_short_address_country', $posted['jr_geo_short_address_country'], true);
            } else {
                $address = jr_reverse_geocode($latitude, $longitude);
                add_post_meta($post_id, 'geo_address', $address['address'], true);
                add_post_meta($post_id, 'geo_country', $address['country'], true);
                add_post_meta($post_id, 'geo_short_address', $address['short_address'], true);
                add_post_meta($post_id, 'geo_short_address_country', $address['short_address_country'], true);
            }
        }
    } else {
        // They left the field blank so we assume the job is for 'anywhere'
    }
    ### Set terms
    $post_into_cats = array();
    $post_into_types = array();
    if ($posted['job_term_cat'] > 0) {
        $post_into_cats[] = get_term_by('id', $posted['job_term_cat'], 'job_cat')->slug;
    }
    if ($featured_cost && is_numeric($featured_cost) && $featured_cost > 0 && $posted['featureit'] == 'yes') {
        global $featured_job_cat_id;
        $featured_job_cat_name = get_term_by('id', $featured_job_cat_id, 'job_cat')->name;
        $post_into_cats[] = sanitize_title($featured_job_cat_name);
    }
    if (sizeof($post_into_cats) > 0) {
        wp_set_object_terms($post_id, $post_into_cats, 'job_cat');
    }
    $post_into_types[] = get_term_by('slug', sanitize_title($posted['job_term_type']), 'job_type')->slug;
    if (sizeof($post_into_types) > 0) {
        wp_set_object_terms($post_id, $post_into_types, 'job_type');
    }
    ### Salary
    $salary = array();
    if ($posted['job_term_salary'] > 0) {
        $salary[] = get_term_by('id', $posted['job_term_salary'], 'job_salary')->slug;
    }
    if (sizeof($salary) > 0) {
        wp_set_object_terms($post_id, $salary, 'job_salary');
    }
    ### Tags
    if ($posted['tags']) {
        $thetags = explode(',', $posted['tags']);
        $thetags = array_map('trim', $thetags);
        $thetags = array_map('strtolower', $thetags);
        if (sizeof($thetags) > 0) {
            wp_set_object_terms($post_id, $thetags, 'job_tag');
        }
    }
    ## Load APIs and Link to company image
    include_once ABSPATH . 'wp-admin/includes/file.php';
    include_once ABSPATH . 'wp-admin/includes/image.php';
    include_once ABSPATH . 'wp-admin/includes/media.php';
    $name_parts = pathinfo($posted['company-logo-name']);
    $name = trim(substr($name, 0, -(1 + strlen($name_parts['extension']))));
    $url = $posted['company-logo'];
    $type = $posted['company-logo-type'];
    $file = $posted['company-logo-file'];
    $title = $posted['company-logo-name'];
    $content = '';
    if ($file) {
        // use image exif/iptc data for title and caption defaults if possible
        if ($image_meta = @wp_read_image_metadata($file)) {
            if (trim($image_meta['title'])) {
                $title = $image_meta['title'];
            }
            if (trim($image_meta['caption'])) {
                $content = $image_meta['caption'];
            }
        }
        // Construct the attachment array
        $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content), array());
        // Save the data
        $id = wp_insert_attachment($attachment, $file, $post_id);
        if (!is_wp_error($id)) {
            wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file));
        }
        update_post_meta($post_id, '_thumbnail_id', $id);
    }
    ### If paying with user pack, update the customers pack totals
    if ($user_pack) {
        $inspack = '';
        $user_pack->inc_job_count();
    } elseif (!empty($posted['job_pack'])) {
        $inspack = $posted['job_pack'];
    }
    if ($posted['featureit'] == 'yes') {
        $insfeatured = 1;
    }
    ### Create the order in the database so it can be confirmed by user/IPN before going live
    if ($cost > 0) {
        $jr_order = new jr_order(0, $user_ID, $cost, $post_id, $inspack, $insfeatured);
        $jr_order->insert_order();
        ### Redirect to paypal payment page	(if paid listing)
        $name = urlencode(__('Job Listing ', 'appthemes') . $posted['job_title'] . __(' with Job Pack ', 'appthemes') . $job_pack->pack_name);
        $link = $jr_order->generate_paypal_link($name);
        $jr_log->write_log('Sending user (#' . $user_ID . ') to paypal after job submission (' . $posted['job_title'] . ' - order#' . $jr_order->id . ').');
        header('Location: ' . $link);
        exit;
    } else {
        ### FREE LISTING / LISTING PAID WITH USER PACK (no additional cost)
        if (get_option('jr_jobs_require_moderation') == 'yes') {
            if (get_option('jr_new_ad_email') !== 'no') {
                jr_admin_new_job_pending($post_id);
            }
            jr_owner_new_job_pending($post_id);
        } elseif (get_option('jr_new_ad_email') !== 'no') {
            jr_admin_new_job($post_id);
        }
        redirect_myjobs();
    }
}