| 1 |
<?php |
| 2 |
|
| 3 |
if (! defined('ABSPATH')) { |
| 4 |
exit; // Exit if accessed directly |
| 5 |
} |
| 6 |
|
| 7 |
|
| 8 |
/** |
| 9 |
* Singleton implementation of iHomefinderRewriteRules |
| 10 |
* |
| 11 |
* All iHomefinder requests are directed to the $rootPageName, which tries to load a WordPress page that |
| 12 |
* does not exist. We do not want to load a real page. We get the content from the iHomefinder services |
| 13 |
* and display it as a virtual WordPress post. |
| 14 |
* |
| 15 |
* The rewrite rules below set a variable iHomefinderConstants::IHF_TYPE_URL_VAR that is used to determine |
| 16 |
* which VirtualPage retrieves the content from iHomefinder |
| 17 |
* |
| 18 |
* @author ihomefinder |
| 19 |
*/ |
| 20 |
class iHomefinderRewriteRules |
| 21 |
{ |
| 22 |
|
| 23 |
private static $instance; |
| 24 |
private $urlFactory; |
| 25 |
private $rootPageName; |
| 26 |
|
| 27 |
private function __construct() |
| 28 |
{ |
| 29 |
$this->rootPageName = "index.php?pagename=non_existent_page"; |
| 30 |
$this->logger = iHomefinderLogger::getInstance(); |
| 31 |
} |
| 32 |
|
| 33 |
public static function getInstance() |
| 34 |
{ |
| 35 |
if (!isset(self::$instance)) { |
| 36 |
self::$instance = new self(); |
| 37 |
} |
| 38 |
return self::$instance; |
| 39 |
} |
| 40 |
|
| 41 |
public function initialize() |
| 42 |
{ |
| 43 |
$this->addQueryVar(iHomefinderConstants::IHF_TYPE_URL_VAR); |
| 44 |
$this->initRewriteRules(); |
| 45 |
} |
| 46 |
|
| 47 |
public function flushRules() |
| 48 |
{ |
| 49 |
global $wp_rewrite; |
| 50 |
$wp_rewrite->flush_rules(); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Function to initialize rewrite rules for the IHF plugin. |
| 55 |
* |
| 56 |
* During development we initialize and flush the rules often, but |
| 57 |
* this should only be performed when the plugin is registered. |
| 58 |
* |
| 59 |
* We need to map certain URL patters ot an internal ihf page |
| 60 |
* Once requests are routed to that page, we can handle different |
| 61 |
* behavior in functions that listen for updates on that page. |
| 62 |
*/ |
| 63 |
private function initRewriteRules() |
| 64 |
{ |
| 65 |
$this->setRewriteRules(""); |
| 66 |
//set the rules again, to support almost pretty permalinks |
| 67 |
$this->setRewriteRules("index.php/"); |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Add a rewrite rule to the WordPress system |
| 72 |
* |
| 73 |
* @param string $type |
| 74 |
* @param string $pattern |
| 75 |
* |
| 76 |
* @return void |
| 77 |
*/ |
| 78 |
private function addRule($type, $pattern) |
| 79 |
{ |
| 80 |
$matches = array(); |
| 81 |
preg_match_all("/\{(.*?)\}/", $pattern, $matches); |
| 82 |
$matches = $matches[1]; |
| 83 |
$regex = $pattern; |
| 84 |
$redirect = $this->rootPageName . "&" . iHomefinderConstants::IHF_TYPE_URL_VAR . "=" . $type; |
| 85 |
foreach ($matches as $key => $value) { |
| 86 |
$key += 1; |
| 87 |
if (!empty($value)) { |
| 88 |
$regex = str_replace("{" . $value . "}", "([^/]+)", $regex); |
| 89 |
$redirect .= "&" . $value . "=\$matches[" . $key . "]"; |
| 90 |
$this->addQueryVar($value); |
| 91 |
} |
| 92 |
} |
| 93 |
//anchor regex to prevent matching permalink contained in another permalink (ex. home-for-sale and home-for-sale-) |
| 94 |
$regex = "^" . $regex . "$"; |
| 95 |
//$this->logger->debug("just added rewrite rule: " . $regex . $redirect); |
| 96 |
add_rewrite_rule($regex, $redirect, "top"); |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* WordPress reserves some names (name, term, page) in /wp-includes/class-wp.php |
| 101 |
* ($public_query_vars, $private_query_vars) that should not be used |
| 102 |
* |
| 103 |
* @param string $name |
| 104 |
* |
| 105 |
* @return void |
| 106 |
*/ |
| 107 |
private function addQueryVar($name) |
| 108 |
{ |
| 109 |
global $wp; |
| 110 |
$wp->add_query_var($name); |
| 111 |
//$this->logger->debug("just added query var: " . $name); |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Note: The order of these search rules is important. The match will pick |
| 116 |
* the first page it finds that matches any of the first few selected characters. |
| 117 |
|
| 118 |
* For example: |
| 119 |
* listing-search |
| 120 |
* listing-search-results |
| 121 |
|
| 122 |
* When "listing-search-results" is selected, the "listing-search" may be |
| 123 |
* returned instead. If you encounter this problem, a simple fix is to change |
| 124 |
* the first few characters of the problem page to something unique. |
| 125 |
* |
| 126 |
* @param string $matchRulePrefix |
| 127 |
* |
| 128 |
* @return void |
| 129 |
*/ |
| 130 |
private function setRewriteRules($matchRulePrefix) |
| 131 |
{ |
| 132 |
$urlFactory = iHomefinderUrlFactory::getInstance(); |
| 133 |
$this->addRule( |
| 134 |
iHomefinderVirtualPageFactory::LISTING_ADVANCED_SEARCH_FORM, |
| 135 |
$matchRulePrefix . $urlFactory->getListingsAdvancedSearchFormUrl(false) . "/{boardId}" |
| 136 |
); |
| 137 |
$this->addRule( |
| 138 |
iHomefinderVirtualPageFactory::LISTING_ADVANCED_SEARCH_FORM, |
| 139 |
$matchRulePrefix . $urlFactory->getListingsAdvancedSearchFormUrl(false) |
| 140 |
); |
| 141 |
$this->addRule( |
| 142 |
iHomefinderVirtualPageFactory::OFFICE_LIST, |
| 143 |
$matchRulePrefix . $urlFactory->getOfficeListUrl(false) |
| 144 |
); |
| 145 |
$this->addRule( |
| 146 |
iHomefinderVirtualPageFactory::MLS_PORTAL_BOARD_OFFICE_LIST, |
| 147 |
$matchRulePrefix . $urlFactory->getMlsPortalBoardOfficeListUrl(false) |
| 148 |
); |
| 149 |
$this->addRule( |
| 150 |
iHomefinderVirtualPageFactory::MLS_PORTAL_BOARD_OFFICE_LIST_NAME_STARTS_WITH, |
| 151 |
$matchRulePrefix . $urlFactory->getMlsPortalBoardOfficeListNameStartsWithUrl(false) |
| 152 |
); |
| 153 |
$this->addRule( |
| 154 |
iHomefinderVirtualPageFactory::OFFICE_DETAIL, |
| 155 |
$matchRulePrefix . $urlFactory->getOfficeDetailUrl(false) . "/{officeName}/{officeId}" |
| 156 |
); |
| 157 |
$this->addRule( |
| 158 |
iHomefinderVirtualPageFactory::MLS_PORTAL_BOARD_OFFICE_DETAIL, |
| 159 |
$matchRulePrefix . $urlFactory->getMlsPortalBoardOfficeDetailUrl(false) . "/{boardOfficeName}/{boardOfficeId}" |
| 160 |
); |
| 161 |
$this->addRule( |
| 162 |
iHomefinderVirtualPageFactory::AGENT_LIST, |
| 163 |
$matchRulePrefix . $urlFactory->getAgentListUrl(false) |
| 164 |
); |
| 165 |
$this->addRule( |
| 166 |
iHomefinderVirtualPageFactory::MLS_PORTAL_BOARD_MEMBER_LIST, |
| 167 |
$matchRulePrefix . $urlFactory->getMlsPortalBoardMemberListUrl(false) |
| 168 |
); |
| 169 |
$this->addRule( |
| 170 |
iHomefinderVirtualPageFactory::MLS_PORTAL_BOARD_MEMBER_LIST_LAST_NAME_STARTS_WITH, |
| 171 |
$matchRulePrefix . $urlFactory->getMlsPortalBoardMemberListLastNameStartsWithUrl(false) |
| 172 |
); |
| 173 |
$this->addRule( |
| 174 |
iHomefinderVirtualPageFactory::MLS_PORTAL_BOARD_OFFICE_SEARCH, |
| 175 |
$matchRulePrefix . $urlFactory->getMlsPortalBoardOfficeSearchUrl(false) |
| 176 |
); |
| 177 |
$this->addRule( |
| 178 |
iHomefinderVirtualPageFactory::MLS_PORTAL_BOARD_MEMBER_SEARCH, |
| 179 |
$matchRulePrefix . $urlFactory->getMlsPortalBoardMemberSearchUrl(false) |
| 180 |
); |
| 181 |
$this->addRule( |
| 182 |
iHomefinderVirtualPageFactory::AGENT_DETAIL, |
| 183 |
$matchRulePrefix . $urlFactory->getAgentDetailUrl(false) . "/{agentName}/{agentId}" |
| 184 |
); |
| 185 |
$this->addRule( |
| 186 |
iHomefinderVirtualPageFactory::MLS_PORTAL_BOARD_MEMBER_DETAIL, |
| 187 |
$matchRulePrefix . $urlFactory->getMlsPortalBoardMemberDetailUrl(false) . "/{boardMemberName}/{boardMemberId}" |
| 188 |
); |
| 189 |
$this->addRule( |
| 190 |
iHomefinderVirtualPageFactory::CONTACT_FORM, |
| 191 |
$matchRulePrefix . $urlFactory->getContactFormUrl(false) |
| 192 |
); |
| 193 |
$this->addRule( |
| 194 |
iHomefinderVirtualPageFactory::VALUATION_FORM, |
| 195 |
$matchRulePrefix . $urlFactory->getValuationFormUrl(false) |
| 196 |
); |
| 197 |
$this->addRule( |
| 198 |
iHomefinderVirtualPageFactory::MORTGAGE_CALCULATOR, |
| 199 |
$matchRulePrefix . $urlFactory->getMortgageCalculatorUrl(false) |
| 200 |
); |
| 201 |
$this->addRule( |
| 202 |
iHomefinderVirtualPageFactory::OPEN_HOME_SEARCH_FORM, |
| 203 |
$matchRulePrefix . $urlFactory->getOpenHomeSearchFormUrl(false) |
| 204 |
); |
| 205 |
$this->addRule( |
| 206 |
iHomefinderVirtualPageFactory::SOLD_FEATURED_LISTING, |
| 207 |
$matchRulePrefix . $urlFactory->getSoldFeaturedListingUrl(false) |
| 208 |
); |
| 209 |
$this->addRule( |
| 210 |
iHomefinderVirtualPageFactory::PENDING_FEATURED_LISTING, |
| 211 |
$matchRulePrefix . $urlFactory->getPendingFeaturedListingUrl(false) |
| 212 |
); |
| 213 |
$this->addRule( |
| 214 |
iHomefinderVirtualPageFactory::SUPPLEMENTAL_LISTING, |
| 215 |
$matchRulePrefix . $urlFactory->getSupplementalListingUrl(false) |
| 216 |
); |
| 217 |
$this->addRule( |
| 218 |
iHomefinderVirtualPageFactory::LISTING_SEARCH_FORM, |
| 219 |
$matchRulePrefix . $urlFactory->getListingsSearchFormUrl(false) |
| 220 |
); |
| 221 |
$this->addRule( |
| 222 |
iHomefinderVirtualPageFactory::MAP_SEARCH_FORM, |
| 223 |
$matchRulePrefix . $urlFactory->getMapSearchFormUrl(false) |
| 224 |
); |
| 225 |
$this->addRule( |
| 226 |
iHomefinderVirtualPageFactory::ORGANIZER_DELETE_SAVED_SEARCH_SUBMIT, |
| 227 |
$matchRulePrefix . $urlFactory->getOrganizerDeleteSavedSearchSubmitUrl(false) |
| 228 |
); |
| 229 |
$this->addRule( |
| 230 |
iHomefinderVirtualPageFactory::ORGANIZER_DELETE_SAVED_LISTING_SUBMIT, |
| 231 |
$matchRulePrefix . $urlFactory->getOrganizerDeleteSavedListingUrl(false) . "/{savedListingId}" |
| 232 |
); |
| 233 |
$this->addRule( |
| 234 |
iHomefinderVirtualPageFactory::ORGANIZER_EDIT_SAVED_SEARCH, |
| 235 |
$matchRulePrefix . $urlFactory->getOrganizerEditSavedSearchUrl(false) . "/{boardId}" |
| 236 |
); |
| 237 |
$this->addRule( |
| 238 |
iHomefinderVirtualPageFactory::ORGANIZER_EDIT_SAVED_SEARCH, |
| 239 |
$matchRulePrefix . $urlFactory->getOrganizerEditSavedSearchUrl(false) |
| 240 |
); |
| 241 |
$this->addRule( |
| 242 |
iHomefinderVirtualPageFactory::ORGANIZER_EDIT_SAVED_SEARCH_SUBMIT, |
| 243 |
$matchRulePrefix . $urlFactory->getOrganizerEditSavedSearchSubmitUrl(false) |
| 244 |
); |
| 245 |
$this->addRule( |
| 246 |
iHomefinderVirtualPageFactory::ORGANIZER_EMAIL_UPDATES_CONFIRMATION, |
| 247 |
$matchRulePrefix . $urlFactory->getOrganizerEmailUpdatesConfirmationUrl(false) |
| 248 |
); |
| 249 |
$this->addRule( |
| 250 |
iHomefinderVirtualPageFactory::ORGANIZER_HELP, |
| 251 |
$matchRulePrefix . $urlFactory->getOrganizerHelpUrl(false) |
| 252 |
); |
| 253 |
$this->addRule( |
| 254 |
iHomefinderVirtualPageFactory::ORGANIZER_EDIT_SUBSCRIBER, |
| 255 |
$matchRulePrefix . $urlFactory->getOrganizerEditSubscriberUrl(false) |
| 256 |
); |
| 257 |
$this->addRule( |
| 258 |
iHomefinderVirtualPageFactory::ORGANIZER_LOGIN, |
| 259 |
$matchRulePrefix . $urlFactory->getOrganizerLoginUrl(false) |
| 260 |
); |
| 261 |
$this->addRule( |
| 262 |
iHomefinderVirtualPageFactory::ORGANIZER_LOGOUT, |
| 263 |
$matchRulePrefix . $urlFactory->getOrganizerLogoutUrl(false) |
| 264 |
); |
| 265 |
$this->addRule( |
| 266 |
iHomefinderVirtualPageFactory::ORGANIZER_VIEW_SAVED_SEARCH, |
| 267 |
$matchRulePrefix . $urlFactory->getOrganizerViewSavedSearchUrl(false) . "/{searchProfileId}" |
| 268 |
); |
| 269 |
$this->addRule( |
| 270 |
iHomefinderVirtualPageFactory::ORGANIZER_VIEW_SAVED_SEARCH_LIST, |
| 271 |
$matchRulePrefix . $urlFactory->getOrganizerViewSavedSearchListUrl(false) |
| 272 |
); |
| 273 |
$this->addRule( |
| 274 |
iHomefinderVirtualPageFactory::ORGANIZER_VIEW_SAVED_LISTING_LIST, |
| 275 |
$matchRulePrefix . $urlFactory->getOrganizerViewSavedListingListUrl(false) |
| 276 |
); |
| 277 |
$this->addRule( |
| 278 |
iHomefinderVirtualPageFactory::ORGANIZER_RESEND_CONFIRMATION_EMAIL, |
| 279 |
$matchRulePrefix . $urlFactory->getOrganizerResendConfirmationEmailUrl(false) |
| 280 |
); |
| 281 |
$this->addRule( |
| 282 |
iHomefinderVirtualPageFactory::ORGANIZER_ACTIVATE_SUBSCRIBER, |
| 283 |
$matchRulePrefix . $urlFactory->getOrganizerActivateSubscriberUrl(false) |
| 284 |
); |
| 285 |
$this->addRule( |
| 286 |
iHomefinderVirtualPageFactory::ORGANIZER_SEND_SUBSCRIBER_PASSWORD, |
| 287 |
$matchRulePrefix . $urlFactory->getOrganizerSendSubscriberPasswordUrl(false) |
| 288 |
); |
| 289 |
$this->addRule( |
| 290 |
iHomefinderVirtualPageFactory::HOT_SHEET_LISTING_REPORT, |
| 291 |
$matchRulePrefix . $urlFactory->getHotSheetListingReportUrl(false) . "/{savedSearchName}/{savedSearchId}" |
| 292 |
); |
| 293 |
$this->addRule( |
| 294 |
iHomefinderVirtualPageFactory::HOT_SHEET_OPEN_HOME_REPORT, |
| 295 |
$matchRulePrefix . $urlFactory->getHotSheetOpenHomeReportUrl(false) . "/{savedSearchName}/{savedSearchId}" |
| 296 |
); |
| 297 |
$this->addRule( |
| 298 |
iHomefinderVirtualPageFactory::HOT_SHEET_MARKET_REPORT, |
| 299 |
$matchRulePrefix . $urlFactory->getHotSheetMarketReportUrl(false) . "/{savedSearchName}/{savedSearchId}" |
| 300 |
); |
| 301 |
$this->addRule( |
| 302 |
iHomefinderVirtualPageFactory::HOT_SHEET_LIST, |
| 303 |
$matchRulePrefix . $urlFactory->getHotsheetListUrl(false) |
| 304 |
); |
| 305 |
$this->addRule( |
| 306 |
iHomefinderVirtualPageFactory::LISTING_SOLD_DETAIL, |
| 307 |
$matchRulePrefix . $urlFactory->getListingSoldDetailUrl(false) . "/{listingAddress}/{listingNumber}/{boardId}" |
| 308 |
); |
| 309 |
$this->addRule( |
| 310 |
iHomefinderVirtualPageFactory::LISTING_DETAIL, |
| 311 |
$matchRulePrefix . $urlFactory->getListingDetailUrl(false) . "/{listingAddress}/{listingNumber}/{boardId}" |
| 312 |
); |
| 313 |
$this->addRule( |
| 314 |
iHomefinderVirtualPageFactory::LISTING_SEARCH_RESULTS, |
| 315 |
$matchRulePrefix . $urlFactory->getListingsSearchResultsUrl(false) |
| 316 |
); |
| 317 |
$this->addRule( |
| 318 |
iHomefinderVirtualPageFactory::FEATURED_SEARCH, |
| 319 |
$matchRulePrefix . $urlFactory->getFeaturedSearchResultsUrl(false) |
| 320 |
); |
| 321 |
/** |
| 322 |
* @deprecated used only in fixed width |
| 323 |
*/ |
| 324 |
$this->addRule( |
| 325 |
iHomefinderVirtualPageFactory::HOT_SHEET_LISTING_REPORT, |
| 326 |
$matchRulePrefix . $urlFactory->getHotSheetListingReportUrl(false) . "/{savedSearchId}" |
| 327 |
); |
| 328 |
/** |
| 329 |
* @deprecated only used to support old URL |
| 330 |
*/ |
| 331 |
$this->addRule( |
| 332 |
iHomefinderVirtualPageFactory::HOT_SHEET_LISTING_REPORT, |
| 333 |
$matchRulePrefix . "homes-for-sale-toppicks/{savedSearchName}/{savedSearchId}" |
| 334 |
); |
| 335 |
/** |
| 336 |
* @deprecated only used to support old URL |
| 337 |
*/ |
| 338 |
$this->addRule( |
| 339 |
iHomefinderVirtualPageFactory::LISTING_SEARCH_RESULTS, |
| 340 |
$matchRulePrefix . "address-listing-results" |
| 341 |
); |
| 342 |
/** |
| 343 |
* @deprecated only used to support old URL |
| 344 |
*/ |
| 345 |
$this->addRule( |
| 346 |
iHomefinderVirtualPageFactory::LISTING_SEARCH_RESULTS, |
| 347 |
$matchRulePrefix . "id-listing-results" |
| 348 |
); |
| 349 |
/** |
| 350 |
* @deprecated only used to support old URL |
| 351 |
*/ |
| 352 |
$this->addRule( |
| 353 |
iHomefinderVirtualPageFactory::ORGANIZER_LOGIN, |
| 354 |
$matchRulePrefix . "property-organizer-login-submit" |
| 355 |
); |
| 356 |
} |
| 357 |
} |
| 358 |
|