PostHooks.php
1.24 KB
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
<?php
namespace Favorites\Entities\Post;
use Favorites\Config\SettingsRepository;
use Favorites\Entities\Favorite\FavoriteButton;
/**
* Post Actions and Filters
*/
class PostHooks
{
/**
* Settings Repository
*/
private $settings_repo;
/**
* The Content
*/
private $content;
/**
* The Post Object
*/
private $post;
public function __construct()
{
$this->settings_repo = new SettingsRepository;
add_filter('the_content', [$this, 'filterContent']);
}
/**
* Filter the Content
*/
public function filterContent($content)
{
global $post;
if ( !$post ) return $content;
$this->post = $post;
$this->content = $content;
$display = $this->settings_repo->displayInPostType($post->post_type);
if ( !$display ) return $content;
return $this->addFavoriteButton($display);
}
/**
* Add the Favorite Button
* @todo add favorite button html
*/
private function addFavoriteButton($display_in)
{
$output = '';
if ( isset($display_in['before_content']) && $display_in['before_content'] == 'true' ){
$output .= get_favorites_button();
}
$output .= $this->content;
if ( isset($display_in['after_content']) && $display_in['after_content'] == 'true' ){
$output .= get_favorites_button();
}
return $output;
}
}