| 1 |
<?php |
| 2 |
/* |
| 3 |
* License: GPLv3 |
| 4 |
* License URI: https://www.gnu.org/licenses/gpl.txt |
| 5 |
* Copyright 2017-2026 Jean-Sebastien Morisset (https://wpsso.com/) |
| 6 |
*/ |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
|
| 10 |
die( 'These aren\'t the droids you\'re looking for.' ); |
| 11 |
} |
| 12 |
|
| 13 |
if ( ! class_exists( 'WpssoIntegJobWpJobManager' ) ) { |
| 14 |
|
| 15 |
class WpssoIntegJobWpJobManager { |
| 16 |
|
| 17 |
private $p; // Wpsso class object. |
| 18 |
|
| 19 |
public function __construct( &$plugin ) { |
| 20 |
|
| 21 |
$this->p =& $plugin; |
| 22 |
|
| 23 |
if ( $this->p->debug->enabled ) { |
| 24 |
|
| 25 |
$this->p->debug->mark(); |
| 26 |
} |
| 27 |
|
| 28 |
$this->p->util->add_plugin_filters( $this, array( |
| 29 |
'form_select_schema_job_hiring_org_id' => 1, |
| 30 |
'form_select_schema_job_location_id' => 1, |
| 31 |
'get_md_defaults' => 2, |
| 32 |
'get_post_options' => 3, |
| 33 |
'get_job_options' => 3, |
| 34 |
'get_organization_options' => 3, |
| 35 |
'get_place_options' => 3, |
| 36 |
), $prio = -1000 ); |
| 37 |
|
| 38 |
if ( ! empty( $this->p->avail[ 'p' ][ 'schema' ] ) ) { |
| 39 |
|
| 40 |
add_filter( 'wpjm_get_job_listing_structured_data', '__return_empty_array', PHP_INT_MAX ); |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
public function filter_form_select_schema_job_hiring_org_id( $values ) { |
| 45 |
|
| 46 |
if ( $this->p->debug->enabled ) { |
| 47 |
|
| 48 |
$this->p->debug->mark(); |
| 49 |
} |
| 50 |
|
| 51 |
if ( $post_obj = SucomUtilWP::get_post_object( $use_post = false, $output = 'object' ) ) { |
| 52 |
|
| 53 |
if ( $org_name = get_the_company_name( $post_obj ) ) { |
| 54 |
|
| 55 |
$values[ 'wpjm_org-' . $post_obj->ID ] = sprintf( _x( 'WPJM: %s', 'option value', 'wpsso' ), $org_name ); |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
return $values; |
| 60 |
} |
| 61 |
|
| 62 |
public function filter_form_select_schema_job_location_id( $values ) { |
| 63 |
|
| 64 |
if ( $this->p->debug->enabled ) { |
| 65 |
|
| 66 |
$this->p->debug->mark(); |
| 67 |
} |
| 68 |
|
| 69 |
if ( $post_obj = SucomUtilWP::get_post_object( $use_post = false, $output = 'object' ) ) { |
| 70 |
|
| 71 |
if ( $place_name = get_the_job_location( $post_obj ) ) { |
| 72 |
|
| 73 |
$values[ 'wpjm_place-' . $post_obj->ID ] = sprintf( _x( 'WPJM: %s', 'option value', 'wpsso' ), $place_name ); |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
return $values; |
| 78 |
} |
| 79 |
|
| 80 |
public function filter_get_md_defaults( array $md_defs, array $mod ) { |
| 81 |
|
| 82 |
if ( $this->p->debug->enabled ) { |
| 83 |
|
| 84 |
$this->p->debug->mark(); |
| 85 |
} |
| 86 |
|
| 87 |
if ( ! $mod[ 'is_post' ] || $mod[ 'post_type' ] !== 'job_listing' ) { |
| 88 |
|
| 89 |
return $md_defs; |
| 90 |
} |
| 91 |
|
| 92 |
$post_obj = SucomUtilWP::get_post_object( $mod[ 'id' ], $output = 'object' ); |
| 93 |
|
| 94 |
if ( $org_name = get_the_company_name( $post_obj ) ) { |
| 95 |
|
| 96 |
$md_defs[ 'schema_job_hiring_org_id' ] = 'wpjm_org-' . $post_obj->ID; |
| 97 |
} |
| 98 |
|
| 99 |
if ( $place_name = get_the_job_location( $post_obj ) ) { |
| 100 |
|
| 101 |
$md_defs[ 'schema_job_location_id' ] = 'wpjm_place-' . $post_obj->ID; |
| 102 |
} |
| 103 |
|
| 104 |
self::add_schema_job_defaults( $md_defs, $mod[ 'id' ] ); |
| 105 |
|
| 106 |
return $md_defs; |
| 107 |
} |
| 108 |
|
| 109 |
public function filter_get_post_options( array $md_opts, $post_id, array $mod ) { |
| 110 |
|
| 111 |
if ( $this->p->debug->enabled ) { |
| 112 |
|
| 113 |
$this->p->debug->mark(); |
| 114 |
} |
| 115 |
|
| 116 |
if ( $mod[ 'post_type' ] !== 'job_listing' ) { |
| 117 |
|
| 118 |
return $md_opts; |
| 119 |
} |
| 120 |
|
| 121 |
$job_opts = array(); |
| 122 |
|
| 123 |
self::add_schema_job_defaults( $job_opts, $post_id ); // Add defaults to empty array. |
| 124 |
|
| 125 |
foreach ( $job_opts as $key => $val ) { // Hard-code defaults and disable the option. |
| 126 |
|
| 127 |
$md_opts[ $key ] = $val; |
| 128 |
$md_opts[ $key . ':disabled' ] = true; |
| 129 |
} |
| 130 |
|
| 131 |
return $md_opts; |
| 132 |
} |
| 133 |
|
| 134 |
public function filter_get_job_options( $job_opts, $mod, $job_id ) { |
| 135 |
|
| 136 |
if ( $this->p->debug->enabled ) { |
| 137 |
|
| 138 |
$this->p->debug->mark(); |
| 139 |
} |
| 140 |
|
| 141 |
if ( $mod[ 'post_type' ] !== 'job_listing' ) { |
| 142 |
|
| 143 |
return $job_opts; |
| 144 |
} |
| 145 |
|
| 146 |
$job_opts = self::get_schema_job_options( $mod[ 'id' ] ); |
| 147 |
|
| 148 |
$job_opts = SucomUtil::preg_grep_keys( '/^schema_(job_.*)$/', $job_opts, $invert = false, $replace = '$1' ); |
| 149 |
|
| 150 |
return $job_opts; |
| 151 |
} |
| 152 |
|
| 153 |
public function filter_get_organization_options( $org_opts, $mod, $org_id ) { |
| 154 |
|
| 155 |
if ( $this->p->debug->enabled ) { |
| 156 |
|
| 157 |
$this->p->debug->mark(); |
| 158 |
} |
| 159 |
|
| 160 |
if ( false !== $org_opts ) { // First come, first served. |
| 161 |
|
| 162 |
if ( $this->p->debug->enabled ) { |
| 163 |
|
| 164 |
$this->p->debug->log( 'exiting early: org_opts array already defined' ); |
| 165 |
} |
| 166 |
|
| 167 |
return $org_opts; |
| 168 |
} |
| 169 |
|
| 170 |
if ( 0 === strpos( $org_id, 'wpjm_org-' ) ) { |
| 171 |
|
| 172 |
return self::get_organization_id( $org_id, $mod ); // Returns localized values. |
| 173 |
} |
| 174 |
|
| 175 |
return $org_opts; |
| 176 |
} |
| 177 |
|
| 178 |
public function filter_get_place_options( $place_opts, $mod, $place_id ) { |
| 179 |
|
| 180 |
if ( $this->p->debug->enabled ) { |
| 181 |
|
| 182 |
$this->p->debug->mark(); |
| 183 |
} |
| 184 |
|
| 185 |
if ( false !== $place_opts ) { // First come, first served. |
| 186 |
|
| 187 |
if ( $this->p->debug->enabled ) { |
| 188 |
|
| 189 |
$this->p->debug->log( 'exiting early: place_opts array already defined' ); |
| 190 |
} |
| 191 |
|
| 192 |
return $place_opts; |
| 193 |
} |
| 194 |
|
| 195 |
if ( 0 === strpos( $place_id, 'wpjm_place-' ) ) { |
| 196 |
|
| 197 |
return self::get_place_id( $place_id, $mod ); // Returns localized values. |
| 198 |
} |
| 199 |
|
| 200 |
return $place_opts; |
| 201 |
} |
| 202 |
|
| 203 |
private static function get_schema_job_options( $post_id ) { |
| 204 |
|
| 205 |
$wpsso =& Wpsso::get_instance(); |
| 206 |
|
| 207 |
if ( $wpsso->debug->enabled ) { |
| 208 |
|
| 209 |
$wpsso->debug->mark(); |
| 210 |
} |
| 211 |
|
| 212 |
$post_obj = get_post( $post_id ); |
| 213 |
|
| 214 |
$job_opts = array(); |
| 215 |
|
| 216 |
self::add_schema_job_defaults( $job_opts, $post_id ); |
| 217 |
|
| 218 |
$job_opts[ 'schema_job_title' ] = wpjm_get_the_job_title( $post_obj ); |
| 219 |
$job_opts[ 'schema_job_expire_time' ] = '00:00'; |
| 220 |
$job_opts[ 'schema_job_expire_timezone' ] = get_option( 'timezone_string' ); |
| 221 |
$job_opts[ 'schema_job_expire_iso' ] = date_format( date_create( |
| 222 |
$job_opts[ 'schema_job_expire_date' ] . ' ' . |
| 223 |
$job_opts[ 'schema_job_expire_time' ] . ' ' . |
| 224 |
$job_opts[ 'schema_job_expire_timezone' ] |
| 225 |
), 'c' ); |
| 226 |
|
| 227 |
return $job_opts; |
| 228 |
} |
| 229 |
|
| 230 |
private static function add_schema_job_defaults( array &$job_opts, $post_id ) { |
| 231 |
|
| 232 |
$wpsso =& Wpsso::get_instance(); |
| 233 |
|
| 234 |
if ( $wpsso->debug->enabled ) { |
| 235 |
|
| 236 |
$wpsso->debug->mark(); |
| 237 |
} |
| 238 |
|
| 239 |
static $local_cache = array(); |
| 240 |
|
| 241 |
if ( ! isset( $local_cache[ $post_id ] ) ) { // Only create the defaults array once. |
| 242 |
|
| 243 |
if ( $wpsso->debug->enabled ) { |
| 244 |
|
| 245 |
$wpsso->debug->log( 'creating a new defaults static array' ); |
| 246 |
} |
| 247 |
|
| 248 |
$local_cache[ $post_id ] = array(); |
| 249 |
|
| 250 |
/* |
| 251 |
* Get the default schema type (job.posting by default). |
| 252 |
*/ |
| 253 |
$local_cache[ $post_id ][ 'schema_type' ] = $wpsso->options[ 'schema_type_for_job_listing' ]; |
| 254 |
|
| 255 |
$post_obj = get_post( $post_id ); |
| 256 |
|
| 257 |
/* |
| 258 |
* Add post meta. |
| 259 |
*/ |
| 260 |
foreach ( array( |
| 261 |
'schema_job_expire_date' => '_job_expires', |
| 262 |
) as $md_key => $meta_key ) { |
| 263 |
|
| 264 |
$local_cache[ $post_id ][ $md_key ] = get_metadata( 'post', $post_id, $meta_key, $single = true ); |
| 265 |
} |
| 266 |
|
| 267 |
$job_types = wpjm_get_job_employment_types( $post_obj ); |
| 268 |
|
| 269 |
if ( ! empty( $job_types ) ) { |
| 270 |
|
| 271 |
foreach ( $job_types as $empl_type ) { |
| 272 |
|
| 273 |
if ( ! empty( $empl_type ) ) { // Just in case. |
| 274 |
|
| 275 |
/* |
| 276 |
* Google approved values (case sensitive): |
| 277 |
* |
| 278 |
* FULL_TIME |
| 279 |
* PART_TIME |
| 280 |
* CONTRACTOR |
| 281 |
* TEMPORARY |
| 282 |
* INTERN |
| 283 |
* VOLUNTEER |
| 284 |
* PER_DIEM |
| 285 |
* OTHER |
| 286 |
*/ |
| 287 |
$empl_type = SucomUtil::sanitize_hookname( $empl_type ); // Sanitize with underscores. |
| 288 |
|
| 289 |
$empl_type = strtoupper( $empl_type ); |
| 290 |
|
| 291 |
$local_cache[ $post_id ][ 'schema_job_empl_type_' . $empl_type] = 1; // Checkbox value is 0 or 1. |
| 292 |
} |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
if ( $wpsso->debug->enabled ) { |
| 297 |
|
| 298 |
$wpsso->debug->log_arr( 'defaults static array', $local_cache[ $post_id ] ); |
| 299 |
} |
| 300 |
|
| 301 |
} elseif ( $wpsso->debug->enabled ) { |
| 302 |
|
| 303 |
$wpsso->debug->log( 'using the cached defaults static array' ); |
| 304 |
} |
| 305 |
|
| 306 |
if ( ! empty( $local_cache[ $post_id ] ) ) { |
| 307 |
|
| 308 |
$job_opts = array_merge( $job_opts, $local_cache[ $post_id ] ); |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
private static function get_organization_id( $org_id, $mod ) { |
| 313 |
|
| 314 |
$wpsso =& Wpsso::get_instance(); |
| 315 |
|
| 316 |
if ( $wpsso->debug->enabled ) { |
| 317 |
|
| 318 |
$wpsso->debug->mark(); |
| 319 |
} |
| 320 |
|
| 321 |
$count = null; |
| 322 |
|
| 323 |
$post_id = str_replace( 'wpjm_org-', '', $org_id, $count ); |
| 324 |
|
| 325 |
if ( ! $count || ! is_numeric( $post_id ) ) { |
| 326 |
|
| 327 |
return false; |
| 328 |
} |
| 329 |
|
| 330 |
$post_obj = get_post( $post_id ); |
| 331 |
|
| 332 |
$org_opts = array( |
| 333 |
'org_url' => get_the_company_website( $post_obj ), |
| 334 |
'org_name' => get_the_company_name( $post_obj ), |
| 335 |
'org_desc' => get_the_company_tagline( $post_obj ), |
| 336 |
'org_logo_url' => get_the_company_logo( $post_obj, $size = 'full' ), |
| 337 |
'org_sameas' => array(), |
| 338 |
); |
| 339 |
|
| 340 |
$twitter_name = SucomUtil::sanitize_twitter_name( get_the_company_twitter( $post_obj ), $add_at = false ); |
| 341 |
|
| 342 |
if ( ! empty( $twitter_name ) ) { |
| 343 |
|
| 344 |
$org_opts[ 'org_sameas' ][] = 'https://twitter.com/' . $twitter_name; |
| 345 |
} |
| 346 |
|
| 347 |
if ( empty( $org_opts ) ) { |
| 348 |
|
| 349 |
return false; |
| 350 |
|
| 351 |
} |
| 352 |
|
| 353 |
return $org_opts; |
| 354 |
} |
| 355 |
|
| 356 |
private static function get_place_id( $place_id, $mod ) { |
| 357 |
|
| 358 |
$wpsso =& Wpsso::get_instance(); |
| 359 |
|
| 360 |
if ( $wpsso->debug->enabled ) { |
| 361 |
|
| 362 |
$wpsso->debug->mark(); |
| 363 |
} |
| 364 |
|
| 365 |
$count = null; |
| 366 |
|
| 367 |
$post_id = str_replace( 'wpjm_place-', '', $place_id, $count ); |
| 368 |
|
| 369 |
if ( ! $count || ! is_numeric( $post_id ) ) { |
| 370 |
|
| 371 |
return false; |
| 372 |
} |
| 373 |
|
| 374 |
$post_obj = get_post( $post_id ); |
| 375 |
|
| 376 |
$place_opts = array( |
| 377 |
'place_name' => get_the_job_location( $post_obj ), |
| 378 |
); |
| 379 |
|
| 380 |
/* |
| 381 |
* Possible place option keys: |
| 382 |
* |
| 383 |
* 'place_url' |
| 384 |
* 'place_name' |
| 385 |
* 'place_name_alt' |
| 386 |
* 'place_desc' |
| 387 |
* 'place_phone' |
| 388 |
* 'place_currencies_accepted' |
| 389 |
* 'place_payment_accepted' |
| 390 |
* 'place_price_range' |
| 391 |
* 'place_street_address' |
| 392 |
* 'place_po_box_number' |
| 393 |
* 'place_city' |
| 394 |
* 'place_region' |
| 395 |
* 'place_postal_code' |
| 396 |
* 'place_country' |
| 397 |
* 'place_altitude' |
| 398 |
* 'place_latitude' |
| 399 |
* 'place_longitude' |
| 400 |
*/ |
| 401 |
foreach ( array( |
| 402 |
'place_city' => 'geolocation_city', |
| 403 |
'place_region' => 'geolocation_state_short', |
| 404 |
'place_country' => 'geolocation_country_short', // Alpha2 country code. |
| 405 |
'place_latitude' => 'geolocation_lat', |
| 406 |
'place_longitude' => 'geolocation_long', |
| 407 |
) as $md_key => $meta_key ) { |
| 408 |
|
| 409 |
$place_opts[ $md_key ] = get_metadata( 'post', $post_id, $meta_key, $single = true ); |
| 410 |
} |
| 411 |
|
| 412 |
if ( empty( $place_opts ) ) { |
| 413 |
|
| 414 |
return false; |
| 415 |
|
| 416 |
} |
| 417 |
|
| 418 |
return $place_opts; |
| 419 |
} |
| 420 |
} |
| 421 |
} |
| 422 |
|