RTMediaModel::get PHP Method

get() public method

public get ( array $columns, mixed $offset = false, mixed $per_page = false, string $order_by = 'media_id desc', $count_flag = false ) : array
$columns array
$offset mixed
$per_page mixed
$order_by string
return array
    function get($columns, $offset = false, $per_page = false, $order_by = 'media_id desc', $count_flag = false)
    {
        global $wpdb;
        $select = 'SELECT ';
        if ($count_flag) {
            $select .= 'count(*) ';
        } else {
            $select .= "{$this->table_name}.* ";
        }
        $from = " FROM {$this->table_name} ";
        $join = '';
        $where = ' where 2=2 ';
        if (is_multisite()) {
            $where .= $wpdb->prepare(" AND {$this->table_name}.blog_id =%d ", get_current_blog_id());
            // @codingStandardsIgnoreLine
        }
        $temp = 65;
        $columns = apply_filters('rtmedia-model-query-columns', $columns, $count_flag);
        foreach ((array) $columns as $colname => $colvalue) {
            $colname = esc_sql($colname);
            if ('meta_query' === strtolower($colname)) {
                foreach ($colvalue as $meta_query) {
                    if (!isset($meta_query['compare'])) {
                        $meta_query['compare'] = '=';
                    }
                    $tbl_alias = esc_sql(chr($temp++));
                    if (is_multisite()) {
                        $join .= " LEFT JOIN {$wpdb->base_prefix}{$this->meta_table_name} as {$tbl_alias} ON {$this->table_name}.id = {$tbl_alias}.media_id ";
                    } else {
                        $join .= " LEFT JOIN {$wpdb->prefix}{$this->meta_table_name} as {$tbl_alias} ON {$this->table_name}.id = {$tbl_alias}.media_id ";
                    }
                    $meta_query['compare'] = esc_sql($meta_query['compare']);
                    if (isset($meta_query['value'])) {
                        $where .= $wpdb->prepare(" AND  ({$tbl_alias}.meta_key = %s and  {$tbl_alias}.meta_value  {$meta_query["compare"]}  %s ) ", $meta_query['key'], $meta_query['value']);
                        // @codingStandardsIgnoreLine
                    } else {
                        $where .= $wpdb->prepare(" AND  {$tbl_alias}.meta_key = %s ", $meta_query['key']);
                        // @codingStandardsIgnoreLine
                    }
                }
            } else {
                if (is_array($colvalue)) {
                    if (!isset($colvalue['compare'])) {
                        $compare = 'IN';
                    } else {
                        $compare = $colvalue['compare'];
                    }
                    $tmpVal = isset($colvalue['value']) ? $colvalue['value'] : $colvalue;
                    $col_val_comapare = is_array($tmpVal) ? implode("','", esc_sql($tmpVal)) : esc_sql($tmpVal);
                    if ('IS NOT' === $compare) {
                        $col_val_comapare = !empty($colvalue['value']) ? $colvalue['value'] : $col_val_comapare;
                    }
                    $compare = esc_sql($compare);
                    $where .= " AND {$this->table_name}.{$colname} {$compare} ('{$col_val_comapare}')";
                } else {
                    $where .= $wpdb->prepare(" AND {$this->table_name}.{$colname} = %s", $colvalue);
                    // @codingStandardsIgnoreLine
                }
            }
        }
        $qgroup_by = ' ';
        if ($order_by) {
            $order_by = esc_sql($order_by);
            $qorder_by = " ORDER BY {$this->table_name}.{$order_by}";
        } else {
            $qorder_by = '';
        }
        $select = apply_filters('rtmedia-model-select-query', $select, $this->table_name);
        $join = apply_filters('rtmedia-model-join-query', $join, $this->table_name);
        $where = apply_filters('rtmedia-model-where-query', $where, $this->table_name, $join);
        $qgroup_by = apply_filters('rtmedia-model-group-by-query', $qgroup_by, $this->table_name);
        $qorder_by = apply_filters('rtmedia-model-order-by-query', $qorder_by, $this->table_name);
        $sql = $select . $from . $join . $where . $qgroup_by . $qorder_by;
        if (false !== $offset) {
            if (!is_integer($offset)) {
                $offset = 0;
            }
            if (intval($offset) < 0) {
                $offset = 0;
            }
            if (!is_integer($per_page)) {
                $per_page = 1;
            }
            if (intval($per_page) < 1) {
                $per_page = 1;
            }
            //filter added to change the LIMIT
            $limit = apply_filters('rtmedia-model-limit-query', ' LIMIT ' . $offset . ',' . $per_page, $offset, $per_page);
            $sql .= $limit;
        }
        if (!$count_flag) {
            return $wpdb->get_results($sql);
            // @codingStandardsIgnoreLine
        } else {
            return $wpdb->get_var($sql);
            // @codingStandardsIgnoreLine
        }
    }

Usage Example

Exemplo n.º 1
0
 function rtm_change_activity_privacy()
 {
     $data = $_POST;
     if (wp_verify_nonce($data['nonce'], 'rtmedia_activity_privacy_nonce')) {
         $rtm_activity_model = new RTMediaActivityModel();
         $is_ac_privacy_exist = $rtm_activity_model->check($data['activity_id']);
         $privacy = intval($data['privacy']);
         $activity_id = intval($data['activity_id']);
         if (!$is_ac_privacy_exist) {
             // Very first privacy entry for this activity
             $status = $rtm_activity_model->insert(array('privacy' => $privacy, 'activity_id' => $activity_id, 'user_id' => get_current_user_id()));
         } else {
             // Just update the existing value
             $status = $rtm_activity_model->update(array('privacy' => $privacy), array('activity_id' => $activity_id));
         }
         // update privacy of corresponding media
         $media_model = new RTMediaModel();
         $activity_media = $media_model->get(array('activity_id' => $activity_id));
         if (!empty($activity_media) && is_array($activity_media)) {
             foreach ($activity_media as $single_media) {
                 $where = array('id' => $single_media->id);
                 $columns = array('privacy' => $privacy);
                 // update media privacy
                 $media_model->update($columns, $where);
             }
         }
         if ($status === false) {
             $status = 'false';
         } else {
             $status = 'true';
         }
         echo $status;
         wp_die();
     }
 }
All Usage Examples Of RTMediaModel::get