s as $source) { $user_id = $source['account_id']; $return['sources'][$user_id] = self::get_processed_source_data($source); } } else { $source_query = DB::source_query($args); if (isset($source_query[0])) { $source = $source_query[0]; $user_id = $source['account_id']; $return['sources'][$user_id] = self::get_processed_source_data($source); } } } */ return $return; } /** * Retrieves and organizes feed setting data for easy use in * the builder * It will NOT get the settings from the DB, but from the Customizer builder * To be used for updating feed preview on the fly * * @return array|bool * * @since 1.0 */ public function get_feed_settings_preview($settings_db_data) { if (\false === $settings_db_data || sizeof($settings_db_data) === 0) { return \false; } $return = $settings_db_data; $return = wp_parse_args($return, self::settings_defaults()); if (empty($return['sources'])) { return $return; } $sources = array(); foreach ($return['sources'] as $single_source) { array_push($sources, $single_source['account_id']); } $args = array('id' => $sources); $source_query = $this->db->source_query($args); $return['sources'] = array(); if (!empty($source_query)) { foreach ($source_query as $source) { $user_id = $source['account_id']; $return['sources'][$user_id] = self::get_processed_source_data($source); } } return $return; } /** * Default settings, $return_array equalling false will return * the settings in the general way that the "SBI_Shortcode" class, * "sbi_get_processed_options" method does * * @param bool $return_array * * @return array * * @since 1.0 */ public static function settings_defaults($return_array = \true) { { $defaults = array(); $defaults = self::filter_defaults($defaults); // some settings are comma separated and not arrays when the feed is created if ($return_array) { $settings_with_multiples = array('sources'); foreach ($settings_with_multiples as $multiple_key) { if (isset($defaults[$multiple_key])) { $defaults[$multiple_key] = explode(',', $defaults[$multiple_key]); } } } return $defaults; } } /** * Provides backwards compatibility for extensions * * @param array $defaults * * @return array * * @since 1.0 */ public static function filter_defaults($defaults) { return $defaults; } /** * Used for taking raw post data related to settings * an sanitizing it and sorting it to easily use in * the database tables * * @since 1.0 */ private function sanitize_and_sort_data() { $data = $this->data; $sanitized_and_sorted = array('feeds' => array(), 'feed_settings' => array()); foreach ($data as $key => $value) { $data_type = $this->get_data_type($key); $sanitized_values = array(); if (is_array($value)) { foreach ($value as $item) { $type = $this->is_boolean($item) ? 'boolean' : $data_type['sanitization']; $sanitized_values[] = $this->sanitize($type, $item); } } else { $type = $this->is_boolean($value) ? 'boolean' : $data_type['sanitization']; $sanitized_values[] = $this->sanitize($type, $value); } $single_sanitized = array('key' => $key, 'values' => $sanitized_values); $sanitized_and_sorted[$data_type['table']][] = $single_sanitized; } $this->sanitized_and_sorted_data = $sanitized_and_sorted; } /** * Determines what table and sanitization should be used * when handling feed setting data. * * TODO: Add settings that need something other than sanitize_text_field * * @param string $key * * @return array * * @since 1.0 */ private function get_data_type($key) { switch ($key) { case 'feed_name': case 'status': case 'feed_title': $return = array('table' => 'feeds', 'sanitization' => 'sanitize_text_field'); break; case 'author': $return = array('table' => 'feeds', 'sanitization' => 'int'); break; default: $return = array('table' => 'feed_settings', 'sanitization' => 'sanitize_text_field'); break; } return $return; } /** * Check if boolean * for a value * * @param string $type * @param int|string $value * * @return int|string * * @since 1.0 */ private function is_boolean($value) { return $value === 'true' || $value === 'false' || is_bool($value); } private function cast_boolean($value) { return $value === 'true' || $value === \true || $value === 'on'; } /** * Uses the appropriate sanitization function and returns the result * for a value * * @param string $type * @param int|string $value * * @return int|string * * @since 1.0 */ private function sanitize($type, $value) { switch ($type) { case 'int': $return = (int) $value; break; case 'boolean': $return = $this->cast_boolean($value); break; default: $return = sanitize_text_field($value); break; } return $return; } /** * Returns an associate array of all existing sources along with their data * * @param int $page * * @return array * * @since 1.0 */ public function get_source_list($page = 1) { $args['page'] = $page; return $this->db->source_query($args); } }