Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Plugin Name: WP Job Manager - My Custom Content
- * Description: Loads my custom content to WP Job Manager
- * Author: Adam Hurlebaus
- * Version: 1.1.0
- * Text Domain: multidigmjm
- */
- // Exit if accessed directly
- if ( ! defined( 'ABSPATH' ) ) exit;
- if ( ! class_exists( 'WP_Job_Manager' ) ) {
- include( ABSPATH . 'wp-content/plugins/wp-job-manager/includes/abstracts/abstract-wp-job-manager-form.php' );
- }
- class My_Custom_Job_Manager_Content extends WP_Job_Manager_Form{
- /**
- * @var $instance
- */
- private static $instance;
- /**
- * Make sure only one instance is only running.
- */
- public static function instance() {
- if ( ! isset ( self::$instance ) ) {
- self::$instance = new self;
- }
- return self::$instance;
- }
- /**
- * Start things up.
- *
- * @since 1.0
- */
- public function __construct() {
- define( 'CUSTOM_JOB_MANAGER_PLUGIN_DIR', untrailingslashit( plugin_dir_path( __FILE__ ) ) );
- define( 'CUSTOM_JOB_MANAGER_PLUGIN_URL', untrailingslashit( plugins_url( basename( plugin_dir_path( __FILE__ ) ), basename( __FILE__ ) ) ) );
- $this->setup_globals();
- $this->setup_actions();
- }
- /**
- * Set some smart defaults to class variables. Allow some of them to be
- * filtered to allow for early overriding.
- *
- * @since 1.0
- *
- * @return void
- */
- private function setup_globals() {
- $this->file = __FILE__;
- $this->basename = apply_filters( 'multidigmjm_plugin_basenname', plugin_basename( $this->file ) );
- $this->plugin_dir = apply_filters( 'multidigmjm_plugin_dir_path', plugin_dir_path( $this->file ) );
- $this->plugin_url = apply_filters( 'multidigmjm_plugin_dir_url', plugin_dir_url ( $this->file ) );
- $this->lang_dir = apply_filters( 'multidigmjm_lang_dir', trailingslashit( $this->plugin_dir . 'languages' ) );
- $this->domain = 'multidigmjm';
- }
- /**
- * Setup the default hooks and actions for editing cutsom taxonomies and custom fields
- *
- * @since 1.0
- *
- * @return void
- */
- private function setup_actions() {
- add_action( 'init', array( $this, 'register_post_taxonomy' ) );
- add_filter( 'submit_job_form_fields', array( $this, 'form_fields' ) );
- add_action( 'job_manager_update_job_data', array( $this, 'update_job_data' ), 10, 2 );
- add_filter( 'submit_job_form_fields_get_job_data', array( $this, 'form_fields_get_job_data' ), 10, 2 );
- add_filter( 'submit_job_form_fields_get_user_data', array( $this, 'form_fields_get_user_data' ) );
- add_filter( 'job_manager_job_listing_data_fields', array( $this, 'job_listing_data_fields' ) );
- add_action( 'single_job_listing_meta_end', array( $this, 'display_custom_property_type' ) );
- add_action( 'submit_job_form_job_fields_end', array( $this, 'add_property_fields_to_submit' ) );
- add_filter( 'submit_job_form_validate_fields', array( $this, 'custom_job_form_validate_fields' ), 10, 3 );
- add_action( 'wp_enqueue_scripts', array( $this, 'my_script_method' ) );
- add_filter( 'job_manager_locate_template', array( $this, 'custom_field_job_manager_locate_template' ), 10, 3 );
- $this->load_textdomain();
- }
- /**
- * Create the `job_listing_property_type` taxonomy.
- *
- * @since 1.0
- */
- public function register_post_taxonomy() {
- $admin_capability = 'manage_job_listings';
- $singular = __( 'Property Type', 'multidigmjm' );
- $plural = __( 'Property Types', 'multidigmjm' );
- if ( current_theme_supports( 'job-manager-templates' ) ) {
- $rewrite = array(
- 'slug' => _x( 'property-type', 'Property type slug - resave permalinks after changing this', 'multidigmjm' ),
- 'with_front' => false,
- 'hierarchical' => false
- );
- } else {
- $rewrite = false;
- }
- register_taxonomy( 'job_listing_property_type',
- array( 'job_listing' ),
- array(
- 'hierarchical' => true,
- 'update_count_callback' => '_update_post_term_count',
- 'label' => $plural,
- 'labels' => array(
- 'name' => $plural,
- 'singular_name' => $singular,
- 'search_items' => sprintf( __( 'Search %s', 'multidigmjm' ), $plural ),
- 'all_items' => sprintf( __( 'All %s', 'multidigmjm' ), $plural ),
- 'parent_item' => sprintf( __( 'Parent %s', 'multidigmjm' ), $singular ),
- 'parent_item_colon' => sprintf( __( 'Parent %s:', 'multidigmjm' ), $singular ),
- 'edit_item' => sprintf( __( 'Edit %s', 'multidigmjm' ), $singular ),
- 'update_item' => sprintf( __( 'Update %s', 'multidigmjm' ), $singular ),
- 'add_new_item' => sprintf( __( 'Add New %s', 'multidigmjm' ), $singular ),
- 'new_item_name' => sprintf( __( 'New %s Name', 'multidigmjm' ), $singular )
- ),
- 'show_ui' => true,
- 'query_var' => true,
- 'has_archive' => true,
- 'capabilities' => array(
- 'manage_terms' => $admin_capability,
- 'edit_terms' => $admin_capability,
- 'delete_terms' => $admin_capability,
- 'assign_terms' => $admin_capability,
- ),
- 'rewrite' => $rewrite,
- )
- );
- }
- /**
- * Get the value of a repeated fields (e.g. property contacts)
- * @param array $fields
- * @return array
- */
- public function get_repeated_field( $field_prefix, $fields ) {
- $items = array();
- $field_keys = array_keys( $fields );
- $first_field = current( $field_keys );
- if ( ! empty( $_POST[ $field_prefix . '_' . $first_field ] ) && is_array( $_POST[ $field_prefix . '_' . $first_field ] ) ) {
- $keys = array_keys( $_POST[ $field_prefix . '_' . $first_field ] );
- foreach ( $keys as $posted_key ) {
- $item = array();
- foreach ( $fields as $key => $field ) {
- switch ( $field['type'] ) {
- case 'textarea' :
- $item[ $key ] = wp_kses_post( stripslashes( $_POST[ $field_prefix . '_' . $key ][ $posted_key ] ) );
- break;
- default :
- if ( is_array( $_POST[ $field_prefix . '_' . $key ][ $posted_key ] ) ) {
- $item[ $key ] = array_filter( array_map( 'sanitize_text_field', array_map( 'stripslashes', $_POST[ $field_prefix . '_' . $key ][ $posted_key ] ) ) );
- } else {
- $item[ $key ] = sanitize_text_field( stripslashes( $_POST[ $field_prefix . '_' . $key ][ $posted_key ] ) );
- }
- break;
- }
- if ( empty( $item[ $key ] ) && ! empty( $field['required'] ) ) {
- continue 2;
- }
- }
- $items[] = $item;
- }
- }
- return $items;
- }
- /**
- * Get the value of a posted file field
- * @param string $key
- * @param array $field
- * @return string
- */
- public function get_posted_contact_field( $key, $field ) {
- return apply_filters( 'submit_job_form_fields_get_contact_data', $this->get_repeated_field( $key, $field['fields'] ) );
- }
- /**
- * Get the value of a posted file field
- * @param string $key
- * @param array $field
- * @return string
- */
- public function get_posted_location_field( $key, $field ) {
- return apply_filters( 'submit_job_form_fields_get_location_data', $this->get_repeated_field( $key, $field['fields'] ) );
- }
- /**
- * Filter plugin specific templates via the 'job_manager_locate_template' filter
- *
- * @since 1.0
- */
- function custom_field_job_manager_locate_template( $template, $template_name, $template_path ) {
- if ( 'form-fields/location-field.php' === $template_name || 'form-fields/contact-field.php' === $template_name ) {
- return CUSTOM_JOB_MANAGER_PLUGIN_DIR . '/templates/' . $template_name;
- }
- return $template;
- }
- // Script to include datepicker
- function my_script_method () {
- wp_enqueue_script('wp-job-manager-custom', plugin_dir_url( __FILE__ ) . '/js/custom-wp-job-manager.js', array( 'jquery', 'jquery-ui-datepicker', 'jquery-ui-sortable' ));
- wp_localize_script( 'wp-job-manager-custom', 'job_manager_job_submission', array(
- 'i18n_navigate' => __( 'If you wish to edit the posted details use the "edit job" button instead, otherwise changes may be lost.', 'wp-job-manager' ),
- 'i18n_confirm_remove' => __( 'Are you sure you want to remove this item?', 'wp-job-manager' ),
- 'i18n_remove' => __( 'remove', 'wp-job-manager' )
- ) );
- wp_enqueue_style('jquery-style', 'https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.2/themes/smoothness/jquery-ui.css');
- }
- /**
- * Add custom fields to the submission form.
- *
- * @since 1.0
- */
- function form_fields( $fields ) {
- // Modify default fields
- unset($fields['job']['job_location']);
- unset($fields['company']['company_logo']);
- unset($fields['company']['company_video']);
- unset($fields['company']['company_twitter']);
- $fields['job']['job_start'] = array(
- 'label' => __( 'Project start date', 'wp-job-manager' ),
- 'type' => 'text',
- 'placeholder' => 'Click to select date',
- 'required' => false,
- 'priority' => 10
- );
- $fields['job']['job_end'] = array(
- 'label' => __( 'Project end date', 'wp-job-manager' ),
- 'type' => 'text',
- 'placeholder' => 'Click to select date',
- 'required' => false,
- 'priority' => 11
- );
- // Add custom "Property" fields
- $fields['property'] = array();
- $fields['property']['property_type'] = array(
- 'label' => __( 'Property type', 'wp-job-manager' ),
- 'type' => 'select',
- 'options' => multidigmjm_get_properties_simple(),
- 'required' => true,
- 'priority' => '1'
- );
- $fields[ 'property' ][ 'property_name' ] = array(
- 'label' => __( 'Name of property', 'wp-job-manager' ),
- 'type' => 'text',
- 'required' => false,
- 'placeholder' => __( 'Enter the name of the building or complex', 'wp-job-manager' ),
- 'priority' => 2
- );
- $fields[ 'property' ][ 'property_size' ] = array(
- 'label' => __( 'Size of property', 'wp-job-manager' ),
- 'type' => 'text',
- 'required' => true,
- 'placeholder' => __( 'Enter number of units on property', 'wp-job-manager' ),
- 'priority' => 3
- );
- $fields[ 'property' ][ 'property_year' ] = array(
- 'label' => __( 'Year built', 'wp-job-manager' ),
- 'type' => 'text',
- 'required' => true,
- 'placeholder' => __( 'What year was your property built (numbers only)', 'wp-job-manager' ),
- 'priority' => 4
- );
- $fields[ 'property' ][ 'property_contact' ] = array(
- 'label' => __( 'Point of contact', 'wp-job-manager'),
- 'type' => 'contact',
- 'required' => false,
- 'placeholder' => '',
- 'priority' => 5,
- 'fields' => array(
- 'name' => array(
- 'label' => __( 'Name', 'wp-job-manager' ),
- 'type' => 'text',
- 'required' => true,
- 'placeholder' => ''
- ),
- 'title' => array(
- 'label' => __( 'Title', 'wp-job-manager' ),
- 'type' => 'text',
- 'required' => true,
- 'placeholder' => ''
- ),
- 'email' => array(
- 'label' => __( 'Email address', 'wp-job-manager' ),
- 'type' => 'text',
- 'required' => true,
- 'placeholder' => ''
- ),
- 'phone' => array(
- 'label' => __( 'Phone number', 'wp-job-manager' ),
- 'type' => 'textarea',
- 'required' => true,
- 'placeholder' => ''
- )
- )
- );
- $fields[ 'property' ][ 'property_location' ] = array(
- 'label' => __( 'Property location', 'wp-job-manager'),
- 'type' => 'location',
- 'required' => false,
- 'placeholder' => '',
- 'priority' => 6,
- 'fields' => array(
- 'street' => array(
- 'label' => __( 'Street address', 'wp-job-manager' ),
- 'type' => 'text',
- 'required' => true,
- 'placeholder' => ''
- ),
- 'street_two' => array(
- 'label' => __( 'Street address 2', 'wp-job-manager' ),
- 'type' => 'text',
- 'required' => false,
- 'placeholder' => ''
- ),
- 'city' => array(
- 'label' => __( 'City', 'wp-job-manager' ),
- 'type' => 'text',
- 'required' => true,
- 'placeholder' => ''
- ),
- 'state' => array(
- 'label' => __( 'State', 'wp-job-manager' ),
- 'type' => 'select',
- 'options' => array ('Alabama'=>"AL", 'Alaska'=>"AK", 'Arizona'=>"AZ", 'Arkansas'=>"AR", 'California'=>"CA", 'Colorado'=>"CO", 'Connecticut'=>"CT", 'Delaware'=>"DE", 'District Of Columbia'=>"DC", 'Florida'=>"FL", 'Georgia'=>"GA", 'Hawaii'=>"HI", 'Idaho'=>"ID", 'Illinois'=>"IL", 'Indiana'=>"IN", 'Iowa'=>"IA", 'Kansas'=>"KS", 'Kentucky'=>"KY", 'Louisiana'=>"LA", 'Maine'=>"ME", 'Maryland'=>"MD", 'Massachusetts'=>"MA", 'Michigan'=>"MI", 'Minnesota'=>"MN", 'Mississippi'=>"MS", 'Missouri'=>"MO", 'Montana'=>"MT", 'Nebraska'=>"NE", 'Nevada'=>"NV", 'New Hampshire'=>"NH", 'New Jersey'=>"NJ", 'New Mexico'=>"NM", 'New York'=>"NY", 'North Carolina'=>"NC", 'North Dakota'=>"ND", 'Ohio'=>"OH", 'Oklahoma'=>"OK", 'Oregon'=>"OR", 'Pennsylvania'=>"PA", 'Rhode Island'=>"RI", 'South Carolina'=>"SC", 'South Dakota'=>"SD", 'Tennessee'=>"TN", 'Texas'=>"TX", 'Utah'=>"UT", 'Vermont'=>"VT", 'Virginia'=>"VA", 'Washington'=>"WA", 'West Virginia'=>"WV", 'Wisconsin'=>"WI", 'Wyoming'=>"WY"),
- 'required' => true,
- 'placeholder' => __( '-', 'wp-job-manager' )
- ),
- 'zip' => array(
- 'label' => __( 'Zip code', 'wp-job-manager' ),
- 'type' => 'text',
- 'required' => true,
- 'placeholder' => ''
- )
- )
- );
- return $fields;
- }
- /**
- * Get the current value for the property type. We can't rely
- * on basic meta value getting, instead we need to find the term.
- *
- * @since 1.0
- */
- function form_fields_get_job_data( $fields, $job ) {
- $fields['property']['property_type']['value'] = current( wp_get_object_terms( $job->ID, 'job_listing_property_type', array( 'fields' => 'slugs' ) ) );
- return $fields;
- }
- /**
- * Get the user data.
- *
- * @since 1.0
- */
- function form_fields_get_user_data( $fields ) {
- if ( is_user_logged_in() && empty( $_POST['submit_job'] ) ) {
- if ( ! empty( $fields['property'] ) ) {
- foreach ( $fields['property'] as $key => $field ) {
- $fields['property'][ $key ]['value'] = get_user_meta( get_current_user_id(), '_' . $key, true );
- }
- }
- return $fields;
- }
- }
- /**
- * When the form is submitted, update the data.
- *
- * @since 1.0
- */
- function update_job_data( $job_id, $values ) {
- // Loop fields and save meta and term data
- foreach ( $this->fields as $group_key => $group_fields ) {
- foreach ( $group_fields as $key => $field ) {
- update_post_meta( $job_id, '_' . $key, $values[ $group_key ][ $key ] );
- }
- }
- if ( is_user_logged_in() ) {
- update_user_meta( get_current_user_id(), '_property_name', isset( $values['property']['property_name'] ) ? $values['property']['property_name'] : '' );
- update_user_meta( get_current_user_id(), '_property_size', isset( $values['property']['property_size'] ) ? $values['property']['property_size'] : '' );
- update_user_meta( get_current_user_id(), '_property_year', isset( $values['property']['property_year'] ) ? $values['property']['property_year'] : '' );
- }
- $property = isset ( $values[ 'property' ][ 'property_type' ] ) ? $values[ 'property' ][ 'property_type' ] : null;
- if ( ! $property )
- return;
- $term = get_term_by( 'slug', $property, 'job_listing_property_type' );
- wp_set_post_terms( $job_id, array( $term->term_id ), 'job_listing_property_type', false );
- }
- /**
- * Create Admin write panels for custom content.
- *
- * @since 1.0
- */
- function job_listing_data_fields( $fields ) {
- unset($fields['_job_location']);
- $fields['_property_name'] = array(
- 'label' => __( 'Property name', 'wp-job-manager' ),
- 'placeholder' => __( '' ),
- 'description' => __( 'Leave this blank if there is no name', 'wp-job-manager' )
- );
- $fields['_property_size'] = array(
- 'label' => __( 'Property size', 'wp-job-manager' ),
- 'placeholder' => __( '', 'wp-job-manager' ),
- 'description' => __( 'Number of units on property (numbers only)', 'wp-job-manager' )
- );
- $fields['_property_year'] = array(
- 'label' => __( 'Year built', 'wp-job-manager' ),
- 'placeholder' => __( '', 'wp-job-manager' ),
- 'description' => __( 'What year was your property built (numbers only)', 'wp-job-manager' )
- );
- return $fields;
- }
- /**
- * Validate custom fields
- *
- * @since 1.0
- */
- function custom_job_form_validate_fields($success, $fields, $values) {
- $check_size = $values['property']['property_size'];
- $cleaned_size = preg_replace('/[^[:digit:]]/', '', $check_size);
- if ($check_size != $cleaned_size) {
- return new WP_Error( 'validation-error', 'Please only enter numbers for Property Size' );
- }
- $check_year = $values['property']['property_year'];
- $cleaned_year = preg_replace('/[^[:digit:]]/', '', $check_year);
- if ($check_year != $cleaned_year) {
- return new WP_Error( 'validation-error', 'Please only enter numbers for Year Built' );
- }
- return $success;
- }
- /**
- * On a singular job page, display the property type.
- *
- * @since 1.0
- */
- function display_custom_property_type() {
- global $post;
- /**$terms = wp_get_post_terms( $post->ID, 'job_listing_property_type' );
- if ( is_wp_error( $terms ) || empty( $terms ) )
- exit;
- $property = $terms[0];
- $propertyname = $property->name;
- if ( $property )
- echo '<li>' . $propertyname . '</li>';*/
- echo '<li>' . get_the_term_list( $post->ID, 'job_listing_property_type', '', ', ', '' ) . '</li>';
- }
- function add_property_fields_to_submit( $fields ) {
- global $post;
- if (strpos($post->post_content, 'submit') !== false){
- $form_fields = WP_Job_Manager_Form_Submit_Job::instance();
- $property_fields = $form_fields->get_fields( 'property' );
- } else {
- $form_fields = WP_Job_Manager_Form_Edit_Job::instance();
- $property_fields = $form_fields->get_fields( 'property' );
- }
- if ( $property_fields ) : ?>
- <h2><?php _e( 'Property Details', 'wp-job-manager' ); ?></h2>
- <?php foreach ( $property_fields as $key => $field ) : ?>
- <fieldset class="fieldset-<?php esc_attr_e( $key ); ?>">
- <label for="<?php esc_attr_e( $key ); ?>"><?php echo $field['label'] . apply_filters( 'submit_job_form_required_label', $field['required'] ? '' : ' <small>' . __( '(optional)', 'wp-job-manager' ) . '</small>', $field ); ?></label>
- <div class="field <?php echo $field['required'] ? 'required-field' : ''; ?>">
- <?php
- if ($field['type'] == ('contact' || 'location')){
- get_job_manager_template( 'form-fields/' . $field['type'] . '-field.php', array( 'key' => $key, 'field' => $field, 'class' => $this ) );
- }
- else get_job_manager_template( 'form-fields/' . $field['type'] . '-field.php', array( 'key' => $key, 'field' => $field ) );
- ?>
- </div>
- </fieldset>
- <?php endforeach;
- endif;
- }
- /**
- * Loads the plugin language files
- *
- * @since 1.0
- */
- public function load_textdomain() {
- // Traditional WordPress plugin locale filter
- $locale = apply_filters( 'plugin_locale', get_locale(), $this->domain );
- $mofile = sprintf( '%1$s-%2$s.mo', $this->domain, $locale );
- // Setup paths to current locale file
- $mofile_local = $this->lang_dir . $mofile;
- $mofile_global = WP_LANG_DIR . '/' . $this->domain . '/' . $mofile;
- // Look in global /wp-content/languages/multidigmjm folder
- if ( file_exists( $mofile_global ) ) {
- return load_textdomain( $this->domain, $mofile_global );
- // Look in local /wp-content/plugins/multidigmjm/languages/ folder
- } elseif ( file_exists( $mofile_local ) ) {
- return load_textdomain( $this->domain, $mofile_local );
- }
- return false;
- }
- }
- /**
- * Start things up.
- *
- * Use this function instead of a global.
- *
- * $multidigmjm = multidigmjm();
- *
- * @since 1.0
- */
- function multidigmjm() {
- return My_Custom_Job_Manager_Content::instance();
- }
- multidigmjm();
- /**
- * Get properties (terms) helper.
- *
- * @since 1.0
- */
- function multidigmjm_get_properties() {
- $properties = get_terms( 'job_listing_property_type', apply_filters( 'multidigmjm_get_property_args', array( 'hide_empty' => 0 ) ) );
- return $properties;
- }
- /**
- * Create a key => value pair of term ID and term name.
- *
- * @since 1.0
- */
- function multidigmjm_get_properties_simple() {
- $properties = multidigmjm_get_properties();
- $simple = array();
- foreach ( $properties as $property ) {
- $simple[ $property->slug ] = $property->name;
- }
- return apply_filters( 'multidigmjm_get_properties_simple', $simple );
- }
- /**
- * Custom widgets
- *
- * @since 1.1
- */
- function multidigmjm_widgets_init() {
- if ( ! class_exists( 'Custom_Property_Widget' ) )
- return;
- $multidigmjm = multidigmjm();
- include_once( $multidigmjm->plugin_dir . '/widgets.php' );
- register_widget( 'My_Custom_Job_Manager_Content_Widget' );
- }
- add_action( 'after_setup_theme', 'multidigmjm_widgets_init', 11 );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement