/
Post.php
627 lines (542 loc) · 13.7 KB
/
Post.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
<?php
/**
* @category Fishpig
* @package Fishpig_Wordpress
* @license http://fishpig.co.uk/license.txt
* @author Ben Tideswell <help@fishpig.co.uk>
*/
class Fishpig_Wordpress_Model_Post extends Fishpig_Wordpress_Model_Abstract
{
/**
* Entity meta infromation
*
* @var string
*/
protected $_metaTable = 'wordpress/post_meta';
protected $_metaTableObjectField = 'post_id';
/**
* Event data
*
* @var string
*/
protected $_eventPrefix = 'wordpress_post';
protected $_eventObject = 'post';
/**
* Set the model's resource
*
* @return void
*/
public function _construct()
{
$this->_init('wordpress/post');
}
/**
* Set the categories after loading
*
* @return $this
*/
protected function _afterLoad()
{
parent::_afterLoad();
$this->getResource()->preparePosts(array($this));
return $this;
}
/**
* Returns the permalink used to access this post
*
* @return string
*/
public function getPermalink()
{
return $this->getUrl();
}
public function getPostFormat()
{
if (!$this->hasPostFormat()) {
$this->setPostFormat(false);
$formats = Mage::getResourceModel('wordpress/term_collection')
->addTaxonomyFilter('post_format')
->setPageSize(1)
->load();
if (count($formats) > 0) {
$this->setPostFormat(
str_replace('post-format-', '', $formats->getFirstItem()->getSlug())
);
}
}
return $this->_getData('post_format');
}
/**
* Retrieve the post GUID
*
* @return string
*/
public function getGuid()
{
if ($this->getPostType() === 'page') {
return Mage::helper('wordpress')->getUrl() . '?page_id=' . $this->getId();
}
else if ($this->getPostType() === 'post') {
return Mage::helper('wordpress')->getUrl() . '?p=' . $this->getId();
}
return Mage::helper('wordpress')->getUrl() . '?p=' . $this->getId() . '&post_type=' . $this->getPostType();
}
/**
* Retrieve the post excerpt
* If no excerpt, try to shorten the post_content field
*
* @return string
*/
public function getPostExcerpt($includeSuffix = true)
{
if (!$this->getData('post_excerpt')) {
$excerpt = $this->hasMoreTag()
? $this->_getPostTeaser($includeSuffix)
: $this->getPostContent('excerpt');
$this->setPostExcerpt($excerpt);
}
return $this->getData('post_excerpt');
}
/**
* Determine twhether the post has a more tag in it's content field
*
* @return bool
*/
public function hasMoreTag()
{
return strpos($this->getData('post_content'), '<!--more') !== false;
}
/**
* Retrieve the post teaser
* This is the data from the post_content field upto to the MORE_TAG
*
* @return string
*/
protected function _getPostTeaser($includeSuffix = true)
{
if ($this->hasMoreTag()) {
$content = $this->getPostContent('excerpt');
if (preg_match('/<!--more (.*)-->/', $content, $matches)) {
$anchor = $matches[1];
$split = $matches[0];
}
else {
$split = '<!--more-->';
$anchor = $this->_getTeaserAnchor();
}
$excerpt = trim(substr($content, 0, strpos($content, $split)));
if ($excerpt !== '' && $includeSuffix && $anchor) {
$excerpt .= sprintf(' <a href="%s" class="read-more">%s</a>', $this->getPermalink(), $anchor);
}
return $excerpt;
}
return null;
}
public function getParentTerm($taxonomy)
{
$terms = $this->getTermCollection($taxonomy)
->setPageSize(1)
->setCurPage(1)
->load();
return count($terms) > 0 ? $terms->getFirstItem() : false;
}
/**
* Get a collection of terms by the taxonomy
*
* @param string $taxonomy
* @return Fishpig_Wordpress_Model_Resource_Term_Collection
*/
public function getTermCollection($taxonomy)
{
return Mage::getResourceModel('wordpress/term_collection')
->addTaxonomyFilter($taxonomy)
->addPostIdFilter($this->getId());
}
/**
* Retrieve a collection of all parent categories
*
* @return Fishpig_Wordpress_Model_Mysql4_Post_Category_Collection
*/
public function getParentCategories()
{
return $this->getTermCollection('category');
return Mage::getResourceModel('wordpress/term_collection')
->addTaxonomyFilter('post_category')
->addFieldToFilter('main_table.term_id', array('in' => $this->getCategoryIds()));
}
/**
* Gets a collection of post tags
*
* @return Fishpig_Wordpress_Model_Mysql4_Post_Tag_Collection
*/
public function getTags()
{
return $this->getTermCollection('post_tag');
}
/**
* Retrieve the read more anchor text
*
* @return string|false
*/
protected function _getTeaserAnchor()
{
// Allows translation
return stripslashes(Mage::helper('wordpress')->__('Continue reading <span class=\"meta-nav\">→</span>'));
}
/**
* Retrieve the previous post
*
* @return false|Fishpig_Wordpress_Model_Post
*/
public function getPreviousPost()
{
if (!$this->hasPreviousPost()) {
$this->setPreviousPost(false);
$collection = Mage::getResourceModel('wordpress/post_collection')
->addIsViewableFilter()
->addPostTypeFilter($this->getPostType())
->addPostDateFilter(array('lt' => $this->_getData('post_date')))
->setPageSize(1)
->setCurPage(1)
->setOrderByPostDate()
->load();
if ($collection->count() > 0) {
$this->setPreviousPost($collection->getFirstItem());
}
}
return $this->_getData('previous_post');
}
/**
* Retrieve the next post
*
* @return false|Fishpig_Wordpress_Model_Post
*/
public function getNextPost()
{
if (!$this->hasNextPost()) {
$this->setNextPost(false);
$collection = Mage::getResourceModel('wordpress/post_collection')
->addIsViewableFilter()
->addPostTypeFilter($this->getPostType())
->addPostDateFilter(array('gt' => $this->_getData('post_date')))
->setPageSize(1)
->setCurPage(1)
->setOrderByPostDate('asc')
->load();
if ($collection->count() > 0) {
$this->setNextPost($collection->getFirstItem());
}
}
return $this->_getData('next_post');
}
public function isType($type)
{
return $this->getPostType() === $type;
}
public function getTypeInstance()
{
if (!$this->hasTypeInstance() && $this->getPostType()) {
if ($this->getPostType() === 'revision') {
if ($this->getParentPost()) {
$this->setTypeInstance(
$this->getParentPost()->getTypeInstance()
);
}
}
else if ($postTypes = Mage::helper('wordpress/app')->getPostTypes()) {
$this->setTypeInstance(
isset($postTypes[$this->getPostType()]) ? $postTypes[$this->getPostType()] : false
);
}
}
return $this->_getData('type_instance');
}
/**
* Inject string 'Protected: ' on password protected posts
*
* @return string
*/
public function getPostTitle()
{
if ($this->getPostPassword() !== '') {
return Mage::helper('wordpress')->__('Protected: %s', $this->_getData('post_title'));
}
return $this->_getData('post_title');
}
/**
* Retrieve the URL for the comments feed
*
* @return string
*/
public function getCommentFeedUrl()
{
return rtrim($this->getPermalink(), '/') . '/feed/';
}
/**
* Gets the post content
*
* @return string
*/
public function getPostContent($context = 'full')
{
$key = rtrim('filtered_post_content_' . $context, '_');
if (!$this->hasData($key)) {
$this->setData($key, Mage::helper('wordpress/filter')->applyFilters($this->_getData('post_content'), $this, $context));
}
return $this->_getData($key);
}
/**
* Returns a collection of comments for this post
*
* @return Fishpig_Wordpress_Model_Mysql4_Post_Comment_Collection
*/
public function getComments()
{
if (!$this->hasData('comments')) {
$this->setData('comments', $this->getResource()->getPostComments($this));
}
return $this->getData('comments');
}
/**
* Returns a collection of images for this post
*
* @return Fishpig_Wordpress_Model_Mysql4_Image_Collection
*
* NB. This function has not been thoroughly tested
* Please report any bugs
*/
public function getImages()
{
if (!$this->hasData('images')) {
$this->setImages(Mage::getResourceModel('wordpress/image_collection')->setParent($this->getData('ID')));
}
return $this->getData('images');
}
/**
* Returns the featured image for the post
*
* This image must be uploaded and assigned in the WP Admin
*
* @return Fishpig_Wordpress_Model_Image
*/
public function getFeaturedImage()
{
if (!$this->hasData('featured_image')) {
$this->setFeaturedImage($this->getResource()->getFeaturedImage($this));
}
return $this->getData('featured_image');
}
/**
* Get the model for the author of this post
*
* @return Fishpig_Wordpress_Model_Author
*/
public function getAuthor()
{
return Mage::getModel('wordpress/user')->load($this->getAuthorId());
}
/**
* Returns the author ID of the current post
*
* @return int
*/
public function getAuthorId()
{
return $this->getData('post_author');
}
/**
* Returns the post date formatted
* If not format is supplied, the format specified in your Magento config will be used
*
* @return string
*/
public function getPostDate($format = null)
{
if (($date = $this->getData('post_date_gmt')) === '0000-00-00 00:00:00' || $date === '') {
$date = now();
}
return Mage::helper('wordpress')->formatDate($date, $format);
}
/**
* Returns the post date formatted
* If not format is supplied, the format specified in your Magento config will be used
*
* @return string
*/
public function getPostModifiedDate($format = null)
{
if (($date = $this->getData('post_modified_gmt')) === '0000-00-00 00:00:00' || $date === '') {
$date = now();
}
return Mage::helper('wordpress')->formatDate($date, $format);
}
/**
* Returns the post time formatted
* If not format is supplied, the format specified in your Magento config will be used
*
* @return string
*/
public function getPostTime($format = null)
{
if (($date = $this->getData('post_date_gmt')) === '0000-00-00 00:00:00' || $date === '') {
$date = now();
}
return Mage::helper('wordpress')->formatDate($date, $format);
}
/**
* Determine whether the post has been published
*
* @return bool
*/
public function isPublished()
{
return $this->getPostStatus() == 'publish';
}
/**
* Determine whether the post has been published
*
* @return bool
*/
public function isPending()
{
return $this->getPostStatus() == 'pending';
}
/**
* Retrieve the preview URL
*
* @return string
*/
public function getPreviewUrl()
{
if ($this->isPending()) {
return Mage::helper('wordpress')->getUrl('?p=' . $this->getId() . '&preview=1');
}
return '';
}
/**
* Determine whether the current user can view the post/page
* If visibility is protected and user has supplied wrong password, return false
*
* @return bool
*/
public function isViewableForVisitor()
{
return $this->getPostPassword() === ''
|| Mage::getSingleton('core/session')->getPostPassword() == $this->getPostPassword();
}
/**
* Determine whether the post is a sticky post
* This only works if the post collection has been loaded with addStickyPostsToCollection
*
* @return bool
*/
public function isSticky()
{
return $this->_getData('is_sticky');
}
/**
* Determine whether a post object can be viewed
*
* @return string
*/
public function canBeViewed()
{
return $this->isPublished()
|| ($this->getPostStatus() === 'private' && Mage::getSingleton('customer/session')->isLoggedIn());
}
/**
* Wrapper for self::getPermalink()
*
* @return string
*/
public function getUrl()
{
if (!$this->hasUrl()) {
$this->setUrl($this->getGuid());
if ($this->hasPermalink()) {
$this->setUrl(Mage::helper('wordpress')->getUrl($this->_getData('permalink')));
}
else if ($this->getTypeInstance()->isHierarchical()) {
if ($uris = $this->getTypeInstance()->getAllRoutes()) {
if (isset($uris[$this->getId()])) {
$this->setUrl(Mage::helper('wordpress')->getUrl($uris[$this->getId()] . '/'));
}
}
}
}
return $this->_getData('url');
}
public function getParentId()
{
return (int)$this->_getData('post_parent');
}
/**
* Retrieve the parent page
*
* @return false|Fishpig_Wordpress_Model_Post
*/
public function getParentPost()
{
if (!$this->hasParentPost()) {
$this->setParentPost(false);
if ($this->getParentId()) {
$parent = Mage::getModel('wordpress/post')
->setPostType($this->getPostType() === 'revision' ? '*' : $this->getPostType())
->load($this->getParentId());
if ($parent->getId()) {
$this->setParentPost($parent);
}
}
}
return $this->_getData('parent_post');
}
/**
* Retrieve the page's children pages
*
* @return Fishpig_Wordpress_Model_Mysql_Page_Collection
*/
public function getChildrenPosts()
{
return $this->getCollection()
->addPostParentIdFilter($this->getId());
}
/**
* Determine whether children exist
*
* @return bool
*/
public function hasChildrenPosts()
{
return $this->getResource()->hasChildrenPosts($this);
}
/**
* The methods here are legacy methods that have been ported over from the old Page class
* These are deprecated and will be removed shortly.
*/
public function getMenuLabel()
{
return $this->getPostTitle();
}
public function getParentPage()
{
return $this->isType('page')
? $this->getParentPost()
: false;
}
public function hasChildren()
{
return $this->hasChildrenPosts();
}
public function getChildren()
{
return $this->getChildrenPosts();
}
public function isHomepagePage()
{
return $this->isType('page') && (int)$this->getId() === (int)Mage::helper('wordpress/router')->getHomepagePageId();
}
public function isBlogListingPage()
{
return $this->isType('page') && (int)$this->getId() === (int)Mage::helper('wordpress/router')->getBlogPageId();
}
}