wp-job-manager.php
| 1 | <?php |
| 2 | /** |
| 3 | * Plugin Name: WP Job Manager |
| 4 | * Plugin URI: https://wpjobmanager.com/ |
| 5 | * Description: Manage job listings from the WordPress admin panel, and allow users to post jobs directly to your site. |
| 6 | * Version: 1.27.0 |
| 7 | * Author: Automattic |
| 8 | * Author URI: https://wpjobmanager.com/ |
| 9 | * Requires at least: 4.1 |
| 10 | * Tested up to: 4.7 |
| 11 | * Text Domain: wp-job-manager |
| 12 | * Domain Path: /languages/ |
| 13 | * License: GPL2+ |
| 14 | */ |
| 15 | if ( ! defined( 'ABSPATH' ) ) { |
| 16 | exit; |
| 17 | } |
| 18 | |
| 19 | /** |
| 20 | * Handles core plugin hooks and action setup. |
| 21 | * |
| 22 | * @package wp-job-manager |
| 23 | * @since 1.0.0 |
| 24 | */ |
| 25 | class WP_Job_Manager { |
| 26 | /** |
| 27 | * The single instance of the class. |
| 28 | * |
| 29 | * @var self |
| 30 | * @since 1.26.0 |
| 31 | */ |
| 32 | private static $_instance = null; |
| 33 | |
| 34 | /** |
| 35 | * Main WP Job Manager Instance. |
| 36 | * |
| 37 | * Ensures only one instance of WP Job Manager is loaded or can be loaded. |
| 38 | * |
| 39 | * @since 1.26.0 |
| 40 | * @static |
| 41 | * @see WPJM() |
| 42 | * @return self Main instance. |
| 43 | */ |
| 44 | public static function instance() { |
| 45 | if ( is_null( self::$_instance ) ) { |
| 46 | self::$_instance = new self(); |
| 47 | } |
| 48 | return self::$_instance; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Constructor. |
| 53 | */ |
| 54 | public function __construct() { |
| 55 | // Define constants |
| 56 | define( 'JOB_MANAGER_VERSION', '1.27.0' ); |
| 57 | define( 'JOB_MANAGER_PLUGIN_DIR', untrailingslashit( plugin_dir_path( __FILE__ ) ) ); |
| 58 | define( 'JOB_MANAGER_PLUGIN_URL', untrailingslashit( plugins_url( basename( plugin_dir_path( __FILE__ ) ), basename( __FILE__ ) ) ) ); |
| 59 | |
| 60 | // Includes |
| 61 | include_once( 'wp-job-manager-functions.php' ); |
| 62 | include_once( 'wp-job-manager-deprecated.php' ); |
| 63 | include_once( 'includes/class-wp-job-manager-install.php' ); |
| 64 | include_once( 'includes/class-wp-job-manager-post-types.php' ); |
| 65 | include_once( 'includes/class-wp-job-manager-ajax.php' ); |
| 66 | include_once( 'includes/class-wp-job-manager-shortcodes.php' ); |
| 67 | include_once( 'includes/class-wp-job-manager-api.php' ); |
| 68 | include_once( 'includes/class-wp-job-manager-forms.php' ); |
| 69 | include_once( 'includes/class-wp-job-manager-geocode.php' ); |
| 70 | include_once( 'includes/class-wp-job-manager-cache-helper.php' ); |
| 71 | |
| 72 | if ( is_admin() ) { |
| 73 | include_once( 'includes/admin/class-wp-job-manager-admin.php' ); |
| 74 | } |
| 75 | |
| 76 | // Load 3rd party customizations |
| 77 | include_once( 'includes/3rd-party/3rd-party.php' ); |
| 78 | |
| 79 | // Init classes |
| 80 | $this->forms = WP_Job_Manager_Forms::instance(); |
| 81 | $this->post_types = WP_Job_Manager_Post_Types::instance(); |
| 82 | |
| 83 | // Schedule cron jobs |
| 84 | self::maybe_schedule_cron_jobs(); |
| 85 | |
| 86 | // Activation - works with symlinks |
| 87 | register_activation_hook( basename( dirname( __FILE__ ) ) . '/' . basename( __FILE__ ), array( $this, 'activate' ) ); |
| 88 | |
| 89 | // Switch theme |
| 90 | add_action( 'after_switch_theme', array( 'WP_Job_Manager_Ajax', 'add_endpoint' ), 10 ); |
| 91 | add_action( 'after_switch_theme', array( $this->post_types, 'register_post_types' ), 11 ); |
| 92 | add_action( 'after_switch_theme', 'flush_rewrite_rules', 15 ); |
| 93 | |
| 94 | // Actions |
| 95 | add_action( 'after_setup_theme', array( $this, 'load_plugin_textdomain' ) ); |
| 96 | add_action( 'after_setup_theme', array( $this, 'include_template_functions' ), 11 ); |
| 97 | add_action( 'widgets_init', array( $this, 'widgets_init' ) ); |
| 98 | add_action( 'wp_enqueue_scripts', array( $this, 'frontend_scripts' ) ); |
| 99 | add_action( 'admin_init', array( $this, 'updater' ) ); |
| 100 | add_action( 'wp_logout', array( $this, 'cleanup_job_posting_cookies' ) ); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Performs plugin activation steps. |
| 105 | */ |
| 106 | public function activate() { |
| 107 | WP_Job_Manager_Ajax::add_endpoint(); |
| 108 | $this->post_types->register_post_types(); |
| 109 | WP_Job_Manager_Install::install(); |
| 110 | flush_rewrite_rules(); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Handles tasks after plugin is updated. |
| 115 | */ |
| 116 | public function updater() { |
| 117 | if ( version_compare( JOB_MANAGER_VERSION, get_option( 'wp_job_manager_version' ), '>' ) ) { |
| 118 | WP_Job_Manager_Install::install(); |
| 119 | flush_rewrite_rules(); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Loads textdomain for plugin. |
| 125 | */ |
| 126 | public function load_plugin_textdomain() { |
| 127 | load_textdomain( 'wp-job-manager', WP_LANG_DIR . '/wp-job-manager/wp-job-manager-' . apply_filters( 'plugin_locale', get_locale(), 'wp-job-manager' ) . '.mo' ); |
| 128 | load_plugin_textdomain( 'wp-job-manager', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' ); |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Loads plugin's core helper template functions. |
| 133 | */ |
| 134 | public function include_template_functions() { |
| 135 | include_once( 'wp-job-manager-template.php' ); |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Loads plugin's widgets. |
| 140 | */ |
| 141 | public function widgets_init() { |
| 142 | include_once( 'includes/class-wp-job-manager-widget.php' ); |
| 143 | include_once( 'includes/widgets/class-wp-job-manager-widget-recent-jobs.php' ); |
| 144 | include_once( 'includes/widgets/class-wp-job-manager-widget-featured-jobs.php' ); |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Schedule cron jobs for WPJM events. |
| 149 | */ |
| 150 | public static function maybe_schedule_cron_jobs() { |
| 151 | if ( ! wp_next_scheduled( 'job_manager_check_for_expired_jobs' ) ) { |
| 152 | wp_schedule_event( time(), 'hourly', 'job_manager_check_for_expired_jobs' ); |
| 153 | } |
| 154 | if ( ! wp_next_scheduled( 'job_manager_delete_old_previews' ) ) { |
| 155 | wp_schedule_event( time(), 'daily', 'job_manager_delete_old_previews' ); |
| 156 | } |
| 157 | if ( ! wp_next_scheduled( 'job_manager_clear_expired_transients' ) ) { |
| 158 | wp_schedule_event( time(), 'twicedaily', 'job_manager_clear_expired_transients' ); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Cleanup job posting cookies. |
| 164 | */ |
| 165 | public function cleanup_job_posting_cookies() { |
| 166 | if ( isset( $_COOKIE['wp-job-manager-submitting-job-id'] ) ) { |
| 167 | setcookie( 'wp-job-manager-submitting-job-id', '', 0, COOKIEPATH, COOKIE_DOMAIN, false ); |
| 168 | } |
| 169 | if ( isset( $_COOKIE['wp-job-manager-submitting-job-key'] ) ) { |
| 170 | setcookie( 'wp-job-manager-submitting-job-key', '', 0, COOKIEPATH, COOKIE_DOMAIN, false ); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Registers and enqueues scripts and CSS. |
| 176 | */ |
| 177 | public function frontend_scripts() { |
| 178 | global $post; |
| 179 | |
| 180 | $ajax_url = WP_Job_Manager_Ajax::get_endpoint(); |
| 181 | $ajax_filter_deps = array( 'jquery', 'jquery-deserialize' ); |
| 182 | $ajax_data = array( |
| 183 | 'ajax_url' => $ajax_url, |
| 184 | 'is_rtl' => is_rtl() ? 1 : 0, |
| 185 | 'i18n_load_prev_listings' => __( 'Load previous listings', 'wp-job-manager' ), |
| 186 | ); |
| 187 | |
| 188 | /** |
| 189 | * Retrieves the current language for use when caching requests. |
| 190 | * |
| 191 | * @since 1.26.0 |
| 192 | * |
| 193 | * @param string|null $lang |
| 194 | */ |
| 195 | $ajax_data['lang'] = apply_filters( 'wpjm_lang', null ); |
| 196 | |
| 197 | if ( apply_filters( 'job_manager_chosen_enabled', true ) ) { |
| 198 | wp_register_script( 'chosen', JOB_MANAGER_PLUGIN_URL . '/assets/js/jquery-chosen/chosen.jquery.min.js', array( 'jquery' ), '1.1.0', true ); |
| 199 | wp_register_script( 'wp-job-manager-term-multiselect', JOB_MANAGER_PLUGIN_URL . '/assets/js/term-multiselect.min.js', array( 'jquery', 'chosen' ), JOB_MANAGER_VERSION, true ); |
| 200 | wp_register_script( 'wp-job-manager-multiselect', JOB_MANAGER_PLUGIN_URL . '/assets/js/multiselect.min.js', array( 'jquery', 'chosen' ), JOB_MANAGER_VERSION, true ); |
| 201 | wp_enqueue_style( 'chosen', JOB_MANAGER_PLUGIN_URL . '/assets/css/chosen.css', array(), '1.1.0' ); |
| 202 | $ajax_filter_deps[] = 'chosen'; |
| 203 | |
| 204 | wp_localize_script( 'chosen', 'job_manager_chosen_multiselect_args', |
| 205 | apply_filters( 'job_manager_chosen_multiselect_args', array( |
| 206 | 'search_contains' => true, |
| 207 | ) ) |
| 208 | ); |
| 209 | } |
| 210 | |
| 211 | if ( apply_filters( 'job_manager_ajax_file_upload_enabled', true ) ) { |
| 212 | wp_register_script( 'jquery-iframe-transport', JOB_MANAGER_PLUGIN_URL . '/assets/js/jquery-fileupload/jquery.iframe-transport.js', array( 'jquery' ), '1.8.3', true ); |
| 213 | wp_register_script( 'jquery-fileupload', JOB_MANAGER_PLUGIN_URL . '/assets/js/jquery-fileupload/jquery.fileupload.js', array( 'jquery', 'jquery-iframe-transport', 'jquery-ui-widget' ), '9.11.2', true ); |
| 214 | wp_register_script( 'wp-job-manager-ajax-file-upload', JOB_MANAGER_PLUGIN_URL . '/assets/js/ajax-file-upload.min.js', array( 'jquery', 'jquery-fileupload' ), JOB_MANAGER_VERSION, true ); |
| 215 | |
| 216 | ob_start(); |
| 217 | get_job_manager_template( 'form-fields/uploaded-file-html.php', array( |
| 218 | 'name' => '', |
| 219 | 'value' => '', |
| 220 | 'extension' => 'jpg', |
| 221 | ) ); |
| 222 | $js_field_html_img = ob_get_clean(); |
| 223 | |
| 224 | ob_start(); |
| 225 | get_job_manager_template( 'form-fields/uploaded-file-html.php', array( |
| 226 | 'name' => '', |
| 227 | 'value' => '', |
| 228 | 'extension' => 'zip', |
| 229 | ) ); |
| 230 | $js_field_html = ob_get_clean(); |
| 231 | |
| 232 | wp_localize_script( 'wp-job-manager-ajax-file-upload', 'job_manager_ajax_file_upload', array( |
| 233 | 'ajax_url' => $ajax_url, |
| 234 | 'js_field_html_img' => esc_js( str_replace( "\n", '', $js_field_html_img ) ), |
| 235 | 'js_field_html' => esc_js( str_replace( "\n", '', $js_field_html ) ), |
| 236 | 'i18n_invalid_file_type' => __( 'Invalid file type. Accepted types:', 'wp-job-manager' ), |
| 237 | ) ); |
| 238 | } |
| 239 | |
| 240 | wp_register_script( 'jquery-deserialize', JOB_MANAGER_PLUGIN_URL . '/assets/js/jquery-deserialize/jquery.deserialize.js', array( 'jquery' ), '1.2.1', true ); |
| 241 | wp_register_script( 'wp-job-manager-ajax-filters', JOB_MANAGER_PLUGIN_URL . '/assets/js/ajax-filters.min.js', $ajax_filter_deps, JOB_MANAGER_VERSION, true ); |
| 242 | wp_register_script( 'wp-job-manager-job-dashboard', JOB_MANAGER_PLUGIN_URL . '/assets/js/job-dashboard.min.js', array( 'jquery' ), JOB_MANAGER_VERSION, true ); |
| 243 | wp_register_script( 'wp-job-manager-job-application', JOB_MANAGER_PLUGIN_URL . '/assets/js/job-application.min.js', array( 'jquery' ), JOB_MANAGER_VERSION, true ); |
| 244 | wp_register_script( 'wp-job-manager-job-submission', JOB_MANAGER_PLUGIN_URL . '/assets/js/job-submission.min.js', array( 'jquery' ), JOB_MANAGER_VERSION, true ); |
| 245 | wp_localize_script( 'wp-job-manager-ajax-filters', 'job_manager_ajax_filters', $ajax_data ); |
| 246 | wp_localize_script( 'wp-job-manager-job-dashboard', 'job_manager_job_dashboard', array( |
| 247 | 'i18n_confirm_delete' => __( 'Are you sure you want to delete this listing?', 'wp-job-manager' ), |
| 248 | ) ); |
| 249 | |
| 250 | wp_enqueue_style( 'wp-job-manager-frontend', JOB_MANAGER_PLUGIN_URL . '/assets/css/frontend.css', array(), JOB_MANAGER_VERSION ); |
| 251 | if ( is_a( $post, 'WP_Post' ) && has_shortcode( $post->post_content, 'submit_job_form' ) ) { |
| 252 | wp_enqueue_style( 'wp-job-manager-job-submission', JOB_MANAGER_PLUGIN_URL . '/assets/css/job-submission.css', array(), JOB_MANAGER_VERSION ); |
| 253 | } |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * Add post type for Job Manager. |
| 259 | * |
| 260 | * @param array $types |
| 261 | * @return array |
| 262 | */ |
| 263 | function job_manager_add_post_types( $types ) { |
| 264 | $types[] = 'job_listing'; |
| 265 | return $types; |
| 266 | } |
| 267 | add_filter( 'post_types_to_delete_with_user', 'job_manager_add_post_types', 10 ); |
| 268 | |
| 269 | /** |
| 270 | * Main instance of WP Job Manager. |
| 271 | * |
| 272 | * Returns the main instance of WP Job Manager to prevent the need to use globals. |
| 273 | * |
| 274 | * @since 1.26 |
| 275 | * @return WP_Job_Manager |
| 276 | */ |
| 277 | function WPJM() { |
| 278 | return WP_Job_Manager::instance(); |
| 279 | } |
| 280 | |
| 281 | $GLOBALS['job_manager'] = WPJM(); |
| 282 |