Plugin Directory

Changeset 1514036


Ignore:
Timestamp:
10/13/2016 03:17:01 PM (9 years ago)
Author:
spindogs
Message:

release v1.4.2

Location:
wp-platform/trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • wp-platform/trunk/classes/Model.php

    r1513511 r1514036  
    44class Model {
    55
    6     //protected static $table; //abstract
    7     //protected static $settings; //abstract
    8 
    9     public $is_loaded = false;
    10     protected $self;
    11     protected $total;
    12     protected $query_debug;
    13     protected $filters = array();
    14     protected $orderby = array();
    15     protected $limit = false;
    16 
    17     /**
    18      * @param int $id
    19      * @return void
    20      */
    21     public function __construct($id=false)
    22     {
    23 
    24         if (property_exists($this, 'id')) {
    25             $this->id = $id;
    26         }
    27 
    28         $this->self = get_class($this);
    29 
    30     }
    31 
    32     /**
    33      * @return void
    34      */
    35     public function cache()
    36     {
    37         $GLOBALS['cache'][$this->self][$this->id] = $this;
    38     }
    39 
    40     /**
    41      * @param array|bool $row
    42      * @return void
    43      */
    44     public function reload($row=false)
    45     {
    46         $this->is_loaded = false;
    47         $this->load($row);
    48     }
    49 
    50     /**
    51      * @param array|bool $row
    52      * @return bool
    53      */
    54     public function load($row=false)
    55     {
    56         global $wpdb;
    57 
    58         if ($this->is_loaded && !$row) {
    59             return false;
    60         }
    61 
    62         if (!$row) {
    63 
    64             //get from cache
    65             if (!static::settings('is_cached')) {
    66                 //ignore cache if we're not opted in
    67             } elseif (isset($GLOBALS['cache'][$this->self][$this->id])) {
    68 
    69                 $obj_properties = get_object_vars($GLOBALS['cache'][$this->self][$this->id]);
    70 
    71                 foreach ($obj_properties as $key => $val) {
    72                     $this->$key = $val;
    73                 }
    74 
    75                 return true;
    76 
    77             }
    78 
    79             //get from database
    80             if (property_exists($this, 'id') && $this->id) {
    81                 $this->filter('id', $this->id);
    82             } elseif (!$this->filters) {
    83                 return false;
    84             }
    85 
    86             $q = $this->query();
    87             $this->query_debug = $q;
    88             $row = $wpdb->get_row($q);
    89 
    90             if (!$row) {
    91                 return false;
    92             }
    93 
    94         }
    95 
    96         $this->map($row);
    97         $this->is_loaded = true;
    98 
    99         unset($this->total);
    100         unset($this->query_debug);
    101         unset($this->filters);
    102         unset($this->orderby);
    103         unset($this->limit);
    104 
    105         return true;
    106 
    107     }
    108 
    109     /**
    110      * @param string $clean
    111      * @return bool
    112      */
    113     public function loadByClean($clean)
    114     {
    115         $this->filter('clean', $clean);
    116         $this->load();
    117     }
    118 
    119     /**
    120      * @param string $uid
    121      * @return bool
    122      */
    123     public function loadByUID($uid)
    124     {
    125         $this->filter('uid', $uid);
    126         $this->load();
    127     }
    128 
    129     /**
    130      * @param array $values
    131      * @return int
    132      */
    133     public function create($values=array())
    134     {
    135         $this->map($values);
    136         $this->id = false;
    137         $this->save();
    138         return $this->id;
    139     }
    140 
    141     /**
    142      * @param array $values
    143      * @return int
    144      */
    145     public function update($values=array())
    146     {
    147         $this->map($values);
    148         $this->save();
    149         return $this->id;
    150     }
    151 
    152     /**
    153      * @return void
    154      */
    155     public function delete()
    156     {
    157         global $wpdb;
    158         $static = get_called_class();
    159 
    160         if (!static::settings('hard_delete')) {
    161 
    162             $values = array();
    163             $values['is_deleted'] = 1;
    164             Mysql::autoUpdate($values, $this->id, $static::$table);
    165 
    166         } else {
    167 
    168             $q = "DELETE FROM `".$static::$table."`
    169                     WHERE id = ".intval($this->id)."";
    170 
    171             $wpdb->query($q);
    172 
    173         }
    174 
    175     }
    176 
    177     /**
    178      * @return int
    179      */
    180     protected function save()
    181     {
    182         $static = get_called_class();
    183         $values = get_object_vars($this);
    184 
    185         if (isset($values['id'])) {
    186             unset($values['id']); //protect the id
    187         }
    188 
    189         if ($this->id) {
    190             Mysql::autoUpdate($values, $this->id, $static::$table, true);
    191         } else {
    192             $this->id = Mysql::autoCreate($values, $static::$table, '', true);
    193         }
    194 
    195         return $this->id;
    196 
    197     }
    198 
    199     /**
    200      * @param array $row
    201      * @return void
    202      */
    203     public function map($row)
    204     {
    205         $row = (array)$row;
    206         $fields = get_object_vars($this);
    207 
    208         if (array_key_exists('id', $fields) && !empty($this->id)) {
    209             unset($fields['id']); //protect the id
    210         }
    211 
    212         foreach ($row as $key => $val) {
    213             if (array_key_exists($key, $fields)) {
    214                 $this->{$key} = $val;
    215             }
    216         }
    217 
    218     }
    219 
    220     /**
    221      * @param string $key
    222      * @param mixed $val
    223      * @return void
    224      */
    225     public function filter($key, $val)
    226     {
    227         $this->filters[$key] = $val;
    228     }
    229 
    230     /**
    231      * @param string $key
    232      * @param mixed $dir
    233      * @return void
    234      */
    235     public function orderby($key, $dir)
    236     {
    237         $this->orderby[$key] = $dir;
    238     }
    239 
    240     /**
    241      * @param string|int $limit
    242      * @return void
    243      */
    244     public function limit($limit)
    245     {
    246         $this->limit = $limit;
    247     }
    248 
    249     /**
    250      * @param int $page
    251      * @param int $per_page
    252      * @return void
    253      */
    254     public function paged($page, $per_page = PER_PAGE)
    255     {
    256         if (!$page) {
    257             $page = 1;
    258         }
    259 
    260         $offset = ($page - 1) * $per_page;
    261         $limit = $offset.', '.$per_page;
    262 
    263         $this->limit($limit);
    264 
    265     }
    266 
    267     /**
    268      * @param string $q
    269      * @param string $key
    270      * @param string $column
    271      * @param string $operator
    272      * @return string
    273      */
    274     public function filterStr($q, $key, $column, $operator = '=')
    275     {
    276         return $this->filterQuery($q, $key, $column, $operator, 'STRING');
    277     }
    278 
    279     /**
    280      * @param string $q
    281      * @param string $key
    282      * @param string $column
    283      * @param string $operator
    284      * @return string
    285      */
    286     public function filterInt($q, $key, $column, $operator = '=')
    287     {
    288         return $this->filterQuery($q, $key, $column, $operator, 'INTEGER');
    289     }
    290 
    291     /**
    292      * @param string $q
    293      * @param string $key
    294      * @param string $column
    295      * @param string $operator
    296      * @return string
    297      */
    298     public function filterNull($q, $key, $column, $operator = '=')
    299     {
    300         return $this->filterQuery($q, $key, $column, $operator, 'NULL');
    301     }
    302 
    303     /**
    304      * @param string $q
    305      * @param string $key
    306      * @param string $column
    307      * @param string $operator
    308      * @return string
    309      */
    310     public function filterNotNull($q, $key, $column, $operator = '=')
    311     {
    312         return $this->filterQuery($q, $key, $column, $operator, 'NOTNULL');
    313     }
    314 
    315     /**
    316      * @param string $q
    317      * @param string $key
    318      * @param string $column
    319      * @param string $operator
    320      * @return string
    321      */
    322     protected function filterQuery($q, $key, $column, $operator, $type=false)
    323     {
    324         $tag = '{filter_'.$key.'}';
    325 
    326         if (isset($this->filters[$key])) {
    327             $val = $this->filters[$key];
    328         } else {
    329             $q = str_replace($tag, '', $q);
    330             return $q;
    331         }
    332 
    333         if ($type == 'INTEGER') {
    334             $q_where = " AND ".Security::escCol($column)." ".$operator." ".intval($val)."";
    335         }
    336 
    337         if ($type == 'NULL' && $val) {
    338             $q_where = " AND ".Security::escCol($column)." IS NULL";
    339         }
    340 
    341         if ($type == 'NOTNULL' && $val) {
    342             $q_where = " AND ".Security::escCol($column)." IS NOT NULL";
    343         }
    344 
    345         if ($type == 'STRING') {
    346 
    347             if ($operator == 'LIKE') {
    348                 $val = '%'.$val.'%';
    349                 $q_where = " AND ".Security::escCol($column)." LIKE ".Security::escSQL($val)."";
    350             } else {
    351                 $q_where = " AND ".Security::escCol($column)." = ".Security::escSQL($val)."";
    352             }
    353 
    354         }
    355 
    356         $q = str_replace($tag, $q_where, $q);
    357         return $q;
    358 
    359     }
    360 
    361     /**
    362      * @param string $q
    363      * @return string
    364      */
    365     public function filterLimit($q)
    366     {
    367         if (isset($this->limit)) {
    368             $q = str_replace('{limit}', " LIMIT ".escLimit($this->limit)."", $q);
    369         } else {
    370             $q = str_replace('{limit}', '', $q);
    371         }
    372 
    373         return $q;
    374 
    375     }
    376 
    377     /**
    378      * @return array
    379      */
    380     public function getAll()
    381     {
    382         global $wpdb;
    383 
    384         if (property_exists($this, 'id') && $this->id) {
    385             reportBug('You shouldn\'t be calling getAll() on a row object - please instantiate a new query object instead');
    386         }
    387 
    388         $q = $this->query();
    389         $this->query_debug = $q;
    390 
    391         if (static::settings('output_type')) {
    392             $output_type = static::settings('output_type');
    393         } else {
    394             $output_type = 'OBJECT';
    395         }
    396 
    397         $rtn = $wpdb->get_results($q, $output_type);
    398 
    399         if (strpos($q, 'SQL_CALC_FOUND_ROWS') !== false) {
    400             $this->total = $wpdb->get_var("SELECT FOUND_ROWS()");
    401             $this->total = intval($this->total);
    402         }
    403 
    404         foreach ($rtn as &$r) {
    405 
    406             if (property_exists($r, 'id')) {
    407                 $Row = new $this->self($r->id);
    408             } else {
    409                 $Row = new $this->self();
    410             }
    411 
    412             $Row->load($r);
    413             $r = $Row;
    414             unset($r);
    415 
    416         }
    417 
    418         return $rtn;
    419 
    420     }
    421 
    422     /**
    423      * @return int
    424      */
    425     public function getFoundRows()
    426     {
    427         return $this->total;
    428     }
    429 
    430     /**
    431      * @param string $key
    432      * @param mixed $val
    433      * @return mixed|bool
    434      */
    435     public static function settings($key, $val = null)
    436     {
    437         $static = get_called_class();
    438 
    439         if (!property_exists($static, 'settings')) {
    440             return null; //the model has not defined the settings property
    441         }
    442 
    443         if (empty(static::$settings)) {
    444             static::$settings = array();
    445         }
    446 
    447         if (isset($val)) {
    448             static::$settings[$key] = $val;
    449             return true;
    450         } elseif (isset(static::$settings[$key])) {
    451             return static::$settings[$key];
    452         } else {
    453             return null;
    454         }
    455 
    456     }
     6    //protected static $table; //abstract
     7    //protected static $settings; //abstract
     8
     9    public $is_loaded = false;
     10    protected $self;
     11    protected $total;
     12    protected $query_debug;
     13    protected $filters = array();
     14    protected $orderby = array();
     15    protected $limit = false;
     16
     17    /**
     18     * @param int $id
     19     * @return void
     20     */
     21    public function __construct($id=false)
     22    {
     23
     24        if (property_exists($this, 'id')) {
     25            $this->id = $id;
     26        }
     27
     28        $this->self = get_class($this);
     29
     30    }
     31
     32    /**
     33     * @return void
     34     */
     35    public function cache()
     36    {
     37        $GLOBALS['cache'][$this->self][$this->id] = $this;
     38    }
     39
     40    /**
     41     * @param array|bool $row
     42     * @return void
     43     */
     44    public function reload($row=false)
     45    {
     46        $this->is_loaded = false;
     47        $this->load($row);
     48    }
     49
     50    /**
     51     * @param array|bool $row
     52     * @return bool
     53     */
     54    public function load($row=false)
     55    {
     56        global $wpdb;
     57
     58        if ($this->is_loaded && !$row) {
     59            return false;
     60        }
     61
     62        if (!$row) {
     63
     64            //get from cache
     65            if (!static::settings('is_cached')) {
     66                //ignore cache if we're not opted in
     67            } elseif (isset($GLOBALS['cache'][$this->self][$this->id])) {
     68
     69                $obj_properties = get_object_vars($GLOBALS['cache'][$this->self][$this->id]);
     70
     71                foreach ($obj_properties as $key => $val) {
     72                    $this->$key = $val;
     73                }
     74
     75                return true;
     76
     77            }
     78
     79            //get from database
     80            if (property_exists($this, 'id') && $this->id) {
     81                $this->filter('id', $this->id);
     82            } elseif (!$this->filters) {
     83                return false;
     84            }
     85
     86            $q = $this->query();
     87            $this->query_debug = $q;
     88            $row = $wpdb->get_row($q);
     89
     90            if (!$row) {
     91                return false;
     92            }
     93
     94        }
     95
     96        $this->map($row);
     97        $this->loader();
     98
     99        $this->is_loaded = true;
     100
     101        unset($this->total);
     102        unset($this->query_debug);
     103        unset($this->filters);
     104        unset($this->orderby);
     105        unset($this->limit);
     106
     107        return true;
     108
     109    }
     110
     111    /**
     112     * @return void
     113     */
     114    protected function loader()
     115    {
     116        //this is a placeholder
     117    }
     118
     119    /**
     120     * @param string $clean
     121     * @return bool
     122     */
     123    public function loadByClean($clean)
     124    {
     125        $this->filter('clean', $clean);
     126        $this->load();
     127    }
     128
     129    /**
     130     * @param string $uid
     131     * @return bool
     132     */
     133    public function loadByUID($uid)
     134    {
     135        $this->filter('uid', $uid);
     136        $this->load();
     137    }
     138
     139    /**
     140     * @param array $values
     141     * @return int
     142     */
     143    public function create($values=array())
     144    {
     145        $this->map($values);
     146        $this->id = false;
     147        $this->save();
     148        return $this->id;
     149    }
     150
     151    /**
     152     * @param array $values
     153     * @return int
     154     */
     155    public function update($values=array())
     156    {
     157        $this->map($values);
     158        $this->save();
     159        return $this->id;
     160    }
     161
     162    /**
     163     * @return void
     164     */
     165    public function delete()
     166    {
     167        global $wpdb;
     168        $static = get_called_class();
     169
     170        if (!static::settings('hard_delete')) {
     171
     172            $values = array();
     173            $values['is_deleted'] = 1;
     174            Mysql::autoUpdate($values, $this->id, $static::$table);
     175
     176        } else {
     177
     178            $q = "DELETE FROM `".$static::$table."`
     179                    WHERE id = ".intval($this->id)."";
     180
     181            $wpdb->query($q);
     182
     183        }
     184
     185    }
     186
     187    /**
     188     * @return int
     189     */
     190    protected function save()
     191    {
     192        $static = get_called_class();
     193        $values = get_object_vars($this);
     194
     195        if (isset($values['id'])) {
     196            unset($values['id']); //protect the id
     197        }
     198
     199        if ($this->id) {
     200            Mysql::autoUpdate($values, $this->id, $static::$table, true);
     201        } else {
     202            $this->id = Mysql::autoCreate($values, $static::$table, '', true);
     203        }
     204
     205        return $this->id;
     206
     207    }
     208
     209    /**
     210     * @param array $row
     211     * @return void
     212     */
     213    public function map($row)
     214    {
     215        $row = (array)$row;
     216        $fields = get_object_vars($this);
     217
     218        if (array_key_exists('id', $fields) && !empty($this->id)) {
     219            unset($fields['id']); //protect the id
     220        }
     221
     222        foreach ($row as $key => $val) {
     223            if (array_key_exists($key, $fields)) {
     224                $this->{$key} = $val;
     225            }
     226        }
     227
     228    }
     229
     230    /**
     231     * @param string $key
     232      * @param mixed $val
     233     * @return void
     234     */
     235    public function filter($key, $val)
     236    {
     237        $this->filters[$key] = $val;
     238    }
     239
     240    /**
     241     * @param string $key
     242      * @param mixed $dir
     243     * @return void
     244     */
     245    public function orderby($key, $dir)
     246    {
     247        $this->orderby[$key] = $dir;
     248    }
     249
     250    /**
     251      * @param string|int $limit
     252     * @return void
     253     */
     254    public function limit($limit)
     255    {
     256        $this->limit = $limit;
     257    }
     258
     259    /**
     260      * @param int $page
     261      * @param int $per_page
     262     * @return void
     263     */
     264    public function paged($page, $per_page = PER_PAGE)
     265    {
     266        if (!$page) {
     267            $page = 1;
     268        }
     269
     270        $offset = ($page - 1) * $per_page;
     271        $limit = $offset.', '.$per_page;
     272
     273        $this->limit($limit);
     274
     275    }
     276
     277    /**
     278     * @param string $q
     279     * @param string $key
     280     * @param string $column
     281     * @param string $operator
     282     * @return string
     283     */
     284    public function filterStr($q, $key, $column, $operator = '=')
     285    {
     286        return $this->filterQuery($q, $key, $column, $operator, 'STRING');
     287    }
     288
     289    /**
     290     * @param string $q
     291     * @param string $key
     292     * @param string $column
     293     * @param string $operator
     294     * @return string
     295     */
     296    public function filterInt($q, $key, $column, $operator = '=')
     297    {
     298        return $this->filterQuery($q, $key, $column, $operator, 'INTEGER');
     299    }
     300
     301    /**
     302     * @param string $q
     303     * @param string $key
     304     * @param string $column
     305     * @param string $operator
     306     * @return string
     307     */
     308    public function filterNull($q, $key, $column, $operator = '=')
     309    {
     310        return $this->filterQuery($q, $key, $column, $operator, 'NULL');
     311    }
     312
     313    /**
     314     * @param string $q
     315     * @param string $key
     316     * @param string $column
     317     * @param string $operator
     318     * @return string
     319     */
     320    public function filterNotNull($q, $key, $column, $operator = '=')
     321    {
     322        return $this->filterQuery($q, $key, $column, $operator, 'NOTNULL');
     323    }
     324
     325    /**
     326     * @param string $q
     327     * @param string $key
     328     * @param string $column
     329     * @param string $operator
     330     * @return string
     331     */
     332    protected function filterQuery($q, $key, $column, $operator, $type=false)
     333    {
     334        $tag = '{filter_'.$key.'}';
     335
     336        if (isset($this->filters[$key])) {
     337            $val = $this->filters[$key];
     338        } else {
     339            $q = str_replace($tag, '', $q);
     340            return $q;
     341        }
     342
     343        if ($type == 'INTEGER') {
     344            $q_where = " AND ".Security::escCol($column)." ".$operator." ".intval($val)."";
     345        }
     346
     347        if ($type == 'NULL' && $val) {
     348            $q_where = " AND ".Security::escCol($column)." IS NULL";
     349        }
     350
     351        if ($type == 'NOTNULL' && $val) {
     352            $q_where = " AND ".Security::escCol($column)." IS NOT NULL";
     353        }
     354
     355        if ($type == 'STRING') {
     356
     357            if ($operator == 'LIKE') {
     358                $val = '%'.$val.'%';
     359                $q_where = " AND ".Security::escCol($column)." LIKE ".Security::escSQL($val)."";
     360            } else {
     361                $q_where = " AND ".Security::escCol($column)." = ".Security::escSQL($val)."";
     362            }
     363
     364        }
     365
     366        $q = str_replace($tag, $q_where, $q);
     367        return $q;
     368
     369    }
     370
     371    /**
     372     * @param string $q
     373     * @return string
     374     */
     375    public function filterLimit($q)
     376    {
     377        if (isset($this->limit)) {
     378            $q = str_replace('{limit}', " LIMIT ".escLimit($this->limit)."", $q);
     379        } else {
     380            $q = str_replace('{limit}', '', $q);
     381        }
     382
     383        return $q;
     384
     385    }
     386
     387    /**
     388     * @return array
     389     */
     390    public function getAll()
     391    {
     392        global $wpdb;
     393
     394        if (property_exists($this, 'id') && $this->id) {
     395            reportBug('You shouldn\'t be calling getAll() on a row object - please instantiate a new query object instead');
     396        }
     397
     398        $q = $this->query();
     399        $this->query_debug = $q;
     400
     401        if (static::settings('output_type')) {
     402            $output_type = static::settings('output_type');
     403        } else {
     404            $output_type = 'OBJECT';
     405        }
     406
     407        $rtn = $wpdb->get_results($q, $output_type);
     408
     409        if (strpos($q, 'SQL_CALC_FOUND_ROWS') !== false) {
     410            $this->total = $wpdb->get_var("SELECT FOUND_ROWS()");
     411            $this->total = intval($this->total);
     412        }
     413
     414        foreach ($rtn as &$r) {
     415
     416            if (property_exists($r, 'id')) {
     417                $Row = new $this->self($r->id);
     418            } else {
     419                $Row = new $this->self();
     420            }
     421
     422            $Row->load($r);
     423            $r = $Row;
     424            unset($r);
     425
     426        }
     427
     428        return $rtn;
     429
     430    }
     431
     432    /**
     433     * @return int
     434     */
     435    public function getFoundRows()
     436    {
     437        return $this->total;
     438    }
     439
     440    /**
     441     * @param string $key
     442     * @param mixed $val
     443     * @return mixed|bool
     444     */
     445    public static function settings($key, $val = null)
     446    {
     447        $static = get_called_class();
     448
     449        if (!property_exists($static, 'settings')) {
     450            return null; //the model has not defined the settings property
     451        }
     452
     453        if (empty(static::$settings)) {
     454            static::$settings = array();
     455        }
     456
     457        if (isset($val)) {
     458            static::$settings[$key] = $val;
     459            return true;
     460        } elseif (isset(static::$settings[$key])) {
     461            return static::$settings[$key];
     462        } else {
     463            return null;
     464        }
     465
     466    }
    457467
    458468}
  • wp-platform/trunk/classes/PostType.php

    r1492363 r1514036  
    44class PostType {
    55
    6     //protected static $custom_type; //abstract
    7     protected static $models = array();
    8 
    9     public $id;
    10     public $name;
    11     public $url;
    12     public $post_type;
    13     public $parent_id;
    14     public $date_published;
    15     public $image;
    16     public $excerpt;
    17     public $content;
    18 
    19     /**
    20      * @param int $id
    21      * @return void
    22      */
    23     public function __construct($id=false)
    24     {
    25         $this->id = $id;
    26     }
    27 
    28     /**
    29      * @return void
    30      */
    31     public function load()
    32     {
    33         $this->name = get_the_title($this->id);
    34         $this->url = get_permalink($this->id);
    35         $this->post_type = get_post_type($this->id);
    36         $this->parent_id = get_post_field('post_parent', $this->id);
    37         $this->image = array();
    38         $this->content = get_post_field('post_content', $this->id);
    39         $this->content = apply_filters('the_content', $this->content);
    40         $this->date_published = get_the_date('Y-m-d H:i:s', $this->id);
    41         $this->date_published = Filter::from_mysqltime($this->date_published);
    42 
    43         if (has_post_thumbnail($this->id)) {
    44             $attachment_id = get_post_thumbnail_id($this->id);
    45         } else {
    46             $attachment_id = false;
    47         }
    48 
    49         if ($attachment_id) {
    50 
    51             $sizes = get_intermediate_image_sizes();
    52             $this->image['url'] = wp_get_attachment_url($attachment_id);
    53 
    54             foreach ($sizes as $size) {
    55                 $this->image['sizes'][$size] = wp_get_attachment_image_src($attachment_id, $size);
    56                 $this->image['sizes'][$size] = reset($this->image['sizes'][$size]);
    57             }
    58 
    59         }
    60 
    61         if (function_exists('get_fields')) {
    62             $custom_fields = get_fields($this->id);
    63             $this->map($custom_fields);
    64         }
    65 
    66     }
    67 
    68     /**
    69      * @param array $row
    70      * @return void
    71      */
    72     public function map($row)
    73     {
    74         if (!$row) {
    75             return;
    76         }
    77 
    78         $row = (array)$row;
    79         $fields = get_object_vars($this);
    80 
    81         foreach ($row as $key => $val) {
    82 
    83             if (!array_key_exists($key, $fields)) {
    84                 continue;
    85             }
    86 
    87             $this->{$key} = $val;
    88 
    89         }
    90 
    91     }
    92 
    93     /**
    94      * @return void
    95      */
    96     public static function setup()
    97     {
    98         $static = get_called_class();
    99         $custom_type = $static::$custom_type;
    100 
    101         if (!$custom_type) {
    102             throw new Exception('You must define $custom_type in your PostType model');
    103         }
    104 
    105         self::$models[$custom_type]['class'] = $static;
    106 
    107         add_action('wp', array($static, 'listPage'));
    108 
    109     }
    110 
    111     /**
    112      * @return void
    113      */
    114     public static function listPage()
    115     {
    116         global $wp_query;
    117 
    118         if (is_admin()) {
    119             return;
    120         }
    121 
    122         $custom_type = static::$custom_type;
    123         $post_type = $wp_query->get('post_type');
    124 
    125         if ($post_type != $custom_type) {
    126             return;
    127         }
    128 
    129         if (!$wp_query->is_archive()) {
    130             return;
    131         }
    132 
    133         $base_blog = get_site_url(1);
    134         $curr_blog = get_bloginfo('url');
    135         $curr_prefix = str_replace($base_blog, '', $curr_blog);
    136 
    137         $uri = $_SERVER['REQUEST_URI'];
    138         $path = parse_url($uri, PHP_URL_PATH);
    139         $path = str_replace($curr_prefix, '', $path); //remove multisite blog prefix
    140         $path = trim($path, '/');
    141         $page = get_page_by_path($path);
    142 
    143         if (!$page) {
    144             return;
    145         }
    146 
    147         $wp_query->queried_object_id = $page->ID;
    148         $wp_query->queried_object = $page;
    149         $wp_query->queried_object->ancestors = array();
    150 
    151         $wp_query->post = $page;
    152         $wp_query->is_page = 1;
    153         $wp_query->is_singular = 1;
    154         $wp_query->is_404 = 0;
    155         $wp_query->reset_postdata();
    156 
    157         $GLOBALS['wp_the_query'] = $wp_query;
    158 
    159     }
    160 
    161     /**
    162      * @param int $id
    163      * @return Platform\PostType
    164      */
    165     public static function getPost($id=false)
    166     {
    167         if (!$id) {
    168             $id = get_the_ID();
    169         }
    170 
    171         if (!$id) {
    172             return false;
    173         }
    174 
    175         $models = self::$models;
    176         $post_type = get_post_type($id);
    177 
    178         if (!$post_type) {
    179             return false;
    180         }
    181 
    182         if (isset($models[$post_type]['class'])) {
    183             $class = $models[$post_type]['class'];
    184         } else {
    185             $class = get_called_class();
    186         }
    187 
    188         $post = new $class($id);
    189         $post->load();
    190 
    191         return $post;
    192 
    193     }
    194 
    195     /**
    196      * @return array
    197      */
    198     public static function getAll()
    199     {
    200         $args = array(
    201             'post_type' => static::$custom_type,
    202             'posts_per_page' => -1
    203         );
    204 
    205         $rtn = get_posts($args);
    206         $rtn = Collection::convert($rtn);
    207         return $rtn;
    208     }
    209 
    210     /**
    211      * @param int $limit
    212      * @return array
    213      */
    214     public static function getLatest($limit)
    215     {
    216         $args = array(
    217             'post_type' => static::$custom_type,
    218             'orderby' => 'date',
    219             'order' => 'DESC',
    220             'posts_per_page' => $limit
    221         );
    222 
    223         $rtn = get_posts($args);
    224         $rtn = Collection::convert($rtn);
    225         return $rtn;
    226     }
    227 
    228     /**
    229      * @return array
    230      */
    231     public static function getFeatured()
    232     {
    233         $meta_query = array();
    234         $meta_query[] = array(
    235             'key' => 'is_featured',
    236             'compare' => '=',
    237             'value' => true
    238         );
    239 
    240         $args = array(
    241             'post_type' => static::$custom_type,
    242             'posts_per_page' => -1,
    243             'meta_query' => $meta_query
    244         );
    245 
    246         $rtn = get_posts($args);
    247         $rtn = Collection::convert($rtn);
    248         return $rtn;
    249     }
     6    //protected static $custom_type; //abstract
     7    protected static $models = array();
     8
     9    public $id;
     10    public $name;
     11    public $url;
     12    public $post_type;
     13    public $parent_id;
     14    public $date_published;
     15    public $image;
     16    public $excerpt;
     17    public $content;
     18
     19    /**
     20     * @param int $id
     21     * @return void
     22     */
     23    public function __construct($id=false)
     24    {
     25        $this->id = $id;
     26    }
     27
     28    /**
     29     * @return void
     30     */
     31    public function load()
     32    {
     33        $this->name = get_the_title($this->id);
     34        $this->url = get_permalink($this->id);
     35        $this->post_type = get_post_type($this->id);
     36        $this->parent_id = get_post_field('post_parent', $this->id);
     37        $this->image = array();
     38        $this->content = get_post_field('post_content', $this->id);
     39        $this->content = apply_filters('the_content', $this->content);
     40        $this->date_published = get_the_date('Y-m-d H:i:s', $this->id);
     41        $this->date_published = Filter::from_mysqltime($this->date_published);
     42
     43        if (has_post_thumbnail($this->id)) {
     44            $attachment_id = get_post_thumbnail_id($this->id);
     45        } else {
     46            $attachment_id = false;
     47        }
     48
     49        if ($attachment_id) {
     50
     51            $sizes = get_intermediate_image_sizes();
     52            $this->image['url'] = wp_get_attachment_url($attachment_id);
     53
     54            foreach ($sizes as $size) {
     55                $this->image['sizes'][$size] = wp_get_attachment_image_src($attachment_id, $size);
     56                $this->image['sizes'][$size] = reset($this->image['sizes'][$size]);
     57            }
     58
     59        }
     60
     61        if (function_exists('get_fields')) {
     62            $custom_fields = get_fields($this->id);
     63            $this->map($custom_fields);
     64        }
     65
     66        $this->loader();
     67
     68    }
     69
     70    /**
     71     * @return void
     72     */
     73    public function loader()
     74    {
     75        //this is a placeholder
     76    }
     77
     78    /**
     79     * @param array $row
     80     * @return void
     81     */
     82    public function map($row)
     83    {
     84        if (!$row) {
     85            return;
     86        }
     87
     88        $row = (array)$row;
     89        $fields = get_object_vars($this);
     90
     91        foreach ($row as $key => $val) {
     92
     93            if (!array_key_exists($key, $fields)) {
     94                continue;
     95            }
     96
     97            $this->{$key} = $val;
     98
     99        }
     100
     101    }
     102
     103    /**
     104     * @return void
     105     */
     106    public static function setup()
     107    {
     108        $static = get_called_class();
     109        $custom_type = $static::$custom_type;
     110
     111        if (!$custom_type) {
     112            throw new Exception('You must define $custom_type in your PostType model');
     113        }
     114
     115        self::$models[$custom_type]['class'] = $static;
     116
     117        add_action('wp', array($static, 'listPage'));
     118
     119    }
     120
     121    /**
     122     * @return void
     123     */
     124    public static function listPage()
     125    {
     126        global $wp_query;
     127
     128        if (is_admin()) {
     129            return;
     130        }
     131
     132        $custom_type = static::$custom_type;
     133        $post_type = $wp_query->get('post_type');
     134
     135        if ($post_type != $custom_type) {
     136            return;
     137        }
     138
     139        if (!$wp_query->is_archive()) {
     140            return;
     141        }
     142
     143        $base_blog = get_site_url(1);
     144        $curr_blog = get_bloginfo('url');
     145        $curr_prefix = str_replace($base_blog, '', $curr_blog);
     146
     147        $uri = $_SERVER['REQUEST_URI'];
     148        $path = parse_url($uri, PHP_URL_PATH);
     149        $path = str_replace($curr_prefix, '', $path); //remove multisite blog prefix
     150        $path = trim($path, '/');
     151        $page = get_page_by_path($path);
     152
     153        if (!$page) {
     154            return;
     155        }
     156
     157        $wp_query->queried_object_id = $page->ID;
     158        $wp_query->queried_object = $page;
     159        $wp_query->queried_object->ancestors = array();
     160
     161        $wp_query->post = $page;
     162        $wp_query->is_page = 1;
     163        $wp_query->is_singular = 1;
     164        $wp_query->is_404 = 0;
     165        $wp_query->reset_postdata();
     166
     167        $GLOBALS['wp_the_query'] = $wp_query;
     168
     169    }
     170
     171    /**
     172     * @param int $id
     173     * @return Platform\PostType
     174     */
     175    public static function getPost($id=false)
     176    {
     177        if (!$id) {
     178            $id = get_the_ID();
     179        }
     180
     181        if (!$id) {
     182            return false;
     183        }
     184
     185        $models = self::$models;
     186        $post_type = get_post_type($id);
     187
     188        if (!$post_type) {
     189            return false;
     190        }
     191
     192        if (isset($models[$post_type]['class'])) {
     193            $class = $models[$post_type]['class'];
     194        } else {
     195            $class = get_called_class();
     196        }
     197
     198        $post = new $class($id);
     199        $post->load();
     200
     201        return $post;
     202
     203    }
     204
     205    /**
     206     * @return array
     207     */
     208    public static function getAll()
     209    {
     210        $args = array(
     211            'post_type' => static::$custom_type,
     212            'posts_per_page' => -1
     213        );
     214
     215        $rtn = get_posts($args);
     216        $rtn = Collection::convert($rtn);
     217        return $rtn;
     218    }
     219
     220    /**
     221     * @param int $limit
     222     * @return array
     223     */
     224    public static function getLatest($limit)
     225    {
     226        $args = array(
     227            'post_type' => static::$custom_type,
     228            'orderby' => 'date',
     229            'order' => 'DESC',
     230            'posts_per_page' => $limit
     231        );
     232
     233        $rtn = get_posts($args);
     234        $rtn = Collection::convert($rtn);
     235        return $rtn;
     236    }
     237
     238    /**
     239     * @return array
     240     */
     241    public static function getFeatured()
     242    {
     243        $meta_query = array();
     244        $meta_query[] = array(
     245            'key' => 'is_featured',
     246            'compare' => '=',
     247            'value' => true
     248        );
     249
     250        $args = array(
     251            'post_type' => static::$custom_type,
     252            'posts_per_page' => -1,
     253            'meta_query' => $meta_query
     254        );
     255
     256        $rtn = get_posts($args);
     257        $rtn = Collection::convert($rtn);
     258        return $rtn;
     259    }
    250260
    251261}
  • wp-platform/trunk/plugin.php

    r1513511 r1514036  
    22/**
    33 * Plugin Name: WP-Platform
    4  * Version: 1.4.1
     4 * Version: 1.4.2
    55 * Description: Provides platform to allow developers to build bespoke functionality in an MVC and OOP fashion
    66 */
Note: See TracChangeset for help on using the changeset viewer.