| 1 |
<?php |
| 2 |
namespace MBSocial; |
| 3 |
defined('ABSPATH') or die('No direct access permitted'); |
| 4 |
|
| 5 |
use \MaxButtons\MaxUtils as MaxUtils; |
| 6 |
|
| 7 |
abstract class mbNetwork |
| 8 |
{ |
| 9 |
|
| 10 |
protected $network; // internal name of network |
| 11 |
protected $priority = 'unselected'; // Default priority of network - selected / unselected / readmore / hidden |
| 12 |
|
| 13 |
//protected $api_url; // URL of API to request information |
| 14 |
protected $countable = false; // If network supports count of likes / shares / favs |
| 15 |
protected $count_api = ''; // API URL for requesting counts |
| 16 |
protected $count_check_time = 14400; // (PHP 5.3) 4 * HOUR_IN_SECONDS; // Transient time, how often to check network for new counts |
| 17 |
protected $share_url; // URL to link to when sharing something |
| 18 |
protected $popup = true; // Open in Popup or not |
| 19 |
protected $popup_dimensions = array(400,300); // Default popup dimensions |
| 20 |
protected $return_var; // Count API return var ( JSON format ) |
| 21 |
|
| 22 |
protected $displayMobile = true; |
| 23 |
protected $displayDesktop = true; |
| 24 |
|
| 25 |
protected $nice_name; |
| 26 |
|
| 27 |
protected $label; |
| 28 |
|
| 29 |
protected $icon_type = 'fa'; |
| 30 |
protected $icon = 'fa-circle'; |
| 31 |
|
| 32 |
abstract public function __construct(); |
| 33 |
|
| 34 |
|
| 35 |
public function get($name) |
| 36 |
{ |
| 37 |
if (isset($this->$name)) |
| 38 |
return $this->$name; |
| 39 |
else |
| 40 |
return null; |
| 41 |
|
| 42 |
} |
| 43 |
|
| 44 |
public function forMobile() |
| 45 |
{ |
| 46 |
return $this->displayMobile; |
| 47 |
} |
| 48 |
|
| 49 |
public function forDesktop() |
| 50 |
{ |
| 51 |
return $this->displayDesktop; |
| 52 |
} |
| 53 |
|
| 54 |
public function isCountable() |
| 55 |
{ |
| 56 |
return $this->countable; |
| 57 |
} |
| 58 |
|
| 59 |
public function get_nice_name() |
| 60 |
{ |
| 61 |
if (isset($this->nice_name)) |
| 62 |
return $this->nice_name; |
| 63 |
else |
| 64 |
return ucfirst($this->network); |
| 65 |
|
| 66 |
} |
| 67 |
|
| 68 |
public function getRemoteShareCount($share_url) |
| 69 |
{ |
| 70 |
if (! $this->countable) |
| 71 |
return false; |
| 72 |
|
| 73 |
$count_api = $this->count_api; |
| 74 |
|
| 75 |
if ($count_api == '') return false; // no api |
| 76 |
|
| 77 |
$network = $this->network; |
| 78 |
$timeout = 60; // prevent the same requests from running multiple times ( i.e. one page, many collections on same url ) . |
| 79 |
$locked = maxUtils::get_transient('shares-' . $network . '-' . $share_url. '-lock'); |
| 80 |
|
| 81 |
if ($locked == true) |
| 82 |
return 'locked'; // try again on next refresh. |
| 83 |
|
| 84 |
//lock out next request while this one is still running. |
| 85 |
maxUtils::set_transient('shares-' . $network . '-' . $share_url . '-lock', true, $timeout ); |
| 86 |
|
| 87 |
$count_api = str_replace("{url}", $share_url, $count_api); |
| 88 |
|
| 89 |
$count = $this->remoteRequest($count_api); |
| 90 |
|
| 91 |
if (defined('MAXBUTTONS_DEBUG') && MAXBUTTONS_DEBUG) |
| 92 |
{ |
| 93 |
$admin = MB()->getClass("admin"); |
| 94 |
$admin->log("Get Remote Share", "Call: $count_api - Network : " . $this->network . " - Count: $count \n "); |
| 95 |
} |
| 96 |
|
| 97 |
if ($count !== false) |
| 98 |
{ |
| 99 |
$network = $this->network; |
| 100 |
$check_time = $this->count_check_time; |
| 101 |
|
| 102 |
// set count |
| 103 |
maxUtils::set_transient('shares-' . $network . '-' . $share_url, $count, $check_time ); |
| 104 |
|
| 105 |
} |
| 106 |
|
| 107 |
// remove lock |
| 108 |
maxUtils::delete_transient('shares-' . $network . '-' . $share_url . '-lock'); |
| 109 |
|
| 110 |
return $count; |
| 111 |
} |
| 112 |
|
| 113 |
protected function remoteRequest($url) |
| 114 |
{ |
| 115 |
$response = wp_remote_get($url); |
| 116 |
$result_path = $this->return_var; |
| 117 |
$result_array = explode("|",$result_path); |
| 118 |
if (count($result_array) == 0) |
| 119 |
$result_array = array($result_path); |
| 120 |
|
| 121 |
if (is_wp_error($response) || $response['response']['code'] != 200) { |
| 122 |
return false; |
| 123 |
} |
| 124 |
else { |
| 125 |
$result = wp_remote_retrieve_body($response); |
| 126 |
} |
| 127 |
$result = json_decode($result, true); |
| 128 |
|
| 129 |
|
| 130 |
foreach($result_array as $result_val) |
| 131 |
{ |
| 132 |
if (isset($result[$result_val])) |
| 133 |
$result = $result[$result_val]; |
| 134 |
|
| 135 |
} |
| 136 |
if (is_int($result)) |
| 137 |
return $result; |
| 138 |
|
| 139 |
return 0; // some networks don't return the json return var. Only return false on network errors |
| 140 |
|
| 141 |
} |
| 142 |
|
| 143 |
public function getShareCount($args = array()) |
| 144 |
{ |
| 145 |
if ( $this->count_api == '') |
| 146 |
return 0; // no api - count always zero. |
| 147 |
|
| 148 |
$defaults = array( |
| 149 |
"url" => "", |
| 150 |
"preview" => false, |
| 151 |
"force_share_update" => false, |
| 152 |
|
| 153 |
); |
| 154 |
|
| 155 |
$args = wp_parse_args($args,$defaults); |
| 156 |
|
| 157 |
$share_url = esc_url($args["url"]); |
| 158 |
$network = $this->network; |
| 159 |
//$count = get_transient('mb-col-' . $network . '-' . $share_url . '-shares'); |
| 160 |
$count = maxUtils::get_transient('shares-' . $network . '-' . $share_url); |
| 161 |
|
| 162 |
if ($args["force_share_update"]) |
| 163 |
$count = -1; // force an update |
| 164 |
|
| 165 |
if ( ($count === false || $count == -1) && ! $args["preview"]) |
| 166 |
{ // request from external - this is done via ajax on runtime. |
| 167 |
return false; |
| 168 |
} |
| 169 |
|
| 170 |
return $count; |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
/** Function to display additional network-specific options. |
| 175 |
* @return String Option Output. |
| 176 |
**/ |
| 177 |
public function admin() { |
| 178 |
return ''; |
| 179 |
|
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
} |
| 184 |
|