_escape() public method

Escape data. Works on arrays.
Since: 2.8.0
public _escape ( string | array $data ) : string | array
$data string | array
return string | array escaped
 /**
  * Used internally to get a list of network IDs matching the query vars.
  *
  * @since 4.6.0
  * @access protected
  *
  * @return int|array A single count of network IDs if a count query. An array of network IDs if a full query.
  */
 protected function get_network_ids()
 {
     $order = $this->parse_order($this->query_vars['order']);
     // Disable ORDER BY with 'none', an empty array, or boolean false.
     if (in_array($this->query_vars['orderby'], array('none', array(), false), true)) {
         $orderby = '';
     } elseif (!empty($this->query_vars['orderby'])) {
         $ordersby = is_array($this->query_vars['orderby']) ? $this->query_vars['orderby'] : preg_split('/[,\\s]/', $this->query_vars['orderby']);
         $orderby_array = array();
         foreach ($ordersby as $_key => $_value) {
             if (!$_value) {
                 continue;
             }
             if (is_int($_key)) {
                 $_orderby = $_value;
                 $_order = $order;
             } else {
                 $_orderby = $_key;
                 $_order = $_value;
             }
             $parsed = $this->parse_orderby($_orderby);
             if (!$parsed) {
                 continue;
             }
             if ('network__in' === $_orderby) {
                 $orderby_array[] = $parsed;
                 continue;
             }
             $orderby_array[] = $parsed . ' ' . $this->parse_order($_order);
         }
         $orderby = implode(', ', $orderby_array);
     } else {
         $orderby = "{$this->db->site}.id {$order}";
     }
     $number = absint($this->query_vars['number']);
     $offset = absint($this->query_vars['offset']);
     if (!empty($number)) {
         if ($offset) {
             $limits = 'LIMIT ' . $offset . ',' . $number;
         } else {
             $limits = 'LIMIT ' . $number;
         }
     }
     if ($this->query_vars['count']) {
         $fields = 'COUNT(*)';
     } else {
         $fields = "{$this->db->site}.id";
     }
     // Parse network IDs for an IN clause.
     if (!empty($this->query_vars['network__in'])) {
         $this->sql_clauses['where']['network__in'] = "{$this->db->site}.id IN ( " . implode(',', wp_parse_id_list($this->query_vars['network__in'])) . ' )';
     }
     // Parse network IDs for a NOT IN clause.
     if (!empty($this->query_vars['network__not_in'])) {
         $this->sql_clauses['where']['network__not_in'] = "{$this->db->site}.id NOT IN ( " . implode(',', wp_parse_id_list($this->query_vars['network__not_in'])) . ' )';
     }
     if (!empty($this->query_vars['domain'])) {
         $this->sql_clauses['where']['domain'] = $this->db->prepare("{$this->db->site}.domain = %s", $this->query_vars['domain']);
     }
     // Parse network domain for an IN clause.
     if (is_array($this->query_vars['domain__in'])) {
         $this->sql_clauses['where']['domain__in'] = "{$this->db->site}.domain IN ( '" . implode("', '", $this->db->_escape($this->query_vars['domain__in'])) . "' )";
     }
     // Parse network domain for a NOT IN clause.
     if (is_array($this->query_vars['domain__not_in'])) {
         $this->sql_clauses['where']['domain__not_in'] = "{$this->db->site}.domain NOT IN ( '" . implode("', '", $this->db->_escape($this->query_vars['domain__not_in'])) . "' )";
     }
     if (!empty($this->query_vars['path'])) {
         $this->sql_clauses['where']['path'] = $this->db->prepare("{$this->db->site}.path = %s", $this->query_vars['path']);
     }
     // Parse network path for an IN clause.
     if (is_array($this->query_vars['path__in'])) {
         $this->sql_clauses['where']['path__in'] = "{$this->db->site}.path IN ( '" . implode("', '", $this->db->_escape($this->query_vars['path__in'])) . "' )";
     }
     // Parse network path for a NOT IN clause.
     if (is_array($this->query_vars['path__not_in'])) {
         $this->sql_clauses['where']['path__not_in'] = "{$this->db->site}.path NOT IN ( '" . implode("', '", $this->db->_escape($this->query_vars['path__not_in'])) . "' )";
     }
     // Falsey search strings are ignored.
     if (strlen($this->query_vars['search'])) {
         $this->sql_clauses['where']['search'] = $this->get_search_sql($this->query_vars['search'], array("{$this->db->site}.domain", "{$this->db->site}.path"));
     }
     $join = '';
     $where = implode(' AND ', $this->sql_clauses['where']);
     $pieces = array('fields', 'join', 'where', 'orderby', 'limits', 'groupby');
     /**
      * Filters the network query clauses.
      *
      * @since 4.6.0
      *
      * @param array            $pieces A compacted array of network query clauses.
      * @param WP_Network_Query &$this  Current instance of WP_Network_Query, passed by reference.
      */
     $clauses = apply_filters_ref_array('networks_clauses', array(compact($pieces), &$this));
     $fields = isset($clauses['fields']) ? $clauses['fields'] : '';
     $join = isset($clauses['join']) ? $clauses['join'] : '';
     $where = isset($clauses['where']) ? $clauses['where'] : '';
     $orderby = isset($clauses['orderby']) ? $clauses['orderby'] : '';
     $limits = isset($clauses['limits']) ? $clauses['limits'] : '';
     $groupby = isset($clauses['groupby']) ? $clauses['groupby'] : '';
     if ($where) {
         $where = 'WHERE ' . $where;
     }
     if ($groupby) {
         $groupby = 'GROUP BY ' . $groupby;
     }
     if ($orderby) {
         $orderby = "ORDER BY {$orderby}";
     }
     $found_rows = '';
     if (!$this->query_vars['no_found_rows']) {
         $found_rows = 'SQL_CALC_FOUND_ROWS';
     }
     $this->sql_clauses['select'] = "SELECT {$found_rows} {$fields}";
     $this->sql_clauses['from'] = "FROM {$this->db->site} {$join}";
     $this->sql_clauses['groupby'] = $groupby;
     $this->sql_clauses['orderby'] = $orderby;
     $this->sql_clauses['limits'] = $limits;
     $this->request = "{$this->sql_clauses['select']} {$this->sql_clauses['from']} {$where} {$this->sql_clauses['groupby']} {$this->sql_clauses['orderby']} {$this->sql_clauses['limits']}";
     if ($this->query_vars['count']) {
         return intval($this->db->get_var($this->request));
     }
     $network_ids = $this->db->get_col($this->request);
     return array_map('intval', $network_ids);
 }
Exemplo n.º 2
0
 /**
  * {@inheritdoc}
  */
 public function escape($text)
 {
     return $this->_db->_escape($text);
 }
 /**
  * Used internally to get a list of site IDs matching the query vars.
  *
  * @since 4.6.0
  * @access protected
  *
  * @return int|array A single count of site IDs if a count query. An array of site IDs if a full query.
  */
 protected function get_site_ids()
 {
     $order = $this->parse_order($this->query_vars['order']);
     // Disable ORDER BY with 'none', an empty array, or boolean false.
     if (in_array($this->query_vars['orderby'], array('none', array(), false), true)) {
         $orderby = '';
     } elseif (!empty($this->query_vars['orderby'])) {
         $ordersby = is_array($this->query_vars['orderby']) ? $this->query_vars['orderby'] : preg_split('/[,\\s]/', $this->query_vars['orderby']);
         $orderby_array = array();
         foreach ($ordersby as $_key => $_value) {
             if (!$_value) {
                 continue;
             }
             if (is_int($_key)) {
                 $_orderby = $_value;
                 $_order = $order;
             } else {
                 $_orderby = $_key;
                 $_order = $_value;
             }
             $parsed = $this->parse_orderby($_orderby);
             if (!$parsed) {
                 continue;
             }
             if ('site__in' === $_orderby || 'network__in' === $_orderby) {
                 $orderby_array[] = $parsed;
                 continue;
             }
             $orderby_array[] = $parsed . ' ' . $this->parse_order($_order);
         }
         $orderby = implode(', ', $orderby_array);
     } else {
         $orderby = "blog_id {$order}";
     }
     $number = absint($this->query_vars['number']);
     $offset = absint($this->query_vars['offset']);
     if (!empty($number)) {
         if ($offset) {
             $limits = 'LIMIT ' . $offset . ',' . $number;
         } else {
             $limits = 'LIMIT ' . $number;
         }
     }
     if ($this->query_vars['count']) {
         $fields = 'COUNT(*)';
     } else {
         $fields = 'blog_id';
     }
     // Parse site IDs for an IN clause.
     $site_id = absint($this->query_vars['ID']);
     if (!empty($site_id)) {
         $this->sql_clauses['where']['ID'] = $this->db->prepare('blog_id = %d', $site_id);
     }
     // Parse site IDs for an IN clause.
     if (!empty($this->query_vars['site__in'])) {
         $this->sql_clauses['where']['site__in'] = "blog_id IN ( " . implode(',', wp_parse_id_list($this->query_vars['site__in'])) . ' )';
     }
     // Parse site IDs for a NOT IN clause.
     if (!empty($this->query_vars['site__not_in'])) {
         $this->sql_clauses['where']['site__not_in'] = "blog_id NOT IN ( " . implode(',', wp_parse_id_list($this->query_vars['site__not_in'])) . ' )';
     }
     $network_id = absint($this->query_vars['network_id']);
     if (!empty($network_id)) {
         $this->sql_clauses['where']['network_id'] = $this->db->prepare('site_id = %d', $network_id);
     }
     // Parse site network IDs for an IN clause.
     if (!empty($this->query_vars['network__in'])) {
         $this->sql_clauses['where']['network__in'] = 'site_id IN ( ' . implode(',', wp_parse_id_list($this->query_vars['network__in'])) . ' )';
     }
     // Parse site network IDs for a NOT IN clause.
     if (!empty($this->query_vars['network__not_in'])) {
         $this->sql_clauses['where']['network__not_in'] = 'site_id NOT IN ( ' . implode(',', wp_parse_id_list($this->query_vars['network__not_in'])) . ' )';
     }
     if (!empty($this->query_vars['domain'])) {
         $this->sql_clauses['where']['domain'] = $this->db->prepare('domain = %s', $this->query_vars['domain']);
     }
     // Parse site domain for an IN clause.
     if (is_array($this->query_vars['domain__in'])) {
         $this->sql_clauses['where']['domain__in'] = "domain IN ( '" . implode("', '", $this->db->_escape($this->query_vars['domain__in'])) . "' )";
     }
     // Parse site domain for a NOT IN clause.
     if (is_array($this->query_vars['domain__not_in'])) {
         $this->sql_clauses['where']['domain__not_in'] = "domain NOT IN ( '" . implode("', '", $this->db->_escape($this->query_vars['domain__not_in'])) . "' )";
     }
     if (!empty($this->query_vars['path'])) {
         $this->sql_clauses['where']['path'] = $this->db->prepare('path = %s', $this->query_vars['path']);
     }
     // Parse site path for an IN clause.
     if (is_array($this->query_vars['path__in'])) {
         $this->sql_clauses['where']['path__in'] = "path IN ( '" . implode("', '", $this->db->_escape($this->query_vars['path__in'])) . "' )";
     }
     // Parse site path for a NOT IN clause.
     if (is_array($this->query_vars['path__not_in'])) {
         $this->sql_clauses['where']['path__not_in'] = "path NOT IN ( '" . implode("', '", $this->db->_escape($this->query_vars['path__not_in'])) . "' )";
     }
     if (is_numeric($this->query_vars['archived'])) {
         $archived = absint($this->query_vars['archived']);
         $this->sql_clauses['where']['archived'] = $this->db->prepare("archived = %d ", $archived);
     }
     if (is_numeric($this->query_vars['mature'])) {
         $mature = absint($this->query_vars['mature']);
         $this->sql_clauses['where']['mature'] = $this->db->prepare("mature = %d ", $mature);
     }
     if (is_numeric($this->query_vars['spam'])) {
         $spam = absint($this->query_vars['spam']);
         $this->sql_clauses['where']['spam'] = $this->db->prepare("spam = %d ", $spam);
     }
     if (is_numeric($this->query_vars['deleted'])) {
         $deleted = absint($this->query_vars['deleted']);
         $this->sql_clauses['where']['deleted'] = $this->db->prepare("deleted = %d ", $deleted);
     }
     if (is_numeric($this->query_vars['public'])) {
         $public = absint($this->query_vars['public']);
         $this->sql_clauses['where']['public'] = $this->db->prepare("public = %d ", $public);
     }
     // Falsey search strings are ignored.
     if (strlen($this->query_vars['search'])) {
         $search_columns = array();
         if ($this->query_vars['search_columns']) {
             $search_columns = array_intersect($this->query_vars['search_columns'], array('domain', 'path'));
         }
         if (!$search_columns) {
             $search_columns = array('domain', 'path');
         }
         /**
          * Filters the columns to search in a WP_Site_Query search.
          *
          * The default columns include 'domain' and 'path.
          *
          * @since 4.6.0
          *
          * @param array         $search_columns Array of column names to be searched.
          * @param string        $search         Text being searched.
          * @param WP_Site_Query $this           The current WP_Site_Query instance.
          */
         $search_columns = apply_filters('site_search_columns', $search_columns, $this->query_vars['search'], $this);
         $this->sql_clauses['where']['search'] = $this->get_search_sql($this->query_vars['search'], $search_columns);
     }
     $date_query = $this->query_vars['date_query'];
     if (!empty($date_query) && is_array($date_query)) {
         $this->date_query = new WP_Date_Query($date_query, 'registered');
         $this->sql_clauses['where']['date_query'] = preg_replace('/^\\s*AND\\s*/', '', $this->date_query->get_sql());
     }
     $join = '';
     $where = implode(' AND ', $this->sql_clauses['where']);
     $pieces = array('fields', 'join', 'where', 'orderby', 'limits', 'groupby');
     /**
      * Filters the site query clauses.
      *
      * @since 4.6.0
      *
      * @param array $pieces A compacted array of site query clauses.
      * @param WP_Site_Query &$this Current instance of WP_Site_Query, passed by reference.
      */
     $clauses = apply_filters_ref_array('sites_clauses', array(compact($pieces), &$this));
     $fields = isset($clauses['fields']) ? $clauses['fields'] : '';
     $join = isset($clauses['join']) ? $clauses['join'] : '';
     $where = isset($clauses['where']) ? $clauses['where'] : '';
     $orderby = isset($clauses['orderby']) ? $clauses['orderby'] : '';
     $limits = isset($clauses['limits']) ? $clauses['limits'] : '';
     $groupby = isset($clauses['groupby']) ? $clauses['groupby'] : '';
     if ($where) {
         $where = 'WHERE ' . $where;
     }
     if ($groupby) {
         $groupby = 'GROUP BY ' . $groupby;
     }
     if ($orderby) {
         $orderby = "ORDER BY {$orderby}";
     }
     $found_rows = '';
     if (!$this->query_vars['no_found_rows']) {
         $found_rows = 'SQL_CALC_FOUND_ROWS';
     }
     $this->sql_clauses['select'] = "SELECT {$found_rows} {$fields}";
     $this->sql_clauses['from'] = "FROM {$this->db->blogs} {$join}";
     $this->sql_clauses['groupby'] = $groupby;
     $this->sql_clauses['orderby'] = $orderby;
     $this->sql_clauses['limits'] = $limits;
     $this->request = "{$this->sql_clauses['select']} {$this->sql_clauses['from']} {$where} {$this->sql_clauses['groupby']} {$this->sql_clauses['orderby']} {$this->sql_clauses['limits']}";
     if ($this->query_vars['count']) {
         return intval($this->db->get_var($this->request));
     }
     $site_ids = $this->db->get_col($this->request);
     return array_map('intval', $site_ids);
 }
Exemplo n.º 4
0
 /**
  * {@inheritdoc}
  * @codeCoverageIgnore
  */
 public function escape($text, $extra = false)
 {
     return $this->_db->_escape($text);
 }