[ 'type' => 'text', 'label' => '', ], 'location' => [ 'type' => 'text', 'label' => '', ], 'companyDescription' => [ 'type' => 'html', 'label' => '', ], 'requirements' => [ 'type' => 'html', 'label' => '', ], 'additionalText' => [ 'type' => 'html', 'label' => '', ], 'footer' => [ 'type' => 'html', 'label' => '', ], 'responsibleUser' => [ 'type' => 'text', 'label' => '', ], 'responsibleEmail' => [ 'type' => 'email', 'label' => '', ], 'responsibleGender' => null, 'jobProfile' => [ 'type' => 'text', 'label' => '', ], 'url' => [ 'type' => 'text', 'label' => '', ], 'applyUrl' => [ 'type' => 'text', 'label' => '', ], 'dateOfCreation' => [ 'type' => 'static', 'label' => '', ], 'dateOfUpdate' => [ 'type' => 'static', 'label' => '', ], 'dateSinceOnline' => [ 'type' => 'static', 'label' => '', ], 'dateSinceOnlineCorrected' => [ 'type' => 'static', 'label' => '', ], 'occupationType' => [ 'type' => 'text', 'label' => '', ], 'careerLevel' => [ 'type' => 'text', 'label' => '', ], 'region' => [ 'type' => 'text', 'label' => '', ], 'publishedOn' => null, 'multiPostProviders' => null, ]; // --------------------------------------------------------------------------------------------------------------------- // ACTIVATION / DEACTIVATION (BEGIN) // --------------------------------------------------------------------------------------------------------------------- function arevos_job_ads_flush_rewrite_rules() { // call your CPT registration function here (it should also be hooked into 'init') arevos_job_ads_register_cpt(); flush_rewrite_rules(); } function arevos_job_ads_activate() { arevos_job_ads_flush_rewrite_rules(); } register_activation_hook( __FILE__, 'arevos_job_ads_activate' ); function arevos_job_ads_deactivate() { arevos_job_ads_flush_rewrite_rules(); } register_deactivation_hook( __FILE__, 'arevos_job_ads_deactivate' ); // --------------------------------------------------------------------------------------------------------------------- // ACTIVATION / DEACTIVATION (END) // --------------------------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------------------------- // REGISTER POST TYPE (BEGIN) // --------------------------------------------------------------------------------------------------------------------- function arevos_job_ads_register_cpt() { $labels = [ 'name' => __( 'Job Ads', AREVOS_JOB_ADS_TEXT_DOMAIN ), 'singular_name' => __( 'Job Ad', AREVOS_JOB_ADS_TEXT_DOMAIN ), 'menu_name' => __( 'Job Ads', AREVOS_JOB_ADS_TEXT_DOMAIN ), 'name_admin_bar' => __( 'Job Ads', AREVOS_JOB_ADS_TEXT_DOMAIN ), 'all_items' => __( 'All Job Ads', AREVOS_JOB_ADS_TEXT_DOMAIN ), 'add_new' => __( 'Add New', AREVOS_JOB_ADS_TEXT_DOMAIN ), 'add_new_item' => __( 'Add New Job Ad', AREVOS_JOB_ADS_TEXT_DOMAIN ), 'edit_item' => __( 'Edit Job Ad', AREVOS_JOB_ADS_TEXT_DOMAIN ), 'new_item' => __( 'New Job Ad', AREVOS_JOB_ADS_TEXT_DOMAIN ), 'view_item' => __( 'View Job Ad', AREVOS_JOB_ADS_TEXT_DOMAIN ), 'search_items' => __( 'Search Job Ads', AREVOS_JOB_ADS_TEXT_DOMAIN ), 'not_found' => __( 'No Job Ads found', AREVOS_JOB_ADS_TEXT_DOMAIN ), 'not_found_in_trash' => __( 'No Job Ads found in Trash', AREVOS_JOB_ADS_TEXT_DOMAIN ), 'parent_item_colon' => __( 'Parent Page', AREVOS_JOB_ADS_TEXT_DOMAIN ), ]; $args = [ 'labels' => $labels, 'public' => true, 'exclude_from_search' => false, 'publicly_queryable' => true, 'show_ui' => true, 'show_in_menu' => true, 'has_archive' => true, 'menu_icon' => 'dashicons-format-aside', 'capability_type' => 'post', 'hierarchical' => false, 'rewrite' => [ 'slug' => 'job-ads' ], 'supports' => [ 'title', 'editor', 'thumbnail', 'excerpt' ], 'taxonomies' => [], ]; register_post_type( AREVOS_JOB_ADS_POST_TYPE, $args ); } add_action( 'init', 'arevos_job_ads_register_cpt' ); // --------------------------------------------------------------------------------------------------------------------- // REGISTER POST TYPE (END) // --------------------------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------------------------- // INTERNATIONALIZATION (BEGIN) // --------------------------------------------------------------------------------------------------------------------- function arevos_job_ads_textdomain() { $plugin_rel_path = basename( dirname( __FILE__ ) ) . '/languages/'; load_plugin_textdomain( AREVOS_JOB_ADS_TEXT_DOMAIN, false, $plugin_rel_path ); } add_action( 'plugins_loaded', 'arevos_job_ads_textdomain' ); // --------------------------------------------------------------------------------------------------------------------- // INTERNATIONALIZATION (END) // --------------------------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------------------------- // JOB EDIT FORM (BEGIN) // --------------------------------------------------------------------------------------------------------------------- function arevos_job_ads_render_element( $type, $name, $value, $label ) { $type = esc_attr( $type ); $id = $name; $id = esc_attr( $id ); $name = esc_attr( $name ); if ( empty( $label ) ) { $label = ucfirst(preg_replace('/([a-z])([A-Z])/', '$1 $2', $name)); } $label = esc_html__( $label, AREVOS_JOB_ADS_TEXT_DOMAIN ); echo ""; switch ( $type ) { case 'text': case 'email': $value = esc_attr( $value ); echo "
"; break; case 'textarea': $value = esc_html( $value ); echo ""; break; case 'static': $value = esc_html( $value ); echo "$value
", PHP_EOL; break; case 'html': wp_editor( $value, $id, [] ); break; default: throw new Exception( 'Wrong element type' ); } } function arevos_job_ads_custom_fields_render_metabox( $post ) { global $arevos_job_ads_meta_fields; $meta = get_post_meta( $post->ID ); foreach ( $arevos_job_ads_meta_fields as $name => $specs ) { if ( null === $specs ) { continue; } arevos_job_ads_render_element( $specs['type'], $name, $meta[$name][0], $specs['label'] ); } } function arevos_job_ads_save_post( $post_id ) { global $arevos_job_ads_meta_fields; if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { return $post_id; } foreach ( $arevos_job_ads_meta_fields as $name => $specs ) { if ( null === $specs ) { continue; } update_post_meta( $post_id, $name, @$_POST[$name] ); } } add_action( 'save_post_' . AREVOS_JOB_ADS_POST_TYPE, 'arevos_job_ads_save_post' ); function arevos_job_ads_custom_fields_metabox() { add_meta_box( 'arevos-job-ads-custom-fields', __( 'Job Information', AREVOS_JOB_ADS_TEXT_DOMAIN ), 'arevos_job_ads_custom_fields_render_metabox', AREVOS_JOB_ADS_POST_TYPE, 'advanced' ); } add_action( 'add_meta_boxes', 'arevos_job_ads_custom_fields_metabox' ); // --------------------------------------------------------------------------------------------------------------------- // JOB EDIT FORM (FORM) // --------------------------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------------------------- // IMPORT JOB ADS (BEGIN) // --------------------------------------------------------------------------------------------------------------------- function arevos_job_ads_fetch_feed() { $curl = curl_init( AREVOS_JOB_ADS_FEED_URL ); curl_setopt( $curl, CURLOPT_SSL_VERIFYHOST, 0 ); curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, 0 ); curl_setopt( $curl, CURLOPT_RETURNTRANSFER, 1 ); $xml = curl_exec( $curl ); curl_close( $curl ); return $xml; } function arevos_job_ads_prepare_job( SimpleXMLElement $job ) { $data = (array) $job; /** @var SimpleXMLElement $filed */ foreach ( $data as & $filed ) { if ( $filed instanceof SimpleXMLElement ) { $filed = $filed->__toString(); } if ( is_scalar( $filed ) ) { $filed = trim( $filed ); } } unset( $filed ); return $data; } function arevos_job_ads_get_post_id($external_id ) { global $wpdb; $external_id = strval( $external_id ); $external_id = trim( $external_id ); if ( ! $external_id ) { return 0; } $external_id = esc_sql($external_id); $sql = "select post_id from $wpdb->postmeta where meta_value = '$external_id'"; $result = $wpdb->get_row( $sql, ARRAY_A ); return ($result) ? $result['post_id'] : 0; } function arevos_job_ads_insert_post( array $data ) { global $arevos_job_ads_meta_fields; $post_id = arevos_job_ads_get_post_id( $data['id'] ); if ( ! function_exists( 'is_user_logged_in' ) ) { function is_user_logged_in() { return false; } } if ( ! function_exists( 'wp_get_current_user' ) ) { function wp_get_current_user() { return 1; } } $postarr = [ 'ID' => $post_id, 'post_content' => $data['content'], 'post_title' => $data['name'], 'post_excerpt' => html_entity_decode( $data['description'], ENT_COMPAT | ENT_HTML401 | ENT_QUOTES ), 'post_status' => 'publish', 'post_type' => AREVOS_JOB_ADS_POST_TYPE, ]; $post_id = wp_insert_post( $postarr, true ); if ( $post_id instanceof WP_Error ) { vdie( $post_id->get_error_messages() ); } update_post_meta( $post_id, 'external_id', $data['id'] ); foreach ( array_keys( $arevos_job_ads_meta_fields ) as $field ) { update_post_meta( $post_id, $field, $data[$field] ); } return $post_id; } function arevos_job_ads_update_posts() { $xml = arevos_job_ads_fetch_feed(); if ( ! $xml ) { return false; } /** @var SimpleXMLElement $dom */ $dom = simplexml_load_string( $xml, "SimpleXMLElement", LIBXML_NOCDATA | LIBXML_NOBLANKS ); if ( ! $dom ) { return false; } /** @var SimpleXMLElement $job */ foreach ( $dom as $job ) { $data = arevos_job_ads_prepare_job( $job ); arevos_job_ads_insert_post( $data ); } return true; } // --------------------------------------------------------------------------------------------------------------------- // IMPORT JOB ADS (END) // --------------------------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------------------------- // SETTINGS (BEGIN) // --------------------------------------------------------------------------------------------------------------------- function arevos_job_ads_get_sync_period() { return get_option( 'arevos_job_ads_sync_period', 1 ); } function arevos_job_ads_settings_callback() { echo ''; } function arevos_job_ads_sync_period_callback() { $options = [ 1 => __( 'Every Hour', AREVOS_JOB_ADS_TEXT_DOMAIN ), 3 => __( 'Every 3 Hours', AREVOS_JOB_ADS_TEXT_DOMAIN ), 6 => __( 'Every 6 Hours', AREVOS_JOB_ADS_TEXT_DOMAIN ), 12 => __( '2 Times a Day', AREVOS_JOB_ADS_TEXT_DOMAIN ), 24 => __( 'Once a Day', AREVOS_JOB_ADS_TEXT_DOMAIN ), ]; $value = arevos_job_ads_get_sync_period(); echo ''; } function arevos_job_ads_manual_sync_callback() { $url = admin_url( "admin-post.php?action=arevos_job_ads_sync" ); echo "Synchronize Now"; } function arevos_job_ads_settings() { $section = 'arevos_job_ads_settings_section'; $page = 'general'; add_settings_section( $section, __( 'Arevos Job Ads', AREVOS_JOB_ADS_TEXT_DOMAIN ), 'arevos_job_ads_settings_callback', $page ); add_settings_field( 'arevos_job_ads_sync_period', __( 'Synchronization Period', AREVOS_JOB_ADS_TEXT_DOMAIN ), 'arevos_job_ads_sync_period_callback', $page, $section ); register_setting( $page, 'arevos_job_ads_sync_period' ); add_settings_field( 'arevos_job_ads_manual_sync', __( 'Manual Synchronization', AREVOS_JOB_ADS_TEXT_DOMAIN ), 'arevos_job_ads_manual_sync_callback', $page, $section ); } add_action( 'admin_init', 'arevos_job_ads_settings' ); function arevos_job_ads_sync() { $result = ( arevos_job_ads_update_posts() ) ? 'success' : 'fail'; set_transient( 'arevos_job_ads_sync_result', $result, 45 ); $url = admin_url('edit.php?post_type=' . AREVOS_JOB_ADS_POST_TYPE ); header( "Location: $url" ); exit; } add_action( 'admin_post_nopriv_arevos_job_ads_sync', 'arevos_job_ads_sync' ); add_action( 'admin_post_arevos_job_ads_sync', 'arevos_job_ads_sync' ); function arevos_job_ads_sync_notice() { if ( $result = get_transient( 'arevos_job_ads_sync_result' ) ) { if ( 'success' == $result ) { $message = esc_html__( 'Job Ads successfully updated.', AREVOS_JOB_ADS_TEXT_DOMAIN ); $notice = <<$message
END; } else { $message = esc_html__( 'Job Ads are not updated.', AREVOS_JOB_ADS_TEXT_DOMAIN ); $notice = <<$message
END; } echo $notice; delete_transient('arevos_job_ads_sync_result' ); } } add_action( 'admin_notices', 'arevos_job_ads_sync_notice' ); // --------------------------------------------------------------------------------------------------------------------- // SETTINGS (END) // --------------------------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------------------------- // CRON (BEGIN) // --------------------------------------------------------------------------------------------------------------------- // crontab: // MAILTO=vityk@orange35.com // 0 * * * * wget -q --tries=1 --timeout=60 -O- http://arevos.net/wp-admin/admin-post.php?action=arevos_job_ads_cron function arevos_job_ads_cron() { $interval = intval( arevos_job_ads_get_sync_period() ); $run = false; if ( 1 == $interval ) { $run = true; } else { $curr_hour = intval( date( 'H' ) ); if ( 0 == ( $curr_hour % $interval ) ) { $run = true; } } if ( $run ) { arevos_job_ads_update_posts(); } } add_action( 'admin_post_nopriv_arevos_job_ads_cron', 'arevos_job_ads_cron' ); add_action( 'admin_post_arevos_job_ads_cron', 'arevos_job_ads_cron' ); // --------------------------------------------------------------------------------------------------------------------- // CRON (END) // ---------------------------------------------------------------------------------------------------------------------