def = $def; $this->req = self::normalise_request( $request, $def ); } public static function normalise_request( $r, $def ) { $r = (array) $r; $per_page = isset( $r['per_page'] ) ? (int) $r['per_page'] : 0; if ( $per_page <= 0 ) { $per_page = (int) $def->get( 'per_page', 0 ); } if ( $per_page <= 0 && class_exists( 'AIEO_Table_Registry' ) ) { $rid = (string) $def->get( 'registry_id', '' ); $per_page = $rid && AIEO_Table_Registry::get( $rid ) ? AIEO_Table_Registry::per_page( $rid ) : AIEO_Table_Registry::site_defaults()['per_page']; } $per_page = max( self::PER_PAGE_MIN, min( self::PER_PAGE_MAX, $per_page ?: 25 ) ); $sort = isset( $r['sort'] ) ? (string) $r['sort'] : ''; $dir_given = isset( $r['dir'] ) && '' !== trim( (string) $r['dir'] ); $dir = ( $dir_given && 'desc' === strtolower( trim( (string) $r['dir'] ) ) ) ? 'DESC' : 'ASC'; if ( '' !== $sort && ! in_array( $sort, $def->sortable_keys(), true ) ) { $sort = $def->sort_for_column( $sort ); } if ( '' !== $sort && ! in_array( $sort, $def->sortable_keys(), true ) ) { $sort = ''; } if ( '' === $sort ) { $d = $def->default_sort(); if ( $d ) { $sort = $d[0]; if ( ! $dir_given ) { $dir = $d[1]; } } } return array( 'page' => max( 1, isset( $r['page'] ) ? (int) $r['page'] : 1 ), 'per_page' => $per_page, 'sort' => $sort, 'dir' => $dir, 'search' => isset( $r['search'] ) ? trim( (string) $r['search'] ) : '', 'params' => $def->apply_param_defaults( isset( $r['params'] ) && is_array( $r['params'] ) ? $r['params'] : array() ), 'cursor' => isset( $r['cursor'] ) ? (string) $r['cursor'] : '', 'keys' => isset( $r['keys'] ) && is_array( $r['keys'] ) ? array_slice( array_values( array_filter( array_map( 'strval', $r['keys'] ), 'strlen' ) ), 0, 5000 ) : array(), ); } public function run() { if ( ! $this->def->is_valid() ) { return array( 'error' => 'invalid_definition', 'errors' => $this->def->errors(), ); } return ( 'rows' === $this->def->source() ) ? $this->run_callable() : $this->run_sql(); } private function run_callable() { $rows = call_user_func( $this->def->get( 'rows' ), $this->req['params'] ); $rows = is_array( $rows ) ? array_values( $rows ) : array(); $total = count( $rows ); $rows = $this->filter_rows_in_php( $rows ); $rows = $this->sort_rows_in_php( $rows ); $filtered = count( $rows ); $this->clamp_page( $filtered ); $offset = ( $this->req['page'] - 1 ) * $this->req['per_page']; $page = $this->decorate_rows( array_slice( $rows, $offset, $this->req['per_page'] ) ); return array( 'columns' => $this->public_columns(), 'rows' => $this->shape_rows( $page ), 'rows_assoc' => $page, 'totals' => $this->aggregate_in_php( $rows ), 'meta' => $this->meta( $total, $filtered ), ); } private function run_sql() { global $wpdb; $from = $this->from_clause(); if ( '' === $from ) { return array( 'error' => 'no_source' ); } $this->where = array(); $this->apply_base_where(); $base_only = $this->where; $this->apply_params(); $this->apply_search(); $base_sql = $base_only ? ' WHERE ' . implode( ' AND ', $base_only ) : ''; $where_sql = $this->where ? ' WHERE ' . implode( ' AND ', $this->where ) : ''; $total = (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$from}{$base_sql}" ); $filtered = ( $where_sql === $base_sql ) ? $total : (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$from}{$where_sql}" ); $this->clamp_page( $filtered ); $order = $this->order_clause(); $offset = ( $this->req['page'] - 1 ) * $this->req['per_page']; $select = $this->select_list(); $sql = $wpdb->prepare( "SELECT {$select} FROM {$from}{$where_sql}{$order} LIMIT %d OFFSET %d", $this->req['per_page'], $offset ); $rows = $wpdb->get_results( $sql, ARRAY_A ); $rows = is_array( $rows ) ? $rows : array(); $rows = $this->decorate_rows( $rows ); return array( 'columns' => $this->public_columns(), 'rows' => $this->shape_rows( $rows ), 'rows_assoc' => $rows, 'totals' => $this->aggregates_in_sql( $from, $where_sql ), 'meta' => $this->meta( $total, $filtered ), ); } public function row_keys( $after = '', $limit = 500 ) { global $wpdb; if ( ! $this->def->is_valid() ) { return null; } $key = $this->def->row_key(); if ( '' === $key ) { return null; } $limit = max( 1, (int) $limit ); if ( 'rows' === $this->def->source() ) { $rows = call_user_func( $this->def->get( 'rows' ), $this->req['params'] ); $rows = is_array( $rows ) ? array_values( $rows ) : array(); $rows = $this->filter_rows_in_php( $rows ); $keys = array(); foreach ( $rows as $row ) { $v = (string) AIEO_Table_Columns::value( $row, $key, '' ); if ( '' !== $v ) { $keys[] = $v; } } sort( $keys, SORT_STRING ); if ( '' !== (string) $after ) { $keys = array_values( array_filter( $keys, static function ( $v ) use ( $after ) { return strcmp( $v, (string) $after ) > 0; } ) ); } return array_slice( $keys, 0, $limit ); } $from = $this->from_clause(); if ( '' === $from ) { return null; } $this->where = array(); $this->apply_base_where(); $this->apply_params(); $this->apply_search(); $expr = $this->select_expression_for( $key ); if ( '' !== (string) $after ) { $this->where[] = $wpdb->prepare( "{$expr} > %s", (string) $after ); } $where_sql = $this->where ? ' WHERE ' . implode( ' AND ', $this->where ) : ''; $sql = $wpdb->prepare( "SELECT {$expr} FROM {$from}{$where_sql} ORDER BY {$expr} ASC LIMIT %d", $limit ); $keys = $wpdb->get_col( $sql ); return array_map( 'strval', is_array( $keys ) ? $keys : array() ); } public function count_filtered() { global $wpdb; if ( ! $this->def->is_valid() ) { return 0; } if ( 'rows' === $this->def->source() ) { $rows = call_user_func( $this->def->get( 'rows' ), $this->req['params'] ); $rows = is_array( $rows ) ? array_values( $rows ) : array(); return count( $this->filter_rows_in_php( $rows ) ); } $from = $this->from_clause(); if ( '' === $from ) { return 0; } $this->where = array(); $this->apply_base_where(); $this->apply_params(); $this->apply_search(); $where_sql = $this->where ? ' WHERE ' . implode( ' AND ', $this->where ) : ''; return (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$from}{$where_sql}" ); } public function figures() { global $wpdb; $select = trim( (string) ( $this->def->get( 'figures', array() )['select'] ?? '' ) ); if ( '' === $select || ! $this->def->is_valid() || 'rows' === $this->def->source() ) { return null; } $from = $this->from_clause(); if ( '' === $from ) { return null; } $this->where = array(); $this->apply_base_where(); $this->apply_params(); $this->apply_search(); $where_sql = $this->where ? ' WHERE ' . implode( ' AND ', $this->where ) : ''; $row = $wpdb->get_row( 'SELECT ' . self::expand_prefix( $select ) . " FROM {$from}{$where_sql}", ARRAY_A ); return is_array( $row ) ? $row : null; } private function decorate_rows( $rows ) { if ( ! $rows ) { return $rows; } $out = apply_filters( 'aieo_report_rows', $rows, $this->def, $this->req ); return is_array( $out ) && count( $out ) === count( $rows ) ? array_values( $out ) : $rows; } private function clamp_page( $filtered ) { $per_page = max( 1, (int) $this->req['per_page'] ); $pages = (int) ceil( max( 0, (int) $filtered ) / $per_page ); $this->req['page'] = max( 1, min( (int) $this->req['page'], max( 1, $pages ) ) ); } private function from_clause() { global $wpdb; if ( 'sql' === $this->def->source() ) { $raw = $this->def->get( 'sql' ); if ( AIEO_Report_Definition::is_sql_builder( $raw ) ) { $raw = call_user_func( $raw, $this->req['params'] ); if ( ! is_string( $raw ) || ! AIEO_Report_Definition::looks_like_a_select( $raw ) ) { return ''; } } $sql = rtrim( trim( (string) $raw ), '; ' ); $sql = self::expand_prefix( $sql ); return '( ' . $sql . ' ) AS aieo_src'; } $table = self::expand_prefix( (string) $this->def->get( 'table' ) ); if ( ! preg_match( '/^[A-Za-z0-9_]+$/', $table ) ) { return ''; } return "`{$table}` AS aieo_src"; } public static function expand_prefix( $sql ) { global $wpdb; return str_replace( array( '{prefix}', '{$prefix}' ), $wpdb->prefix, (string) $sql ); } public static function branch_site_time_expression( $column ) { $column = preg_replace( '/[^A-Za-z0-9_]/', '', (string) $column ); $tz = wp_timezone_string(); if ( ! preg_match( '#^([A-Za-z]+(/[A-Za-z_\-]+){1,2}|[+\-]\d{2}:\d{2})$#', $tz ) ) { $tz = '+00:00'; } return "COALESCE(CONVERT_TZ(`{$column}`, '+00:00', '" . $tz . "'), `{$column}`)"; } private function select_list() { $parts = array(); foreach ( $this->def->columns() as $col ) { $key = (string) $col['key']; if ( '' !== (string) ( isset( $col['select'] ) ? $col['select'] : '' ) ) { $parts[] = $col['select'] . ' AS `' . $key . '`'; } else { $parts[] = '`' . $key . '`'; } } return $parts ? implode( ', ', $parts ) : '*'; } private function order_clause() { $tie = $this->tie_break_key(); if ( '' === $this->req['sort'] ) { return $tie ? ' ORDER BY `' . $tie . '` ASC' : ''; } if ( ! in_array( $this->req['sort'], $this->def->sortable_keys(), true ) ) { return $tie ? ' ORDER BY `' . $tie . '` ASC' : ''; } $dir = 'DESC' === $this->req['dir'] ? 'DESC' : 'ASC'; $order = ' ORDER BY `' . $this->req['sort'] . '` ' . $dir; if ( '' !== $tie && $tie !== $this->req['sort'] ) { $order .= ', `' . $tie . '` ' . $dir; } return $order; } private function tie_break_key() { $key = $this->def->get( 'row_key', '' ); if ( ! is_string( $key ) || '' === $key ) { return ''; } foreach ( $this->def->columns() as $col ) { if ( (string) $col['key'] === $key ) { return '' === (string) ( isset( $col['select'] ) ? $col['select'] : '' ) ? $key : ''; } } return ''; } private function apply_base_where() { $base = trim( (string) $this->def->get( 'base_where', '' ) ); if ( '' !== $base ) { $this->where[] = '(' . self::expand_prefix( $base ) . ')'; } } private function apply_search() { global $wpdb; $row_key = $this->def->row_key(); if ( ! empty( $this->req['keys'] ) && '' !== $row_key ) { $ph = implode( ', ', array_fill( 0, count( $this->req['keys'] ), '%s' ) ); $this->where[] = $wpdb->prepare( $this->select_expression_for( $row_key ) . " IN ({$ph})", $this->req['keys'] ); } $term = $this->req['search']; $keys = $this->def->searchable_keys(); if ( '' === $term || ! $keys ) { return; } $like = '%' . $wpdb->esc_like( $term ) . '%'; $parts = array(); foreach ( $keys as $key ) { $expr = $this->select_expression_for( $key ); $parts[] = $wpdb->prepare( "{$expr} LIKE %s", $like ); } $this->where[] = '(' . implode( ' OR ', $parts ) . ')'; } private function apply_params() { global $wpdb; $filterable = $this->def->filterable(); foreach ( $this->req['params'] as $key => $value ) { $key = (string) $key; if ( ! isset( $filterable[ $key ] ) ) { continue; } $kind = $filterable[ $key ]; if ( 'multiselect' === $kind ) { $values = array_values( array_filter( array_map( 'strval', (array) $value ), 'strlen' ) ); if ( ! $values ) { continue; } $ph = implode( ', ', array_fill( 0, count( $values ), '%s' ) ); $this->where[] = $wpdb->prepare( $this->select_expression_for( $key ) . " IN ({$ph})", $values ); continue; } if ( 'number_range' === $kind || 'date_range' === $kind ) { $from = is_array( $value ) && isset( $value['from'] ) ? (string) $value['from'] : ''; $to = is_array( $value ) && isset( $value['to'] ) ? (string) $value['to'] : ''; if ( 'date_range' === $kind && $this->column_stores_utc( $key ) ) { if ( '' !== $from ) { $from = self::site_day_start_utc( $from ); } if ( '' !== $to ) { $to = self::site_day_end_utc( $to ); } } elseif ( 'date_range' === $kind ) { if ( '' !== $to ) { $to = gmdate( 'Y-m-d H:i:s', strtotime( $to . ' +1 day' ) ); } if ( '' !== $from ) { $from = gmdate( 'Y-m-d H:i:s', strtotime( $from ) ); } } if ( '' !== $from ) { $this->where[] = $wpdb->prepare( $this->select_expression_for( $key ) . " >= %s", $from ); } if ( '' !== $to ) { $op = ( 'date_range' === $kind ) ? '<' : '<='; $this->where[] = $wpdb->prepare( $this->select_expression_for( $key ) . " {$op} %s", $to ); } continue; } $value = is_scalar( $value ) ? (string) $value : ''; if ( '' === $value ) { continue; } if ( 'number' === $kind ) { $this->where[] = $wpdb->prepare( $this->select_expression_for( $key ) . " = %f", (float) $value ); continue; } $exact = $this->column_filter_is_exact( $key, $kind ); if ( $exact ) { $this->where[] = $wpdb->prepare( $this->select_expression_for( $key ) . " = %s", $value ); } else { $this->where[] = $wpdb->prepare( $this->select_expression_for( $key ) . " LIKE %s", '%' . $wpdb->esc_like( $value ) . '%' ); } } } private function column_filter_is_exact( $key, $kind ) { foreach ( $this->def->columns() as $col ) { if ( (string) $col['key'] !== $key ) { continue; } if ( isset( $col['filter_match'] ) ) { return 'exact' === (string) $col['filter_match']; } } return in_array( $kind, array( 'select', 'multiselect' ), true ); } private function aggregates_in_sql( $from, $where_sql ) { global $wpdb; $aggregates = (array) $this->def->get( 'aggregates', array() ); if ( ! $aggregates ) { return array(); } $parts = array(); foreach ( $aggregates as $key => $func ) { $expr = $this->select_expression_for( $this->def->aggregate_source( (string) $key ) ); $fn = strtoupper( (string) $func ); $parts[] = ( 'COUNT_DISTINCT' === $fn ) ? 'COUNT(DISTINCT ' . $expr . ') AS `' . $key . '`' : $fn . '(' . $expr . ') AS `' . $key . '`'; } $row = $wpdb->get_row( "SELECT " . implode( ', ', $parts ) . " FROM {$from}{$where_sql}", ARRAY_A ); if ( ! is_array( $row ) ) { return array(); } foreach ( $row as $key => $value ) { if ( null === $value && 0 !== strpos( strtoupper( (string) $aggregates[ $key ] ), 'COUNT' ) ) { $row[ $key ] = 0; } } return $row; } private function select_expression_for( $key ) { foreach ( $this->def->columns() as $col ) { if ( (string) $col['key'] === $key ) { return '' !== (string) ( isset( $col['select'] ) ? $col['select'] : '' ) ? (string) $col['select'] : '`' . $key . '`'; } } return '`' . $key . '`'; } private function column_stores_utc( $key ) { foreach ( $this->def->columns() as $col ) { if ( (string) $col['key'] === (string) $key ) { return isset( $col['format']['tz'] ) && 'utc' === $col['format']['tz']; } } return false; } public function filter_options( $key, $cascade = true ) { global $wpdb; $key = (string) $key; if ( ! isset( $this->def->filterable()[ $key ] ) ) { return array( 'options' => array(), 'truncated' => false ); } if ( 'rows' === $this->def->source() ) { $rows = call_user_func( $this->def->get( 'rows' ), $this->req['params'] ); $vals = array(); foreach ( (array) $rows as $row ) { $v = AIEO_Table_Columns::value( $row, $key, null ); if ( null !== $v && '' !== $v ) { $vals[ (string) $v ] = true; } } $vals = array_keys( $vals ); sort( $vals ); return array( 'options' => array_slice( $vals, 0, self::FILTER_OPTIONS_MAX ), 'truncated' => count( $vals ) > self::FILTER_OPTIONS_MAX, ); } $from = $this->from_clause(); if ( '' === $from ) { return array( 'options' => array(), 'truncated' => false ); } $this->where = array(); $this->apply_base_where(); if ( $cascade && $this->req['params'] ) { $others = $this->req['params']; unset( $others[ $key ] ); if ( $others ) { $keep = $this->req['params']; $this->req['params'] = $others; $this->apply_params(); $this->req['params'] = $keep; } } $base = $this->where ? ' AND ' . implode( ' AND ', $this->where ) : ''; $expr = $this->select_expression_for( $key ); $rows = $wpdb->get_col( $wpdb->prepare( "SELECT DISTINCT {$expr} FROM {$from} WHERE {$expr} IS NOT NULL AND {$expr} <> ''{$base} ORDER BY 1 ASC LIMIT %d", self::FILTER_OPTIONS_MAX + 1 ) ); $rows = is_array( $rows ) ? $rows : array(); $truncated = count( $rows ) > self::FILTER_OPTIONS_MAX; return array( 'options' => array_slice( $rows, 0, self::FILTER_OPTIONS_MAX ), 'truncated' => $truncated, ); } private function filter_rows_in_php( $rows ) { $term = $this->req['search']; $searchable = $this->def->searchable_keys(); $filterable = $this->def->filterable(); return array_values( array_filter( $rows, function ( $row ) use ( $term, $searchable, $filterable ) { if ( ! empty( $this->req['keys'] ) && ! in_array( (string) AIEO_Table_Columns::value( $row, $this->def->row_key(), '' ), $this->req['keys'], true ) ) { return false; } foreach ( $this->req['params'] as $key => $value ) { $key = (string) $key; if ( ! isset( $filterable[ $key ] ) || '' === $value || null === $value ) { continue; } $kind = $filterable[ $key ]; $have = (string) AIEO_Table_Columns::value( $row, $key, '' ); if ( 'number_range' === $kind || 'date_range' === $kind ) { $from = is_array( $value ) && isset( $value['from'] ) ? (string) $value['from'] : ''; $to = is_array( $value ) && isset( $value['to'] ) ? (string) $value['to'] : ''; if ( 'number_range' === $kind ) { if ( '' !== $from && (float) $have < (float) $from ) { return false; } if ( '' !== $to && (float) $have > (float) $to ) { return false; } continue; } $utc = $this->column_stores_utc( $key ); $ts = strtotime( $have . ( $utc ? ' UTC' : '' ) ); if ( false === $ts ) { return false; } if ( '' !== $from ) { $b = $utc ? self::site_day_start_utc( $from ) : $from; if ( '' !== $b && $ts < strtotime( $b . ( $utc ? ' UTC' : '' ) ) ) { return false; } } if ( '' !== $to ) { $b = $utc ? self::site_day_end_utc( $to ) : gmdate( 'Y-m-d H:i:s', strtotime( $to . ' +1 day' ) ); if ( '' !== $b && $ts >= strtotime( $b . ( $utc ? ' UTC' : '' ) ) ) { return false; } } continue; } if ( is_array( $value ) ) { if ( ! in_array( $have, array_map( 'strval', $value ), true ) ) { return false; } continue; } if ( 'number' === $kind ) { if ( (float) $have !== (float) $value ) { return false; } continue; } if ( $this->column_filter_is_exact( $key, $kind ) ) { if ( $have !== (string) $value ) { return false; } } elseif ( '' === $have || false === stripos( $have, (string) $value ) ) { return false; } } if ( '' === $term || ! $searchable ) { return true; } foreach ( $searchable as $key ) { $have = (string) AIEO_Table_Columns::value( $row, $key, '' ); if ( '' !== $have && false !== stripos( $have, $term ) ) { return true; } } return false; } ) ); } private function sort_rows_in_php( $rows ) { $sort = $this->req['sort']; if ( '' === $sort ) { return $rows; } $factor = 'DESC' === $this->req['dir'] ? -1 : 1; usort( $rows, static function ( $a, $b ) use ( $sort, $factor ) { $x = AIEO_Table_Columns::value( $a, $sort, null ); $y = AIEO_Table_Columns::value( $b, $sort, null ); $xe = ( null === $x || '' === $x ); $ye = ( null === $y || '' === $y ); if ( $xe && $ye ) { return 0; } if ( $xe ) { return 1; } if ( $ye ) { return -1; } if ( is_numeric( $x ) && is_numeric( $y ) ) { return $factor * ( (float) $x <=> (float) $y ); } return $factor * strnatcasecmp( (string) $x, (string) $y ); } ); return $rows; } private function aggregate_in_php( $rows ) { $aggregates = (array) $this->def->get( 'aggregates', array() ); if ( ! $aggregates ) { return array(); } $out = array(); foreach ( $aggregates as $key => $func ) { $fn = strtoupper( (string) $func ); $src = $this->def->aggregate_source( (string) $key ); if ( 'COUNT_DISTINCT' === $fn ) { $seen = array(); foreach ( $rows as $row ) { $v = AIEO_Table_Columns::value( $row, $src, null ); if ( null === $v || '' === $v || is_array( $v ) ) { continue; } $seen[ (string) $v ] = true; } $out[ $key ] = count( $seen ); continue; } $values = array(); foreach ( $rows as $row ) { $v = AIEO_Table_Columns::value( $row, $src, null ); if ( null !== $v && '' !== $v && is_numeric( $v ) ) { $values[] = (float) $v; } } switch ( $fn ) { case 'COUNT': $out[ $key ] = count( $values ); break; case 'AVG': $out[ $key ] = $values ? array_sum( $values ) / count( $values ) : 0; break; case 'MIN': $out[ $key ] = $values ? min( $values ) : 0; break; case 'MAX': $out[ $key ] = $values ? max( $values ) : 0; break; default: $out[ $key ] = array_sum( $values ); break; } } return $out; } private function shape_rows( $rows ) { $keys = array(); foreach ( $this->def->columns() as $col ) { $keys[] = (string) $col['key']; } $out = array(); foreach ( $rows as $row ) { $line = array(); foreach ( $keys as $key ) { $line[] = AIEO_Table_Columns::value( $row, $key, null ); } $out[] = $line; } return $out; } private function public_columns() { $out = array(); foreach ( $this->def->columns() as $col ) { $public = $col; $public['hidden'] = ! empty( $col['hidden'] ); unset( $public['select'] ); $public['label'] = ( '' !== (string) $col['label'] ) ? __( (string) $col['label'], 'ai-eshop-optimizer' ) : (string) $col['key']; $out[] = $public; } return $out; } private function meta( $total, $filtered ) { $per_page = $this->req['per_page']; return array( 'total' => (int) $total, 'filtered' => (int) $filtered, 'page' => (int) $this->req['page'], 'per_page' => (int) $per_page, 'pages' => (int) ( $per_page > 0 ? (int) ceil( $filtered / $per_page ) : 0 ), 'sort' => (string) $this->req['sort'], 'dir' => (string) $this->req['dir'], 'generated_at' => current_time( 'mysql', true ), 'source' => $this->def->source(), 'demo' => (bool) apply_filters( 'aieo_report_demo_mode', false, $this->def->id() ), 'site' => (string) wp_parse_url( home_url(), PHP_URL_HOST ), 'tz' => (string) wp_timezone_string(), 'currency' => function_exists( 'get_woocommerce_currency' ) ? get_woocommerce_currency() : '', 'locale' => (string) determine_locale(), 'definition_version' => $this->def->version(), 'report' => $this->def->id(), 'next_cursor' => null, ); } public static function site_day_start_utc( $date ) { return self::site_day_boundary_utc( $date, 0 ); } public static function site_day_end_utc( $date ) { return self::site_day_boundary_utc( $date, 1 ); } private static function site_day_boundary_utc( $date, $plus_days ) { $date = trim( (string) $date ); if ( ! preg_match( '/^\d{4}-\d{2}-\d{2}/', $date ) ) { return ''; } try { $local = new DateTimeImmutable( substr( $date, 0, 10 ) . ' 00:00:00', wp_timezone() ); if ( $plus_days ) { $local = $local->modify( '+' . (int) $plus_days . ' day' ); } return $local->setTimezone( new DateTimeZone( 'UTC' ) )->format( 'Y-m-d H:i:s' ); } catch ( Exception $e ) { return ''; } } }