ID); return new WP_REST_Response( $properties, 200 ); } function bc_select_properties_for_user($userId){ global $wpdb; $sql = $wpdb->prepare("SELECT * FROM wp_rental_properties WHERE user_id = %d;", $userId); return $wpdb->get_results($sql, OBJECT); } function bc_select_property($userId, $propertyId){ global $wpdb; $sql = $wpdb->prepare("SELECT * FROM wp_rental_properties WHERE user_id = %d AND id = %d;", $userId, $propertyId); return $wpdb->get_row($sql); } function bc_update_property( WP_REST_Request $request ) { global $wpdb; global $current_user; $response = new WP_REST_Response(); $userId = $current_user->ID; $table_name = $wpdb->prefix."rental_properties"; $data = $wpdb->update($table_name, array( 'rental_name' => $request['rental_name'], 'address1' => $request['address1'], 'address2' => $request['address2'], 'city' => $request['city'], 'state' => $request['state'], 'zip' => $request['zip'] ), array( 'id' => $request['id'], 'user_id' => $userId, )); if(!empty($request['redirect'])){ $response->set_status( 302 ); $response->header('Location', $request['redirect']); return $response; } $response->data = $data; $response->set_status( 201 ); return $response; } function bc_create_property( WP_REST_Request $request ) { global $wpdb; global $current_user; $response = new WP_REST_Response(); if(!bc_user_can_add_property($current_user->ID)) { $response->set_status(400); $response->data = array( 'error' => 'Subscription plan unit count exceeded.' ); return $response; } $userId = $current_user->ID; $table_name = $wpdb->prefix."rental_properties"; $data = $wpdb->insert($table_name, array( 'user_id' => $userId, 'rental_name' => $request['rental_name'], 'address1' => $request['address1'], 'address2' => $request['address2'], 'city' => $request['city'], 'state' => $request['state'], 'zip' => $request['zip'] )); if(!empty($request['redirect'])){ $response->set_status( 302 ); $response->header('Location', $request['redirect']); return $response; } $response->data = $data; $response->set_status( 201 ); return $response; } function bc_delete_property( WP_REST_Request $request ) { global $wpdb; global $current_user; $response = new WP_REST_Response(); $userId = $current_user->ID; $table_name = $wpdb->prefix."rental_properties"; $data = $wpdb->delete($table_name, array( 'id' => $request['id'], 'user_id' => $userId )); $response->data = $data; $response->set_status(201); return $response; } function bc_user_can_add_property($userId){ return true; // temporary allow any number of props $planCount = bc_get_plan_count_for_user($userId); $unitCount = bc_get_unit_count_for_user($userId); return $unitCount < $planCount; } function bc_get_subscription_for_user($userId){ global $wpdb; $metaKey = '_stripe_customer_id'; $sql = $wpdb->prepare(" SELECT s.* FROM wp_fullstripe_subscribers s JOIN $wpdb->usermeta um ON um.meta_value = s.stripeCustomerID AND um.user_id = %d AND um.meta_key = %s ", $userId, $metaKey); return $wpdb->get_row($sql); } function bc_get_plan_count_for_user($userId){ $subscription = bc_get_subscription_for_user($userId); $planId = $subscription->planID; $planUnitCount = 1; if($planID === ''){ } else if ($planID === ''){ } else if ($planID === ''){ } return $planUnitCount; } function bc_get_unit_count_for_user($userId){ global $wpdb; $sql = $wpdb->prepare("SELECT COUNT(*) FROM wp_rental_properties WHERE user_id = %d", $userId); return $wpdb->get_var($sql); } function bc_get_template( $template ) { $haystack = $template; $needle = 'bauscode'; if ( strpos($haystack, $needle) !== FALSE ) { $page_template = dirname( __FILE__ ) . '/' . $template; } return $page_template; } function bc_rental_shortcode($atts) { ob_start(); include(dirname(__FILE__).'/templates/bauscode-rental-create.php'); $content = ob_get_clean(); return $content; } function bc_plugin_activate(){ bc_init_db_schema(); } function bc_init_db_schema(){ global $wpdb; $table_name = $wpdb->prefix."rental_properties"; if ($wpdb->get_var('SHOW TABLES LIKE '.$table_name) != $table_name) { $sql = 'CREATE TABLE '.$table_name.'( id bigint(20) NOT NULL AUTO_INCREMENT, user_id bigint(20) UNSIGNED NOT NULL, rental_name varchar(50), address1 varchar(50), address2 varchar(50), city varchar(50), state varchar(10), zip varchar(10), PRIMARY KEY (id))'; require_once(ABSPATH.'wp-admin/includes/upgrade.php'); dbDelta($sql); add_option("rental_properties", "1.0"); } } // filters add_shortcode('rental_properties', bc_rental_shortcode); register_activation_hook(__FILE__, bc_plugin_activate); add_action( 'wp_enqueue_scripts', function() { wp_enqueue_script( 'rental_properties', plugin_dir_url( __FILE__ ) . 'rental-properties.js', array( 'jquery' ) ); wp_enqueue_style( 'rental_properties', plugin_dir_url( __FILE__ ) . 'rental-properties.css' ); wp_localize_script('rental_properties', 'RentalProperties', array( 'root' => esc_url_raw( rest_url() ), 'nonce' => wp_create_nonce( 'wp_rest' ), 'current_user_id' => get_current_user_id() ) ); }); // routes add_action( 'rest_api_init', function () { register_rest_route( 'bauscode', '/rental_properties', array( 'methods' => 'GET', 'callback' => 'bc_select_properties', 'args' => array(), 'permission_callback' => function() { return is_user_logged_in(); } ) ); register_rest_route( 'bauscode', '/rental_properties/(?P[\d]+)', array( 'methods' => 'PUT', 'callback' => 'bc_update_property', 'args' => array( 'id' => array(), ), 'permission_callback' => function() { return is_user_logged_in(); } ) ); register_rest_route( 'bauscode', '/rental_properties/(?P[\d]+)/update', array( 'methods' => 'POST', // form-accessible alternate route 'callback' => 'bc_update_property', 'args' => array( 'id' => array(), ), 'permission_callback' => function() { return is_user_logged_in(); } ) ); register_rest_route( 'bauscode', '/rental_properties', array( 'methods' => 'POST', 'callback' => 'bc_create_property', 'args' => array(), 'permission_callback' => function() { return is_user_logged_in(); } ) ); register_rest_route( 'bauscode', '/rental_properties/(?P[\d]+)', array( 'methods' => 'DELETE', 'callback' => 'bc_delete_property', 'args' => array( 'id' => array(), ), 'permission_callback' => function() { return is_user_logged_in(); } ) ); register_rest_route( 'bauscode', '/rental_properties/(?P[\d]+)/delete', array( 'methods' => 'POST', // form-accessible alternate route 'callback' => 'bc_delete_properties', 'args' => array( 'id' => array(), ), 'permission_callback' => function() { return is_user_logged_in(); } ) ); } ); ?>