class IDXBoost_REST_API_Endpoints
{
const API_NAMESPACE = 'idx-boost';
const API_VERSION = 'v1';
const API_ADD_REGISTRATION_KEY = '/reg-key';
const API_CREATE_USER_ENDPOINT = '/users';
const API_GET_POST = '/posts';
const API_GET_CATEGORIES = '/categories';
const API_ADD_PAGES = '/add_page';
const API_EDIT_PAGES = '/edit_page';
const API_DELETE_PAGES = '/delete_page';
const API_REPLACE_FAVICON = '/replace_favicon';
const API_REPLACE_URL_SITE = '/replace_url_site';
const API_GET_PROPERTY_GROUP = '/get_property_group';
const API_ADD_PROPERTY_GROUP = '/add_property_group';
const API_UPDATE_PROPERTY_GROUP = '/update_property_group';
const API_UPDATE_PROPERTY_GROUP_PARENT = '/update_property_group_parent';
const API_DELETE_PROPERTY_GROUP = '/delete_property_group';
public static function registerEndpoints()
{
$dns_api_rest_name_version = implode('/', [self::API_NAMESPACE, self::API_VERSION]);
register_rest_route($dns_api_rest_name_version, self::API_CREATE_USER_ENDPOINT, array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => ['IDXBoost_REST_API_Endpoints', 'createUser'],
'permission_callback' => ['IDXBoost_REST_API_Endpoints', 'loginJWT']
));
register_rest_route($dns_api_rest_name_version, self::API_ADD_REGISTRATION_KEY, array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => ['IDXBoost_REST_API_Endpoints', 'addRegKey'],
'permission_callback' => ['IDXBoost_REST_API_Endpoints', 'loginJWT']
));
register_rest_route($dns_api_rest_name_version, self::API_GET_POST, array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => ['IDXBoost_REST_API_Endpoints', 'getPost'],
'permission_callback' => ['IDXBoost_REST_API_Endpoints', 'loginJWT']
));
register_rest_route($dns_api_rest_name_version, self::API_GET_PROPERTY_GROUP, array(
'methods' => WP_REST_Server::READABLE,
'callback' => ['IDXBoost_REST_API_Endpoints', 'getPropertyGroup'],
'permission_callback' => ['IDXBoost_REST_API_Endpoints', 'loginJWT']
));
register_rest_route($dns_api_rest_name_version, self::API_ADD_PROPERTY_GROUP, array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => ['IDXBoost_REST_API_Endpoints', 'addPropertyGroup'],
'permission_callback' => ['IDXBoost_REST_API_Endpoints', 'loginJWT']
));
register_rest_route($dns_api_rest_name_version, self::API_UPDATE_PROPERTY_GROUP, array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => ['IDXBoost_REST_API_Endpoints', 'updatePropertyGroup'],
'permission_callback' => ['IDXBoost_REST_API_Endpoints', 'loginJWT']
));
register_rest_route($dns_api_rest_name_version, self::API_UPDATE_PROPERTY_GROUP_PARENT, array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => ['IDXBoost_REST_API_Endpoints', 'updatePropertyGroupParent'],
'permission_callback' => ['IDXBoost_REST_API_Endpoints', 'loginJWT']
));
register_rest_route($dns_api_rest_name_version, self::API_DELETE_PROPERTY_GROUP, array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => ['IDXBoost_REST_API_Endpoints', 'deletePropertyGroup'],
'permission_callback' => ['IDXBoost_REST_API_Endpoints', 'loginJWT']
));
register_rest_route($dns_api_rest_name_version, self::API_GET_CATEGORIES, array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => ['IDXBoost_REST_API_Endpoints', 'getCategories'],
'permission_callback' => ['IDXBoost_REST_API_Endpoints', 'loginJWT']
));
register_rest_route($dns_api_rest_name_version, self::API_ADD_PAGES, array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => ['IDXBoost_REST_API_Endpoints', 'addPage'],
'permission_callback' => ['IDXBoost_REST_API_Endpoints', 'loginJWT']
));
register_rest_route($dns_api_rest_name_version, self::API_EDIT_PAGES, array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => ['IDXBoost_REST_API_Endpoints', 'editPage'],
'permission_callback' => ['IDXBoost_REST_API_Endpoints', 'loginJWT']
));
register_rest_route($dns_api_rest_name_version, self::API_DELETE_PAGES, array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => ['IDXBoost_REST_API_Endpoints', 'deletePage'],
'permission_callback' => ['IDXBoost_REST_API_Endpoints', 'loginJWT']
));
register_rest_route($dns_api_rest_name_version, self::API_REPLACE_FAVICON, array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => ['IDXBoost_REST_API_Endpoints', 'replaceFavicon'],
'permission_callback' => ['IDXBoost_REST_API_Endpoints', 'loginJWT']
));
register_rest_route($dns_api_rest_name_version, self::API_REPLACE_URL_SITE, array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => ['IDXBoost_REST_API_Endpoints', 'replaceUrlSite'],
'permission_callback' => ['IDXBoost_REST_API_Endpoints', 'loginJWT']
));
}
public static function loginJWT(WP_REST_Request $request)
{
$token = $_POST['token'];
try {
$publicKey = <<= $issuedAt->getTimestamp()) {
return true;
}
} catch (Exception $ex) {
return false;
}
return false;
}
public static function getCategories(WP_REST_Request $request)
{
$reg_key = $_POST['reg-key'];
$response = [];
if (!$reg_key) {
$response = [
'status' => '400',
'message' => 'Bad Request',
'data' => []
];
} else {
if (get_option('idxboost_registration_key') != $reg_key) {
$response = [
'status' => '403',
'message' => 'Forbidden',
'data' => []
];
} else {
$categories = get_categories(array(
'orderby' => 'name',
'order' => 'ASC'
));
$response = [
'status' => '200',
'message' => 'Ok',
'data' => $categories
];
}
}
return new WP_REST_Response($response);
}
public static function getPost(WP_REST_Request $request)
{
$reg_key = $_POST['reg-key'];
$response = [];
if (!$reg_key) {
$response = [
'status' => '400',
'message' => 'Bad Request',
'data' => []
];
} else {
if (get_option('idxboost_registration_key') != $reg_key) {
$response = [
'status' => '403',
'message' => 'Forbidden',
'data' => []
];
} else {
$args = array(
'post_status' => array('publish'),
);
if (isset($_POST['category'])) {
$args['category'] = ($_POST['category']);
}
$args['numberposts'] = 10;
if (isset($_POST['size'])) {
$args['numberposts'] = ($_POST['size']);
}
$posts = get_posts($args);
$response_posts = array();
foreach ($posts as $post) {
$excerpt = get_the_excerpt($post);
$excerpt = $excerpt != '' ? $excerpt : mb_strimwidth(wp_trim_excerpt('', $post), 0, 100, '...');
$excerpt = str_replace('[…]', '...', $excerpt);
$response_posts[] = array(
'id' => $post->ID,
'post_title' => $post->post_title,
'post_excerpt' => $excerpt,
'post_date' => $post->post_date,
'post_content' => $post->post_content,
'permalink' => get_permalink($post),
'image' => get_the_post_thumbnail_url($post),
);
}
$response = [
'status' => '200',
'message' => 'OK',
'data' => $response_posts
];
}
}
return new WP_REST_Response($response);
}
public static function addRegKey(WP_REST_Request $request)
{
$reg_key = $_POST['reg-key'];
$install_url = $_POST['install-url'];
if (!$reg_key || !$install_url) {
$response = [
'status' => '400',
'message' => 'Bad Request'
];
} else {
if (get_option('siteurl') != $install_url) {
$response = [
'status' => '404',
'message' => 'Website url not found'
];
} else {
if (get_option('idxboost_registration_key') == '') {
update_option('idxboost_registration_key', $reg_key);
$response = [
'status' => '200',
'message' => 'OK'
];
} else {
$response = [
'status' => '406',
'message' => 'Not Acceptable'
];
}
}
}
return new WP_REST_Response($response);
}
public static function addPage(WP_REST_Request $request)
{
$reg_key = $_POST['reg_key'];
if (!$reg_key) {
$response = [
'status' => '400',
'message' => 'Bad Request'
];
} else {
if (get_option('idxboost_registration_key') != $reg_key) {
$response = [
'status' => '403',
'message' => 'Forbidden',
'data' => []
];
} else {
$my_post = array(
'post_title' => $_POST['post_title'],
'post_content' => '',
'post_status' => 'publish',
'post_author' => 1,
'post_type' => 'page'
);
$postId = wp_insert_post($my_post);
$post = get_post($postId);
add_post_meta($postId, 'idx_page_type', $_POST['page_type']);
add_post_meta($postId, 'idx_page_id', $_POST['page_id']);
$response = [
'status' => '200',
'message' => 'OK',
'data' => ['post_id' => $postId, 'post_name' => $post->post_name, 'permalink' => get_permalink($post->ID)]
];
}
}
return new WP_REST_Response($response);
}
public static function editPage(WP_REST_Request $request)
{
$reg_key = $_POST['reg_key'];
if (!$reg_key) {
$response = [
'status' => '400',
'message' => 'Bad Request'
];
} else {
if (get_option('idxboost_registration_key') != $reg_key) {
$response = [
'status' => '403',
'message' => 'Forbidden',
'data' => []
];
} else {
$my_post = array(
'ID' => $_POST['post_id'],
'post_title' => $_POST['post_title']
);
wp_update_post($my_post);
$post = get_post($_POST['post_id']);
$response = [
'status' => '200',
'message' => 'OK',
'data' => ['post_id' => $_POST['post_id'], 'post_name' => $post->post_name, 'permalink' => get_permalink($post->ID)]
];
}
}
return new WP_REST_Response($response);
}
public static function deletePage(WP_REST_Request $request)
{
$reg_key = $_POST['reg_key'];
if (!$reg_key) {
$response = [
'status' => '400',
'message' => 'Bad Request'
];
} else {
if (get_option('idxboost_registration_key') != $reg_key) {
$response = [
'status' => '403',
'message' => 'Forbidden',
'data' => []
];
} else {
wp_delete_post($_POST['post_id'], true);
$response = [
'status' => '200',
'message' => 'OK',
];
}
}
return new WP_REST_Response($response);
}
public static function createUser(WP_REST_Request $request)
{
$email_address = filter_input(INPUT_POST, 'email_address', FILTER_SANITIZE_STRING);
$ib_blogname = filter_input(INPUT_POST, 'ib_blogname', FILTER_SANITIZE_STRING);
$ib_blogdescription = filter_input(INPUT_POST, 'ib_blogdescription', FILTER_SANITIZE_STRING);
$ib_registration_key = filter_input(INPUT_POST, 'ib_registration_key', FILTER_SANITIZE_STRING);
$ib_agent_info = isset($_POST['ib_agent_info']) ? $_POST['ib_agent_info'] : '';
$ib_pusher_settings = isset($_POST['ib_pusher_settings']) ? $_POST['ib_pusher_settings'] : '';
$ib_search_settings = isset($_POST['ib_search_settings']) ? $_POST['ib_search_settings'] : '';
$ib_admin_email = $email_address;
if (false === is_email($email_address)) {
$response = [
'error' => 'email_not_valid',
'message' => 'The email parameter has no a valid format.'
];
return new WP_REST_Response($response);
}
if (false === username_exists($email_address)) {
$password = wp_generate_password(20, true, true);
$user_id = wp_create_user($email_address, $password, $email_address);
if (is_wp_error($user_id)) {
$response = [
'error' => $user_id->get_error_codes(),
'message' => $user_id->get_error_messages()
];
} else {
$user = new WP_User($user_id);
$user->set_role('administrator');
$response = [
'user_id' => $user_id,
'username' => $email_address,
'password' => $password
];
}
update_option('blogname', $ib_blogname);
update_option('blogdescription', $ib_blogdescription);
update_option('admin_email', $ib_admin_email);
update_option('idxboost_registration_key', $ib_registration_key);
update_option('idxboost_agent_info', $ib_agent_info);
update_option('idxboost_pusher_settings', $ib_pusher_settings);
update_option('idxboost_search_settings', $ib_search_settings);
// flush access token
delete_transient('flex_api_access_token');
flex_idx_get_access_token();
return new WP_REST_Response($response);
} else {
$response = [
'error_code' => 'existing_user_login',
'error_message' => 'Sorry, that username already exists!'
];
return new WP_REST_Response($response);
}
}
public static function replaceFavicon(WP_REST_Request $request)
{
$reg_key = $_POST['reg_key'];
if (!$reg_key) {
$response = [
'status' => '400',
'message' => 'Bad Request',
'data' => []
];
} else {
$response = [
'status' => '200',
'message' => 'OK',
'data' => []
];
$favicon = $_POST['favicon'];
if ($favicon == '') {
$favicon = get_option('favicon');
$file = str_replace('/wp-content/themes', '', get_theme_root()) . '/' . $favicon;
unlink($file);
} else {
$favicon_old = get_option('favicon');
if ($favicon_old && basename($favicon) != $favicon) {
$file = str_replace('/wp-content/themes', '', get_theme_root()) . '/' . $favicon_old;
unlink($file);
}
$favicon = str_replace('\\', '', $favicon);
update_option('favicon', basename($favicon));
file_put_contents(str_replace('/wp-content/themes', '', get_theme_root()) . '/' . basename($favicon), file_get_contents($favicon));
}
}
return new WP_REST_Response($response);
}
public static function getPropertyGroup(WP_REST_Request $request)
{
$slug = sanitize_text_field($request->get_param('slug'));
if (!$slug) {
return new WP_REST_Response([
'status' => 400,
'message' => 'Slug is required',
'data' => []
]);
}
$query = new WP_Query([
'name' => $slug,
'post_type' => 'flex-idx-pages',
'post_status' => 'publish',
'posts_per_page' => 1
]);
if (!$query->have_posts()) {
return new WP_REST_Response([
'status' => 404,
'message' => 'Not found',
'data' => []
]);
}
$post = $query->posts[0];
return new WP_REST_Response([
'status' => 200,
'message' => 'Post found',
'data' => [
'post_id' => $post->ID,
'post_title' => $post->post_title,
'post_name' => $post->post_name,
'permalink' => get_permalink($post->ID),
'content' => $post->post_content
]
]);
}
public static function addPropertyGroup(WP_REST_Request $request)
{
$reg_key = sanitize_text_field($_POST['reg_key']);
$post_title = sanitize_text_field($_POST['post_title']);
$group_id = sanitize_text_field($_POST['group_id']);
$post_name = !empty($_POST['post_name'])
? sanitize_title($_POST['post_name'])
: sanitize_title($post_title);
if (!$reg_key || !$post_title) {
return new WP_REST_Response([
'status' => 400,
'message' => 'Bad Request',
'data' => []
]);
}
if (get_option('idxboost_registration_key') != $reg_key) {
return new WP_REST_Response([
'status' => '403',
'message' => 'Forbidden',
'data' => []
]);
}
$current_user_id = get_current_user_id();
$post_status = 'publish';
$post_type = 'flex-idx-pages';
$post_id = wp_insert_post(array(
'post_title' => $post_title,
'post_name' => $post_name,
'post_content' => '[list_property_collection column="two" group_id="' . $group_id . '"]',
'post_status' => $post_status,
'post_author' => $current_user_id,
'post_type' => $post_type
));
update_post_meta($post_id, '_flex_id_page', 'flex_idx_page_our_property_collection');
add_post_meta($post_id, 'property_collection_group_id', $group_id);
if (is_wp_error($post_id)) {
return new WP_REST_Response([
'status' => 500,
'message' => 'Some error was occurred while creating the post',
'data' => $post_id->get_error_messages()
]);
}
// Obtener permalink
$final_post = get_post($post_id);
$final_slug = $final_post->post_name;
$permalink = get_permalink($post_id);
return new WP_REST_Response([
'status' => 200,
'message' => 'Post successfully created',
'data' => [
'post_id' => $post_id,
'post_name' => $final_slug,
'permalink' => $permalink
]
]);
}
public static function updatePropertyGroup(WP_REST_Request $request)
{
$reg_key = sanitize_text_field($_POST['reg_key']);
$post_id = intval($_POST['post_id']);
$post_title = sanitize_text_field($_POST['post_title']);
$group_id = sanitize_text_field($_POST['group_id']);
$post_name = !empty($_POST['post_name'])
? sanitize_title($_POST['post_name'])
: sanitize_title($post_title);
if (!$post_id || !$post_title) {
return new WP_REST_Response([
'status' => 400,
'message' => 'Bad Request',
'data' => []
]);
}
if (get_option('idxboost_registration_key') != $reg_key) {
return new WP_REST_Response([
'status' => '403',
'message' => 'Forbidden',
'data' => []
]);
}
$post = get_post($post_id);
if (!$post) {
return [
'status' => 404,
'message' => 'Post no encontrado',
'data' => []
];
}
// Actualizar post
$updated_post = [
'ID' => $post_id,
'post_title' => $post_title,
'post_name' => $post_name
];
$result = wp_update_post($updated_post, true);
update_post_meta($post_id, 'property_collection_group_id', $group_id);
if (is_wp_error($result)) {
return new WP_REST_Response([
'status' => 500,
'message' => 'Some error was occurred while creating the post',
'data' => $result->get_error_messages()
]);
}
// Obtener permalink actualizado
$final_post = get_post($post_id);
$final_slug = $final_post->post_name;
$permalink = get_permalink($post_id);
return new WP_REST_Response([
'status' => 200,
'message' => 'Post successfully updated',
'data' => [
'post_id' => $post_id,
'post_name' => $final_slug,
'permalink' => $permalink
]
]);
}
public static function deletePropertyGroup(WP_REST_Request $request)
{
$reg_key = sanitize_text_field($_POST['reg_key']);
$post_id = intval($_POST['post_id']);
if (!$post_id) {
return new WP_REST_Response([
'status' => 400,
'message' => 'Bad Request',
'data' => []
], 400);
}
// Validar API key
if (get_option('idxboost_registration_key') != $reg_key) {
return new WP_REST_Response([
'status' => 403,
'message' => 'Forbidden',
'data' => []
], 403);
}
// Verificar que el post exista
$post = get_post($post_id);
if (!$post) {
return new WP_REST_Response([
'status' => 404,
'message' => 'Post not found',
'data' => []
], 404);
}
// Eliminar post
$deleted = wp_delete_post($post_id, true); // true = eliminar permanentemente
if (!$deleted) {
return new WP_REST_Response([
'status' => 500,
'message' => 'Error deleting the post',
'data' => []
], 500);
}
return new WP_REST_Response([
'status' => 200,
'message' => 'Post successfully deleted',
'data' => [
'post_id' => $post_id
]
], 200);
}
public static function updatePropertyGroupParent(WP_REST_Request $request)
{
$reg_key = sanitize_text_field($_POST['reg_key']);
$post_title = sanitize_text_field($_POST['post_title']);
$post_name = !empty($_POST['post_name'])
? sanitize_title($_POST['post_name'])
: sanitize_title($post_title);
if (!$post_title || !$post_name) {
return new WP_REST_Response([
'status' => 400,
'message' => 'Bad Request',
'data' => []
]);
}
if (get_option('idxboost_registration_key') != $reg_key) {
return new WP_REST_Response([
'status' => '403',
'message' => 'Forbidden',
'data' => []
]);
}
$collection = get_posts([
'post_type' => 'flex-idx-pages',
'meta_query' => [
[
'key' => '_flex_id_page',
'value' => 'flex_idx_page_our_property_collection',
],
],
'posts_per_page' => 1,
'order' => 'ASC',
]);
if (empty($collection)) {
return new WP_REST_Response([
'status' => 404,
'message' => 'Not found',
'data' => []
]);
}
$updated_post = [
'ID' => $collection[0]->ID,
'post_title' => $post_title,
'post_name' => $post_name
];
$result = wp_update_post($updated_post, true);
if (is_wp_error($result)) {
return new WP_REST_Response([
'status' => 500,
'message' => 'Some error was occurred while updating the post',
'data' => $result->get_error_messages()
]);
}
// Obtener permalink actualizado
$final_post = get_post($collection[0]->ID);
$final_slug = $final_post->post_name;
$permalink = get_permalink($collection[0]->ID);
return new WP_REST_Response([
'status' => 200,
'message' => 'Post successfully updated',
'data' => [
'post_id' => $collection[0]->ID,
'post_name' => $final_slug,
'permalink' => $permalink
]
]);
}
public static function replaceUrlSite(WP_REST_Request $request)
{
$reg_key = $_POST['reg_key'];
if (!$reg_key) {
$response = [
'status' => '400',
'message' => 'Bad Request',
'data' => []
];
} else {
try {
$old_url = $_POST['old_url'];
$new_url = $_POST['new_url'];
global $wpdb;
$tables = $wpdb->get_results("SHOW TABLES LIKE '{$wpdb->prefix}%'");
foreach ($tables as $table) {
$table_name = current($table);
$columns = $wpdb->get_results("DESCRIBE {$table_name}");
foreach ($columns as $column) {
$column_name = $column->Field;
$wpdb->query("UPDATE $table_name SET $column_name = REPLACE($column_name, '$old_url', '$new_url')");
}
}
$response = [
'status' => '200',
'message' => 'OK',
'data' => []
];
} catch (Exception $ex) {
$response = [
'status' => '400',
'message' => 'Bad Request',
'data' => []
];
}
}
return new WP_REST_Response($response);
}
}
Page not found - Luxury Living HQ Luxury Living HQ
Oops!
We can't seem to find the page you're looking for.
Go to Home page