wpdb->prepare( " {$field} LIKE %s ", $search_term ); } } if ( ! empty( $where_search ) ) { $where .= ' AND (' . implode( ' OR ', $where_search ) . ')'; } $join_lookup = ''; $filter_lookup = ! empty( $filter_site_ids ) || ! empty( $filter_client_ids ) ? true : false; if ( $filter_lookup ) { $lookup_table = '`' . esc_sql( $this->table_name( 'lookup_item_objects' ) ) . '`'; $join_lookup = " JOIN {$lookup_table} lco ON co.id = lco.item_id AND lco.item_name = 'cost' "; $join_and = array(); $clients_sites_ids = array(); if ( $filter_client_ids ) { $safe_client_ids = array_map( 'intval', $filter_client_ids ); $join_and[] = ' lco.object_name = "client" AND lco.object_id IN (' . implode( ',', $safe_client_ids ) . ') '; // if filter by clients need to get sites of the clients to search in lookup table. $cli_sites = MainWP_DB_Client::instance()->get_websites_by_client_ids( $filter_client_ids ); if ( $cli_sites ) { foreach ( $cli_sites as $cli_site ) { $clients_sites_ids[] = $cli_site->id; } } } $filter_site_ids = array_unique( array_merge( $filter_site_ids, $clients_sites_ids ) ); if ( $filter_site_ids ) { $safe_site_ids = array_map( 'intval', $filter_site_ids ); $join_and[] = ' lco.object_name = "site" AND lco.object_id IN (' . implode( ',', $safe_site_ids ) . ') '; } if ( ! empty( $join_and ) ) { if ( 1 === count( $join_and ) ) { $join_lookup .= ' AND ' . $join_and[0]; } elseif ( 2 === count( $join_and ) ) { $join_lookup .= ' AND ( ( ' . $join_and[0] . ' ) OR ( ' . $join_and[1] . ' ) )'; } else { $join_lookup = ''; } } else { $join_lookup = ''; } } if ( ! empty( $filter_prod_type_slugs ) && is_array( $filter_prod_type_slugs ) ) { $filter_prod_type_slugs = MainWP_DB::instance()->escape_array( $filter_prod_type_slugs ); if ( ! empty( $filter_prod_type_slugs ) ) { $safe_prod_types = array_map( 'esc_sql', $filter_prod_type_slugs ); $where .= ' AND co.product_type IN ("' . implode( '","', $safe_prod_types ) . '") '; } } if ( ! empty( $filter_cost_state ) && is_array( $filter_cost_state ) ) { $filter_cost_state = MainWP_DB::instance()->escape_array( $filter_cost_state ); if ( ! empty( $filter_cost_state ) ) { $safe_cost_states = array_map( 'esc_sql', $filter_cost_state ); $where .= ' AND co.cost_status IN ("' . implode( '","', $safe_cost_states ) . '") '; } } $filter_next_renewal = false; if ( ! empty( $filter_dtsstart ) && is_numeric( $filter_dtsstart ) && ! empty( $filter_dtsstop ) && is_numeric( $filter_dtsstop ) && $filter_dtsstart < $filter_dtsstop ) { $filter_next_renewal = true; } if ( $filter_next_renewal ) { $stop_start = $filter_dtsstop - $filter_dtsstart; $where .= ' AND cost_status = "active" AND type = "subscription" AND ( CASE '; $where .= ' WHEN renewal_type = "weekly" AND ( ( next_renewal >= ' . intval( $filter_dtsstart ) . ' AND ( next_renewal <= ' . intval( $filter_dtsstop ) . ' ) ) OR ( next_renewal < ' . intval( $filter_dtsstart ) . ' AND ( MOD( ' . intval( $filter_dtsstop ) . ' - next_renewal, ' . intval( WEEK_IN_SECONDS ) . ' ) < ' . $stop_start . ' ) ) ) THEN true '; $where .= ' WHEN renewal_type = "monthly" AND ( ( next_renewal >= ' . intval( $filter_dtsstart ) . ' AND ( next_renewal <= ' . intval( $filter_dtsstop ) . ' ) ) OR ( next_renewal < ' . intval( $filter_dtsstart ) . ' AND ( MOD( ' . intval( $filter_dtsstop ) . ' - next_renewal, ' . intval( MONTH_IN_SECONDS ) . ' ) < ' . $stop_start . ' ) ) ) THEN true '; $where .= ' WHEN renewal_type = "yearly" AND ( ( next_renewal >= ' . intval( $filter_dtsstart ) . ' AND ( next_renewal <= ' . intval( $filter_dtsstop ) . ' ) ) OR ( next_renewal < ' . intval( $filter_dtsstart ) . ' AND ( MOD( ' . intval( $filter_dtsstop ) . ' - next_renewal, ' . intval( YEAR_IN_SECONDS ) . ' ) < ' . $stop_start . ' ) ) ) THEN true '; $where .= ' WHEN renewal_type = "quarterly" AND ( ( next_renewal >= ' . intval( $filter_dtsstart ) . ' AND ( next_renewal <= ' . intval( $filter_dtsstop ) . ' ) ) OR ( next_renewal < ' . intval( $filter_dtsstart ) . ' AND ( MOD( ' . intval( $filter_dtsstop ) . ' - next_renewal, ' . 3 * intval( MONTH_IN_SECONDS ) . ' ) < ' . $stop_start . ' ) ) ) THEN true '; $where .= ' ELSE false END ) '; } if ( ! empty( $filter_license_type ) && is_array( $filter_license_type ) ) { $filter_license_type = MainWP_DB::instance()->escape_array( $filter_license_type ); if ( ! empty( $filter_license_type ) ) { $safe_license_types = array_map( 'esc_sql', $filter_license_type ); $where .= ' AND co.license_type IN ("' . implode( '","', $safe_license_types ) . '") '; } } if ( ! empty( $filter_payment_method ) && is_array( $filter_payment_method ) ) { $filter_payment_method = MainWP_DB::instance()->escape_array( $filter_payment_method ); if ( ! empty( $filter_payment_method ) ) { $safe_payment_methods = array_map( 'esc_sql', $filter_payment_method ); $where .= ' AND co.payment_method IN ("' . implode( '","', $safe_payment_methods ) . '") '; } } if ( ! empty( $filter_renewal_type ) && is_array( $filter_renewal_type ) ) { $filter_renewal_type = MainWP_DB::instance()->escape_array( $filter_renewal_type ); if ( ! empty( $filter_renewal_type ) ) { $safe_renewal_types = array_map( 'esc_sql', $filter_renewal_type ); $where .= ' AND co.renewal_type IN ("' . implode( '","', $safe_renewal_types ) . '") '; } } /** * PARSE PAGINATION PARAMS */ $limits = ''; $start = isset( $args['start'] ) ? absint( $args['start'] ) : 0; $per_page = isset( $args['records_per_page'] ) ? absint( $args['records_per_page'] ) : 99999; if ( $per_page >= 0 ) { $limits = "LIMIT {$start}, {$per_page}"; } // Default to sorting by record ID. $orderby = 'co.id'; $orderable = array( 'name', 'url', 'type', 'product_type', 'slug', 'license_type', 'cost_status', 'payment_method', 'price', 'renewal_type', 'last_renewal', 'next_renewal' ); if ( isset( $args['orderby'] ) && in_array( $args['orderby'], $orderable, true ) ) { $_orderby = 'next_renewal' === $args['orderby'] ? 'next_renewal_today' : $args['orderby']; $prefix = 'co.'; $orderby = sprintf( '%s%s', $prefix, $_orderby ); } // Show the recent records first by default. $order = 'DESC'; if ( isset( $args['order'] ) && 'ASC' === strtoupper( $args['order'] ) ) { $order = 'ASC'; } $orderby = sprintf( 'ORDER BY %s %s', $orderby, $order ); $selects = array(); $selects[] = ' DISTINCT(co.id) dist_id '; // need at first. $selects[] = ' co.* '; $select = implode( ', ', $selects ); /** * BUILD THE FINAL QUERY */ $cost_table = '`' . esc_sql( $this->table_name( 'cost_tracker' ) ) . '`'; $query = "SELECT {$select} FROM {$cost_table} as co " . $join_lookup . " WHERE 1 {$where} {$orderby} {$limits}"; // Build result count query. $count_query = "SELECT COUNT(DISTINCT(co.id)) as found FROM {$cost_table} as co " . $join_lookup . " WHERE 1 {$where}"; /** * QUERY THE DATABASE FOR RESULTS */ $items = $this->wpdb->get_results( $query ); // phpcs:ignore -- ok. $total = absint( $this->wpdb->get_var( $count_query ) ); // phpcs:ignore -- ok. return array( 'items' => $items, 'count' => $total, ); } }