SB_Analytics.php
147 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * SB_Analytics plugin integration |
| 5 | * Class to impelement filters to return |
| 6 | * data needed in the SB_Analytics plugin |
| 7 | */ |
| 8 | |
| 9 | namespace CustomFacebookFeed\Integrations\Analytics; |
| 10 | |
| 11 | use CustomFacebookFeed\Builder\CFF_Db; |
| 12 | |
| 13 | if (!defined('ABSPATH')) { |
| 14 | exit; // Exit if accessed directly |
| 15 | } |
| 16 | |
| 17 | class SB_Analytics |
| 18 | { |
| 19 | /** |
| 20 | * Summary of current_plugin |
| 21 | * |
| 22 | * @var string |
| 23 | */ |
| 24 | private static $current_plugin = 'facebook'; |
| 25 | |
| 26 | |
| 27 | /** |
| 28 | * Summary of __construct |
| 29 | */ |
| 30 | public function __construct() |
| 31 | { |
| 32 | $this->init(); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Summary of init |
| 37 | * |
| 38 | * @return void |
| 39 | */ |
| 40 | public function init() |
| 41 | { |
| 42 | // Filter Top Posts |
| 43 | add_filter( |
| 44 | 'sb_analytics_filter_top_posts', |
| 45 | [$this, 'filter_top_posts'], |
| 46 | 10, |
| 47 | 3 |
| 48 | ); |
| 49 | |
| 50 | // Filter Profile Details |
| 51 | add_filter( |
| 52 | 'sb_analytics_filter_profile_details', |
| 53 | [$this, 'filter_profile_details'], |
| 54 | 10, |
| 55 | 3 |
| 56 | ); |
| 57 | |
| 58 | // Filter Feed Lists |
| 59 | add_filter( |
| 60 | 'sb_analytics_filter_feed_list', |
| 61 | [$this, 'filter_feed_list'], |
| 62 | 10, |
| 63 | 3 |
| 64 | ); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Summary of filter_top_posts |
| 69 | * |
| 70 | * @param mixed $posts |
| 71 | * @param mixed $post_ids |
| 72 | * @param mixed $plugin_slug |
| 73 | * |
| 74 | * @return mixed |
| 75 | */ |
| 76 | public function filter_top_posts($posts, $post_ids, $plugin_slug) |
| 77 | { |
| 78 | if ($plugin_slug !== self::$current_plugin) { |
| 79 | return $posts; |
| 80 | } |
| 81 | |
| 82 | return CFF_Db::get_posts_by_ids($post_ids); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Summary of filter_profile_details |
| 87 | * |
| 88 | * @param mixed $profile_details |
| 89 | * @param mixed $feed_id |
| 90 | * @param mixed $plugin_slug |
| 91 | * |
| 92 | * @return mixed |
| 93 | */ |
| 94 | public function filter_profile_details($profile_details, $feed_id, $plugin_slug) |
| 95 | { |
| 96 | if ($plugin_slug !== self::$current_plugin) { |
| 97 | return $profile_details; |
| 98 | } |
| 99 | |
| 100 | $source = CFF_Db::get_feed_source_info($feed_id); |
| 101 | if (empty($source) || empty($source['name'])) { |
| 102 | return []; |
| 103 | } |
| 104 | |
| 105 | return [ |
| 106 | 'id' => $source['id'], |
| 107 | 'pluginSlug' => self::$current_plugin, |
| 108 | 'profile' => [ |
| 109 | 'label' => $source['name'], |
| 110 | 'imageSrc' => $source['picture'] |
| 111 | ] |
| 112 | ]; |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Summary of filter_feed_list |
| 117 | * |
| 118 | * @param mixed $feeds |
| 119 | * @param mixed $plugin_slug |
| 120 | * |
| 121 | * @return mixed |
| 122 | */ |
| 123 | public function filter_feed_list($feeds, $plugin_slug) |
| 124 | { |
| 125 | if ($plugin_slug !== self::$current_plugin) { |
| 126 | return $feeds; |
| 127 | } |
| 128 | |
| 129 | $results = []; |
| 130 | $db_fees = CFF_Db::all_feeds_query(); // Get All stored Feeds |
| 131 | |
| 132 | // Transform result feed schema |
| 133 | foreach ($db_fees as $feed) { |
| 134 | array_push( |
| 135 | $results, |
| 136 | [ |
| 137 | 'value' => [ |
| 138 | 'feed_id' => $feed['id'], |
| 139 | ], |
| 140 | 'label' => $feed['feed_name'], |
| 141 | ] |
| 142 | ); |
| 143 | } |
| 144 | return $results; |
| 145 | } |
| 146 | } |
| 147 |