return $route['params'][':' . $param]; } return null; } /** * Check if old vacancy url is used and redirect to new vacancy url * * @param string $currentUrl * @return void */ private static function checkOldVacancyRedirect(string $currentUrl): void { preg_match('/(\d+)-(\d+)(\d+).html/', $currentUrl, $oldVacancyMatch); // Check if the current url is an old vacancy url if (!empty($oldVacancyMatch) && count($oldVacancyMatch) === 4) { // Get the vacancy by the internal id $vacancies = VacanciesListModel::get([], [], [ 'condition' => [ 'type' => 'AND', 'items' => [ [ 'type' => 'COND', 'field' => 'internalId', 'op' => 'EQ', 'param' => $oldVacancyMatch[1] ], ] ], 'search' => [ 'ACTONOMY' => [ 'OPTIONS' => [ 'limit' => 100000 ] ] ], 'what' => [ 'internalId' => 1 ] ]); // Check if the vacancy exists if ( !is_wp_error($vacancies) && !empty($vacancies) && isset($vacancies['listOutput']) && !empty($vacancies['listOutput']) ) { $vacancy = $vacancies['listOutput'][0]; $language = OtysApi::getLanguageByCodeByOtysLanguageId((int) $oldVacancyMatch[2]); $customSlugIsActive = static::customSlugIsActive(); // Get website id $website = static::getSiteId(); // Create new vacancy url $newDetailUrl = Routes::get('vacancy-detail', [ 'slug' => $customSlugIsActive ? $vacancy['slug'][$website] : sanitize_title($vacancy['title']) . '-' . $vacancy['uid'] ], $language); // Redirect to new vacancy url if (wp_redirect($newDetailUrl, 301)) { exit; } } } } /** * Check route against current REQUEST * If route matches the current request return true if * route does not match the curren request return false. * * @since 1.0.0 * @param array $route * @return boolean */ public static function checkRoute(array $route): bool { $currentMethod = is_string($_SERVER['REQUEST_METHOD']) ? preg_replace("/[^a-zA-Z]/", "", $_SERVER['REQUEST_METHOD']) : ''; $currentUrl = parse_url(filter_var($_SERVER['REQUEST_URI'], FILTER_SANITIZE_URL), PHP_URL_PATH); $currentUrl = (substr($currentUrl, -1) === '/') ? $currentUrl : $currentUrl . '/'; preg_match('#' . $route['rewrite_rule'] . '#', $currentUrl, $matches); if (in_array($currentMethod, $route['method']) && preg_match('#' . $route['rewrite_rule'] . '#', $currentUrl)) { return true; } return false; } /** * Throw error page. Loads in 404 page * * @since 1.0.0 * @param string $errorCode * @param string $template * @param array $args * @param boolean $die * @return void */ public static function throwError($errorCode = '404', $template = '404', $args = [], bool $die = true): void { // Set status header with set status code status_header($errorCode); nocache_headers(); $errors = Errors::get(); $templatePath = false; if (($path = self::locateTemplate($template, true)) !== false) { $templatePath = $path; } else { $templatePath = get_query_template('404'); } if ($templatePath !== '') { load_template($templatePath, true, array_merge([ 'errors' => $errors ], $args)); } // Now stop the rest of the script execution if ($die != false) { die(); } } /** * locateTemplate * Locate template file in theme directories * * @since 1.0.0 * @param string $template * @return string */ public static function locateTemplate(string $template = '', $checkRoot = false): string { if ($template === '') { return ''; } $extension = pathinfo($template, PATHINFO_EXTENSION); // Check if the template has .php at the end if not add .php $template = ($extension === 'php') ? $template : $template . '.php'; if ($file_path = locate_template('otys-jobs-apply/' . $template)) { return $file_path; } elseif ($checkRoot && $file_path = locate_template($template)) { return $file_path; } elseif (is_file(OTYS_PLUGIN_TEMPLATE_URL . '/' . $template)) { return OTYS_PLUGIN_TEMPLATE_URL . '/' . $template; } return ''; } /** * Check if custom slug is active * * @return boolean */ public static function customSlugIsActive(): bool { // Get website option $website = OtysApi::getSiteId(); // Setting a website is required if ($website === 0) { return false; } $checkResponse = OtysApi::post([ 'method' => 'check' ], true, false); if (is_wp_error($checkResponse)) { return false; } $response = OtysApi::post([ 'method' => 'Otys.Services.CsmService.getValue', 'params' => [ ['SE3452'], $checkResponse['clientId'] ] ], true, true); if ( !is_wp_error($response) && isset($response['SE3452']) && $response['SE3452']['value'] == 1 ) { return true; } return false; } /** * Set OTYS url format * * This method communicates with the OTYS API to set the url format. * This is used to let OTYS know how the urls are formatted in the WordPress website. * * @return void */ public static function setOtysUrlFormat(): void { // Do not communicate urls to OTYS if the website is not in production mode if (get_option('otys_option_is_production_website') === false) { return; } // Check if custom slug is active this is required $slugsActive = static::customSlugIsActive(); if (!$slugsActive) { return; } $clientCode = OtysApi::getClientId(); $website = OtysApi::getSiteId(); $languages = OtysApi::getLanguages(); $domain = home_url(); $apiUser = OtysApi::getApiUser(); $defaultLanguage = $apiUser['defaultContentLanguage'] ?? 'en'; // Loop through all OTYS Cms languages and set the url format foreach ($languages as $localeCode => $language) { $locale = $localeCode; // Get vacancy detail format $vacancyDetailFormat = Routes::get('vacancy-detail', [ 'slug' => '{{slug}}' ], $locale); // If the first url failed in this language change the locale to the default language $locale = $vacancyDetailFormat !== '' ? $locale : $defaultLanguage; // If vacancy detail format is empty for this language get detail url format for default language $vacancyDetailFormat = $vacancyDetailFormat !== '' ? $vacancyDetailFormat : Routes::get('vacancy-detail', [ 'slug' => '{{slug}}' ], $defaultLanguage); // Get vacancy apply format $vacancyApplyFormat = Routes::get('vacancy-apply', [ 'slug' => '{{slug}}' ], $locale); $result = OtysApi::post([ 'method' => 'Otys.Services.VacancyService.saveUrlFormat', 'params' => [ $clientCode, $website, $localeCode, [ 'id' => '1', 'domain' => $domain, 'vacancy_apply' => $vacancyApplyFormat, 'vacancy_detail' => $vacancyDetailFormat, 'vacancy_preview' => $vacancyDetailFormat . '?preview=true', ] ] ], false, $localeCode); } } /** * Add routes * * @return void */ public static function addRoutes() { static::$routes = []; /** * * Public pages * */ $routes = get_option('otys_option_language_routes', ''); if ($routes !== "" && is_array($routes)) { foreach ($routes as $module => $moduleRoutes) { foreach ($moduleRoutes as $route) { if (!isset($route['locale'])) { continue; } /** * Vacancies overview routes */ if ($module === 'vacancy') { // Vacancy apply static::add( 'vacancy-apply', [ 'en' => '/' . $route['slug'] . '/:slug/apply/', 'nl' => '/' . $route['slug'] . '/:slug/solliciteer/', 'de' => '/' . $route['slug'] . '/:slug/bewerben/', 'fr' => '/' . $route['slug'] . '/:slug/postuler/', 'es' => '/' . $route['slug'] . '/:slug/aplicar/' ], ['\Otys\OtysPlugin\Controllers\VacanciesApplyController', 'callback'], ['POST', 'GET'], $route['locale'] ); // Vacancy Detail static::add( 'vacancy-detail', ['/' . $route['slug'] . '/:slug/'], ['\Otys\OtysPlugin\Controllers\VacanciesDetailController', 'callback'], ['POST', 'GET'], $route['locale'], 'vacancy', ['\Otys\OtysPlugin\Controllers\VacanciesDetailController', 'detailCallback'] ); } } } } /** * Webhook routes */ // Webhook entry point static::add( 'webhooks', ['/webhooks/'], ['\Otys\OtysPlugin\Controllers\WebhooksController', 'index'], ['POST', 'GET'] ); /** * Sitemap */ static::add( 'sitemap', ['/otys-sitemap/'], ['\Otys\OtysPlugin\Controllers\SitemapController', 'callback'], ['POST', 'GET'] ); } }