Skip to content

Commit

Permalink
Simplified sql
Browse files Browse the repository at this point in the history
  • Loading branch information
kodinkat committed Aug 13, 2024
1 parent 85d1b03 commit 1fdfafd
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 30 deletions.
14 changes: 4 additions & 10 deletions dt-mapping/mapping-queries.php
Original file line number Diff line number Diff line change
Expand Up @@ -1305,29 +1305,23 @@ public static function post_type_geojson( $post_type, $args = [], $offset = 0, $
$post_id_filter_sql = '';
$shared_user_join_sql = '';
$shared_user_condition_sql = '';
if ( isset( $args['list_all'], $args['list_all_by_user_id'] ) && !$args['list_all'] ) {
if ( isset( $args['slug'], $args['user_id'] ) && $args['slug'] === 'personal' ) {
$post_id_filter_sql = $wpdb->prepare( "
SELECT * FROM
(
SELECT api_p.ID
FROM $wpdb->posts api_p
LEFT JOIN $wpdb->postmeta as field_type ON ( field_type.post_id = api_p.ID AND field_type.meta_key = 'type' )
WHERE (field_type.meta_value = 'access' OR field_type.meta_value = 'user' OR field_type.meta_value = 'access_placeholder')
AND (api_p.post_status = 'publish')
WHERE (api_p.post_status = 'publish')
AND api_p.post_type = %s
GROUP BY api_p.ID
LIMIT 0, 50000
) AS assigned_post_ids
", ( ( $post_type === 'system-users' ) ? 'contacts' : $post_type ) );

if ( isset( $args['field_type'] ) && $args['field_type'] == 'user_select' ) {
$shared_user_join_sql = "LEFT JOIN $wpdb->dt_share as field_shared_with ON ( field_shared_with.post_id = um.meta_value )";
$shared_user_join_sql = "LEFT JOIN $wpdb->dt_share as field_shared_with ON ( field_shared_with.post_id = um.meta_value )";
$post_id_filter_sql = 'AND (um.meta_value IN ('. $post_id_filter_sql .'))';
} else {
$shared_user_join_sql = "LEFT JOIN $wpdb->dt_share as field_shared_with ON ( field_shared_with.post_id = p.ID )";
$post_id_filter_sql = 'AND (p.ID IN ('. $post_id_filter_sql .'))';
}
$shared_user_condition_sql = "AND (field_shared_with.user_id = ". $args['list_all_by_user_id'] .")";
$shared_user_condition_sql = "AND (field_shared_with.user_id = ". $args['user_id'] .")";
}

if ( isset( $args['field_key'], $args['field_type'] ) && in_array( $args['field_type'], [ 'key_select', 'multi_select' ] ) ){
Expand Down
2 changes: 1 addition & 1 deletion dt-metrics/records/dynamic-records-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jQuery(document).ready(function ($) {

body.offset = offset;
body.limit = limit;
body.list_all = mapbox_library_api.obj.settings.can_list_all;
body.slug = mapbox_library_api.obj.settings.menu_slug;
let query = await window.makeRequest(
'POST',
mapbox_library_api.obj.settings.post_type_rest_url,
Expand Down
26 changes: 7 additions & 19 deletions dt-metrics/records/dynamic-records-map.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class DT_Metrics_Dynamic_Records_Map extends DT_Metrics_Chart_Base
public $slug = 'dynamic_records_map'; // lowercase
public $js_object_name = 'wp_js_object'; // This object will be loaded into the metrics.js file by the wp_localize_script.
public $js_file_name = '/dt-metrics/records/dynamic-records-map.js'; // should be full file name plus extension
public $permissions = [ 'dt_all_access_contacts', 'view_project_metrics', 'multiplier' ];
public $permissions = [ 'dt_all_access_contacts', 'view_project_metrics', 'access_contacts', 'access_groups' ];
public $namespace = 'dt-metrics/records';
public $base_filter = [];

Expand Down Expand Up @@ -107,7 +107,6 @@ public function scripts() {
'map_key' => DT_Mapbox_API::get_key(),
'no_map_key_msg' => _x( 'To view this map, a mapbox key is needed; click here to add.', 'install mapbox key to view map', 'disciple_tools' ),
'map_mirror' => dt_get_location_grid_mirror( true ),
'can_list_all' => in_array( $this->base_slug, [ 'records' ] ),
'menu_slug' => $this->base_slug,
'post_type' => $this->post_type,
'post_types' => $this->post_type_options,
Expand Down Expand Up @@ -190,14 +189,16 @@ public function post_type_geojson( WP_REST_Request $request ){
break;
}

// Determine if record restriction (listing-all) should take effect.
$params = self::capture_list_all_setting( $params );
// Determine type of query to be executed, based on incoming slug.
if ( isset( $params['slug'] ) && $params['slug'] === 'personal' ) {
$params['user_id'] = get_current_user_id();
}

// Execute request query.
$response = Disciple_Tools_Mapping_Queries::post_type_geojson( $params['post_type'], $params, $offset, $limit );

// Ensure to unset list_all_by_user_id for security reasons.
unset( $params['list_all_by_user_id'] );
// Ensure to unset user_id for security reasons.
unset( $params['user_id'] );
}

return [
Expand Down Expand Up @@ -267,17 +268,4 @@ public function points_geojson( WP_REST_Request $request ) {

return Disciple_Tools_Mapping_Queries::points_geojson( $post_type, $query );
}

private function capture_list_all_setting( $params ) {
if ( !isset( $params['list_all'] ) || !$params['list_all'] ) {
$params['list_all'] = false;
$params['list_all_by_user_id'] = get_current_user_id();
} else {
$params['list_all'] = true;
$params['list_all_by_user_id'] = null;
}

return $params;
}

}

0 comments on commit 1fdfafd

Please sign in to comment.