prefix."realty_tech_links" . " ( `id` int(11) NOT NULL AUTO_INCREMENT, `post_id` int(11) NULL, `url` varchar(500) NOT NULL, `link_id` varchar(255) NOT NULL, `link_name` varchar(250) NOT NULL, `page_type` varchar(50) NOT NULL, `use_widget` varchar(50) NOT NULL default '0', PRIMARY KEY (`id`) )"; dbDelta($sql); $sql = "CREATE TABLE IF NOT EXISTS " . $wpdb->prefix."realty_tech_api" . " ( `id` int(11) NOT NULL AUTO_INCREMENT, `api` varchar(255) NOT NULL, PRIMARY KEY (`id`) )"; dbDelta($sql); } public static function RTapexRemove() { global $wpdb; update_option( 'apexAgentApiKey', '' ); update_option( 'ApexIdxDomainUrl', '' ); $deletePages = $wpdb->get_col("SELECT post_id from ".$wpdb->prefix."realty_tech_links"); $wpdb->query("DROP TABLE IF EXISTS ".$wpdb->prefix."realty_tech_links"); $wpdb->query("DROP TABLE IF EXISTS ".$wpdb->prefix."realty_tech_api"); if($deletePages) { foreach($deletePages as $pageId) { wp_delete_post($pageId,true); $wpdb->query("DELETE from ".$wpdb->prefix."postmeta where post_id = $pageId"); } } } public static function RTapexInitHooks() { add_action('admin_enqueue_scripts', array( 'RTapexIdxBase', 'RTapexAddScript' ) ); add_action('admin_enqueue_scripts', array( 'RTapexIdxBase', 'RTapexAddStyle' )); add_action('admin_menu', array( 'RTapexIdxBase', 'RTapexAddMenu' )); add_action('admin_menu', array('RTapexIdxBase','RTapexValidateApi')); add_action('before_delete_post', array('RTapexIdxBase','RTapexUpdateLinksOnDeletePage')); add_action('save_post', array('RTapexIdxBase','RTapexUpdateWrapperPage')); } public static function RTapexInitFilters() { add_filter("plugin_action_links_" . RTAI_PLUGIN_FILE_LOC, array( 'RTapexIdxBase', 'RTapexActLinks')); add_filter('page_link', array( 'RTapexIdxBase', 'RTapexSetNewPageLink'), 25, 2); add_filter( 'get_pages', array( 'RTapexIdxBase', 'RTapexFilterWrapperPage')); } public static function RTapexInitShortCode() { add_shortcode('realtyTech-search-form', array( 'RTapexIdxBase','RTapexSearchForm')); add_shortcode('realtyTech-slider', array( 'RTapexIdxBase','RTapexSlider')); add_shortcode('realtyTech-grid', array( 'RTapexIdxBase','RTapexGrid')); add_shortcode('realtyTech-featured-market', array( 'RTapexIdxBase','RTapexFeaturedMarket')); } public static function RTapexInitRegister() { /*css for search box widget*/ wp_register_style('apexIdxSearchBoxStyle', plugins_url('css/apexIdxSearchBoxWidget.css', __FILE__)); /*end css for search box widget*/ /*js for search box widget*/ wp_register_script('apexIdxsearchBoxWidget', plugins_url('js/apexIdxsearchBoxWidget.js', __FILE__)); /*end js for search box widget*/ /*js for search box widget*/ wp_register_script('apexIdxClipboard', plugins_url('js/clipboard.min.js', __FILE__)); /*end js for search box widget*/ /*css for featured market*/ wp_register_style('apexIdxFeaturedMarket', plugins_url('css/apexIdxFeaturedMarket.css', __FILE__)); /*end css for featured market*/ /*js for featured market*/ wp_register_script('apexIdxFeaturedMarket', plugins_url('js/apexIdxFeaturedMarket.js', __FILE__)); /*end js for featured market*/ /*css for property slider*/ wp_register_style('apexIdxFPSlider', plugins_url('css/apexIdxFPSlider.css', __FILE__)); wp_register_style('apexIdxFPSliderGrid', plugins_url('css/apexIdxFPGridSlider.css', __FILE__)); /*end css for property slider*/ /*js for property grid view*/ wp_register_script('apexIdxInitializeSlider', plugins_url('js/apexIdxInitializeSlider.js', __FILE__)); wp_register_script('apexIdxInitializeSliderGrid', plugins_url('js/apexIdxInitializeGrid.js', __FILE__)); /*end js for property slider*/ } public static function RTapexActLinks( $exsitingsLinksArray ) { // Add a link to this plugin's settings page $apexPageLink = 'Settings'; array_unshift( $exsitingsLinksArray, $apexPageLink ); return $exsitingsLinksArray; } public static function RTapexAddScript($page) { if( 'toplevel_page_apexApiVerification' === $page || 'apex-idx_page_RTapexAgentWidgets' === $page || 'apex-idx_page_RTapexAgentDefaultPages' === $page): wp_enqueue_script('apexIdxbackend', plugins_url('js/apexIdxbackend.js', __FILE__)); endif; if( 'apex-idx_page_RTapexAgentWidgets' === $page ): wp_enqueue_script('apexIdxClipboard'); endif; } public static function RTapexAddStyle($page) { if( 'toplevel_page_apexApiVerification' === $page || 'apex-idx_page_RTapexAgentWidgets' === $page || 'apex-idx_page_RTapexAgentDefaultPages' === $page): wp_enqueue_style('apexIdxbackend', plugins_url('css/apexIdxBackend.css', __FILE__)); wp_enqueue_style('apexBootstrapMin', plugins_url('css/bootstrap.min.css', __FILE__)); endif; } public static function RTapexAddMenu() { add_menu_page('REALTYTECH MLS Plugin Options', 'Apex IDX', 'administrator', 'apexApiVerification', array( 'RTapexIdxBase', 'RTapexApiVerification' ) ); add_submenu_page('apexApiVerification', 'API Console', 'i. API Console', 'administrator', 'apexApiVerification', array( 'RTapexIdxBase', 'RTapexApiVerification' ) ); add_submenu_page('apexApiVerification', 'Manage Apex Idx Widgets', 'ii. Widgets', 'administrator', 'RTapexAgentWidgets', array( 'RTapexIdxBase', 'RTapexAgentWidgets' ) ); add_submenu_page('apexApiVerification', 'Manage Apex Idx Pages', 'iii. Idx Pages', 'administrator', 'RTapexAgentDefaultPages', array( 'RTapexIdxBase', 'RTapexAgentDefaultPages' ) ); } public static function RTapexValidateApi() { global $wpdb; global $apexApiValidationError; global $apexApiValidationSuc; $pageName = get_admin_page_title(); register_setting('apexIdx-settings-group', "apexAgentApiKey"); if (get_option('apexAgentApiKey') != '') { $apikey = get_option('apexAgentApiKey'); $existApi = $wpdb->get_var("SELECT * from ".$wpdb->prefix."realty_tech_api WHERE api = '$apikey'"); if(!$existApi) { if(in_array($pageName ,array('Manage Apex Idx Widgets', 'Manage Apex Idx Pages'))) { wp_redirect('admin.php?page=apexApiVerification'); } $status = RTapexApiAuthentication('authenticate'); if(is_wp_error($status) ) { $apexApiValidationError = $status->get_error_message(); } else { $apexApiValidationSuc = 'You have been registered successfully'; $id = self::RTapexSaveApi($apikey); if($id) { self::RTapexSaveLinks($status); } } } else { $apexApiValidationSuc = 'You are already registered'; } } else { if(in_array($pageName ,array('Manage Apex Idx Widgets', 'Manage Apex Idx Pages'))) { wp_redirect('admin.php?page=apexApiVerification'); exit; } } } public static function RTapexApiVerification() { global $apexApiValidationError; global $apexApiValidationSuc; include RTAI_PLUGIN_DIR . '/views/apexIdxForm.php'; } public static function RTapexAgentWidgets() { global $wpdb; if(isset($_POST['getLatestSelection']) && $_POST['getLatestSelection'] == 1) { self::RTapexGetLatestUpdate(); } $customLinks = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}realty_tech_links where page_type = 'custom' and use_widget= '1'"); include RTAI_PLUGIN_DIR . '/views/apexIdxWidget.php'; } public static function RTapexAgentDefaultPages() { global $wpdb; $link = array(); if(isset($_POST['getLatestSelection']) && $_POST['getLatestSelection'] == 1) { self::RTapexGetLatestUpdate(); } if(isset($_POST['defaultPage']) && $_POST['defaultPage'] == 1) { $defaultLinks = ($_POST['defaultLinks']) ? $_POST['defaultLinks'] : array(); self::RTapexManagePages($defaultLinks, 'default'); } if(isset($_POST['customPage']) && $_POST['customPage'] == 1) { $customLinks = ($_POST['customLinks']) ? $_POST['customLinks'] : array(); self::RTapexManagePages($customLinks, 'custom'); } if(isset($_POST['wrapperPage']) && $_POST['wrapperPage'] == 1) { $wrapperPageName = ($_POST['wrapperPageName']) ? $_POST['wrapperPageName'] : ''; if(!empty($wrapperPageName)) self::RTapexCreateWrapperPage($wrapperPageName); } $defaultLinks = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}realty_tech_links where page_type = 'default'"); $customLinks = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}realty_tech_links where page_type = 'custom'"); $wrapperLinks = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}realty_tech_links where page_type = 'wrapper'"); include RTAI_PLUGIN_DIR . '/views/apexIdxPages.php'; } public static function RTapexIsApiVerified() { global $wpdb; if (get_option('apexAgentApiKey') != '') { $apikey = get_option('apexAgentApiKey'); $existApi = $wpdb->get_var("SELECT * from ".$wpdb->prefix."realty_tech_api WHERE api = '$apikey'"); if(!$existApi) { return false; } else { return true; } } else { return false; } } public static function RTapexSearchForm() { ob_start(); $isIsApiVerified = self::RTapexIsApiVerified(); if(!$isIsApiVerified) { return; } wp_enqueue_script('jquery'); wp_localize_script( 'apexIdxsearchBoxWidget', 'searchFormParams', array( 'ApexIdxDomainUrl' => get_option('ApexIdxDomainUrl') ) ); wp_enqueue_style('apexIdxSearchBoxStyle'); wp_enqueue_script('jquery-ui-autocomplete'); wp_enqueue_script('apexIdxsearchBoxWidget'); include RTAI_PLUGIN_DIR . 'views/apexIdxSearchFormWidget.php'; $output = ob_get_contents(); ob_end_clean(); return $output; } public static function RTapexSlider($atts){ global $wpdb; $isIsApiVerified = self::RTapexIsApiVerified(); if(!$isIsApiVerified) { return; } wp_enqueue_script('jquery'); $apexIdxDomainUrl = get_option('ApexIdxDomainUrl'); if($atts['type'] == 'c') { $url = $apexIdxDomainUrl.'webservices/getcustomLinkProperties.php'; } else { $url = $apexIdxDomainUrl.'webservices/getFeaturedProperties.php'; } $idxDomainUrlResults = $apexIdxDomainUrl.'featuredproperties/'; if($atts['type'] == 'c') { $row = $wpdb->get_row("SELECT link_name FROM ".$wpdb->prefix."realty_tech_links WHERE link_id ='{$atts['id']}'", ARRAY_A); $idxDomainUrlResults = $apexIdxDomainUrl.'c/'.str_replace(' ', '-', $row['link_name']).'/'; } wp_localize_script( 'apexIdxInitializeSlider', 'sliderParams', array( 'idxDomainUrl' => $url, 'idxDomainUrlResults'=>$idxDomainUrlResults, 'type'=>$atts['type'], 'id'=> ($atts['type'] == 'c') ? $atts['id'] : '', ) ); wp_enqueue_style('apexIdxFPSlider'); wp_enqueue_script('apexIdxInitializeSlider'); $output = '
'; return $output; } public static function RTapexGrid($atts) { global $wpdb; $isIsApiVerified = self::RTapexIsApiVerified(); if(!$isIsApiVerified) { return; } $apexIdxDomainUrl = get_option('ApexIdxDomainUrl'); if($atts['type'] == 'c') { $url = $apexIdxDomainUrl.'webservices/getcustomLinkProperties.php'; } else { $url = $apexIdxDomainUrl.'webservices/getFeaturedProperties.php'; } $idxDomainUrlResults = $apexIdxDomainUrl.'featuredproperties/'; if($atts['type'] == 'c') { $row = $wpdb->get_row("SELECT link_name FROM ".$wpdb->prefix."realty_tech_links WHERE link_id = '{$atts['id']}'", ARRAY_A); $idxDomainUrlResults = $apexIdxDomainUrl.'c/'.str_replace(' ', '-', $row['link_name']).'/'; } wp_localize_script( 'apexIdxInitializeSliderGrid', 'GridParams', array( 'idxDomainUrl' => $url, 'idxDomainUrlResults'=>$idxDomainUrlResults, 'type'=>$atts['type'], 'id'=> ($atts['type'] == 'c') ? $atts['id'] : '', ) ); wp_enqueue_script('apexIdxInitializeSliderGrid'); wp_enqueue_style('apexIdxFPSliderGrid'); $output = '
loading
'; return $output; } public static function RTapexFeaturedMarket() { $isIsApiVerified = self::RTapexIsApiVerified(); if(!$isIsApiVerified) { return; } wp_enqueue_script('jquery'); wp_localize_script( 'apexIdxFeaturedMarket', 'fMarketParams', array( 'ApexIdxDomainUrl' => get_option('ApexIdxDomainUrl') ) ); wp_enqueue_style('apexIdxFeaturedMarket'); wp_enqueue_script('apexIdxFeaturedMarket'); include RTAI_PLUGIN_DIR . 'views/apexIdxFeaturedMarket.php'; } public static function RTapexUpdateLinksOnDeletePage($postID) { global $wpdb; $row = $wpdb->get_row("SELECT page_type FROM ".$wpdb->prefix."realty_tech_links WHERE post_id = '{$postID}' ", ARRAY_A); if($row['page_type'] == 'wrapper') { $wpdb->query("DELETE FROM ".$wpdb->prefix."realty_tech_links where post_id = '{$postID}' AND page_type = 'wrapper'"); } else { $wpdb->update( $wpdb->prefix."realty_tech_links", array( 'post_id' => NULL ), array( 'post_id' => $postID ), array( '%d' ), array( '%d' ) ); } } public static function RTapexUpdateWrapperPage($postId) { global $wpdb; if (wp_is_post_revision($postId)) return; $row = $wpdb->get_row("SELECT page_type FROM ".$wpdb->prefix."realty_tech_links WHERE post_id = '{$postId}' ", ARRAY_A); if($row['page_type'] == 'wrapper') { $postTitle = get_the_title($postId); $postUrl = get_permalink($postId); //Update links table add genrated post id $wpdb->update( $wpdb->prefix."realty_tech_links", array( 'url' => $postUrl, 'link_name' => $postTitle, 'link_id' => $postTitle ), array( 'post_id' => $postId ), array( '%s', '%s', '%s' ), array( '%d' ) ); } } public static function RTapexSetNewPageLink($newlink, $pid) { global $wpdb; $row = $wpdb->get_row("SELECT url FROM ".$wpdb->prefix."realty_tech_links WHERE post_id = '{$pid}' ", ARRAY_A); if($row['url'] && !empty($row['url'])) { $newlink = $row['url']; } return $newlink; } public static function RTapexFilterWrapperPage($pages) { global $wpdb; $row = $wpdb->get_row("SELECT post_id FROM ".$wpdb->prefix."realty_tech_links WHERE page_type='wrapper'", ARRAY_A); if($row) { $pagesArray = $pages; for($i=0; $i< count($pages); $i++) { if($pages[$i]->ID == $row['post_id']) { unset($pagesArray[$i]); } } return $pagesArray; } else { return $pages; } } public static function RTapexManagePages($selectedPages = array(), $pageType) { global $wpdb; $links = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}realty_tech_links where page_type = '{$pageType}'"); foreach($links as $link) { if(in_array($link->link_id, $selectedPages)) { self::RTapexInsertUpdatePage($link); } else { ($link->post_id != NULL) ? self::RTapexDeletePage($link) : ''; } } echo '

your selection has been updated successfully

'; } public static function RTapexCreateWrapperPage($pageName) { global $wpdb; $pageContent = ''; $pageTitle = $pageName ; $wrapperPage = array( 'post_title' => $pageTitle, 'post_name' => $pageTitle, 'post_content' => $pageContent, 'post_type' => 'page', 'post_status' => 'publish' ); $wrapperPageId = wp_insert_post($wrapperPage); $wrapperPageUrl = get_permalink($wrapperPageId); //Insert into links table $wpdb->insert( $wpdb->prefix."realty_tech_links", array( 'url' => $wrapperPageUrl, 'link_id' =>$pageName, 'post_id' => $wrapperPageId, 'link_name' =>$pageName, 'page_type' =>'wrapper' ), array( '%s', '%s', '%d', '%s', '%s' ) ); } public static function RTapexInsertUpdatePage($link) { global $wpdb; $row = $wpdb->get_row("SELECT * FROM ".$wpdb->prefix."realty_tech_links WHERE link_id = '{$link->link_id}' ", ARRAY_A); if($row['post_id'] === NULL || empty($row['post_id'])) { //Insert into post table $wpdb->insert( $wpdb->posts, array( 'post_title' => $row['link_name'], 'post_type' => 'page', 'post_name' => $row['link_name'] ), array( '%s', '%s', '%s' ) ); $post_id = $wpdb->insert_id; //Update links table add genrated post id $wpdb->update( $wpdb->prefix."realty_tech_links", array( 'post_id' => $post_id ), array( 'link_id' => $link->link_id ), array( '%d' ), array( '%s' ) ); } else { $wpdb->update( $wpdb->posts, array( 'post_title' => $row['link_name'], 'post_type' => 'page', 'post_name' => $row['link_name'] ), array( 'ID' => $row['post_id'] ), array( '%s', '%s', '%s' ), array( '%d' ) ); } } public static function RTapexUpdateCustomLinks($link) { global $wpdb; if($row = $wpdb->get_row("SELECT * FROM ".$wpdb->prefix."realty_tech_links WHERE link_id = '{$link['link_id']}' ", ARRAY_A)) { //Update links table add genrated post id $wpdb->update( $wpdb->prefix."realty_tech_links", array( 'url' => $link['url'], 'link_name' => $link['link_name'], 'use_widget' => $link['use_widget'], ), array( 'link_id' => $link['link_id'] ), array( '%s', '%s' ), array( '%d' ) ); if($row['post_id'] != NULL && !empty($row['post_id'])) { //Update post related to this link $wpdb->update( $wpdb->posts, array( 'post_title' => $link['link_name'], 'post_type' => 'page', 'post_name' => $link['link_name'] ), array( 'ID' => $row['post_id'] ), array( '%s', '%s', '%s' ), array( '%d' ) ); } } else { //Insert into links table $wpdb->insert( $wpdb->prefix."realty_tech_links", array( 'url' => $link['url'], 'link_id' =>$link['link_id'], 'link_name' =>$link['link_name'], 'page_type' =>'custom', 'use_widget'=> $link['use_widget'] ), array( '%s', '%s', '%s', '%s' ) ); } } public static function RTapexDeletePage($link) { global $wpdb; wp_delete_post($link->post_id,true); $wpdb->query("DELETE from ".$wpdb->prefix."postmeta where post_id = $link->post_id"); //Update links table set post id null $wpdb->update( $wpdb->prefix."realty_tech_links", array( 'post_id' => 'NULL' ), array( 'link_id' => $link->link_id ), array( '%d' ), array( '%s' ) ); } public static function RTapexGetLatestUpdate() { $status = RTapexApiAuthentication('getLinks'); if( is_wp_error($status) ) { $apiError = $status->get_error_message(); echo '

'.$apiError.'

'; return; } else { self::RTapexUpdateLinks($status); echo '

Your latest update has been fetched successfully. Please take an action

'; } } public static function RTapexSaveApi($apikey) { global $wpdb; $wpdb->insert($wpdb->prefix.'realty_tech_api', array('api' => $apikey), array('%s') ); return $wpdb->insert_id; } public static function RTapexUpdateLinks($response = array()) { global $wpdb; $links = $response->links; $cname = $response->cname; $domain = $response->domain; $newLinks = array(); $ApexIdxDomainUrl = 'http://'.$cname.'.'.$domain.'/idx/'; update_option('ApexIdxDomainUrl', $ApexIdxDomainUrl); //update Default Pages url if cname change $defaultPages = array('Advanced Search', 'Sold Properties', 'Featured Properties', 'Open Houses', 'Featured Offices'); foreach($defaultPages as $defaultPage) { //Parse some information before insert $dpName = $defaultPage; $dpUrl = 'http://'.$cname.'.'.$domain.'/idx/'.str_replace(' ','', strtolower($dpName)); $wpdb->update( $wpdb->prefix."realty_tech_links", array( 'url' => $dpUrl ), array( 'link_id' =>str_replace(' ','', $dpName) ), array( '%s' ), array( '%s' ) ); } //Create update custom Links Page if(count($links) > 0) { foreach($links as $link) { $data = array(); //Parse some information before insert $name = str_replace('-', ' ', $link->name); $url = 'http://'.$cname.'.'.$domain.'/idx/c/'.$link->name; $data['link_name'] = $name; $data['use_widget'] = $link->useAsWidget; $data['url'] = $url; $data['link_id'] = $link->id; $newLinks[] = $link->id; self::RTapexUpdateCustomLinks($data); } } $SavedLinks = $wpdb->get_col("SELECT link_id FROM {$wpdb->prefix}realty_tech_links where page_type = 'custom'"); $deleteLinks = array_diff($SavedLinks, $newLinks); if($deleteLinks > 0) { self::RTapexDeleteLinksWithPages($deleteLinks); } } public static function RTapexDeleteLinksWithPages($deleteLinks) { global $wpdb; $linkString = ""; if(count($deleteLinks) > 0) { foreach($deleteLinks as $linkId) { $linkString .= "'$linkId',"; } $linkString = rtrim($linkString,','); $deletePages = $wpdb->get_col("SELECT post_id from ".$wpdb->prefix."realty_tech_links where link_id IN ($linkString) AND post_id != '' AND page_type = 'custom'"); if($wpdb->query("DELETE from ".$wpdb->prefix."realty_tech_links where link_id IN ($linkString) AND page_type = 'custom'") !== false) { foreach($deletePages as $pageId) { wp_delete_post($pageId,true); $wpdb->query("DELETE from ".$wpdb->prefix."postmeta where post_id = $pageId"); } } return true; } return false; } public static function RTapexSaveLinks($response = array()) { global $wpdb; $links = $response->links; $cname = $response->cname; $domain = $response->domain; $ApexIdxDomainUrl = 'http://'.$cname.'.'.$domain.'/idx/'; update_option('ApexIdxDomainUrl', $ApexIdxDomainUrl); //Create custom Links Page if($links && count($links) > 0) { foreach($links as $link) { //Parse some information before insert $name = str_replace('-', ' ', $link->name); $url = 'http://'.$cname.'.'.$domain.'/idx/c/'.$link->name; //Insert into links table $wpdb->insert( $wpdb->prefix."realty_tech_links", array( 'url' => $url, 'link_id' =>$link->id, 'link_name' =>$name, 'page_type' =>'custom', 'use_widget' =>$link->useAsWidget ), array( '%s', '%s', '%s', '%s', '%s' ) ); } } //Create Default Pages $defaultPages = array('Advanced Search', 'Sold Properties', 'Featured Properties', 'Open Houses', 'Featured Offices'); foreach($defaultPages as $defaultPage) { //Parse some information before insert $name = $defaultPage; $url = 'http://'.$cname.'.'.$domain.'/idx/'.str_replace(' ','', strtolower($name)); $wpdb->insert( $wpdb->prefix."realty_tech_links", array( 'url' => $url, 'link_id' =>str_replace(' ','', $name), 'link_name' =>$name, 'page_type' =>'default' ), array( '%s', '%s', '%s', '%s' ) ); } } }