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