| 1 |
<?php |
| 2 |
namespace MBSocial; |
| 3 |
defined('ABSPATH') or die('No direct access permitted'); |
| 4 |
|
| 5 |
$collectionBlock["network"] = array('order' => 10, |
| 6 |
'class' => "networkBlock"); |
| 7 |
|
| 8 |
use \simple_html_dom as simple_html_dom; |
| 9 |
use \MaxButtons\maxField as maxField; |
| 10 |
use \MaxButtons\maxBlocks as maxBlocks; |
| 11 |
|
| 12 |
class networkBlock extends block |
| 13 |
{ |
| 14 |
protected $blockname = "network"; |
| 15 |
protected $fields = array( 'network_active' => array('default' => array() ) |
| 16 |
|
| 17 |
); |
| 18 |
|
| 19 |
protected $meta_fields = array( |
| 20 |
'share_url' => array('default' => ''), |
| 21 |
); |
| 22 |
|
| 23 |
protected $share_data; |
| 24 |
|
| 25 |
|
| 26 |
public function parse($domObj, $args = array() ) |
| 27 |
{ |
| 28 |
$output = ''; |
| 29 |
$blockdata = $this->data[$this->blockname]; |
| 30 |
$admin = MBSocial()->admin(); |
| 31 |
|
| 32 |
//$share_data = $this->getShareData(); |
| 33 |
|
| 34 |
$itemObj = $domObj->find('a', 0); |
| 35 |
$itemObj->href = $this->applyVars($itemObj->href); |
| 36 |
|
| 37 |
$network = $args['network']; |
| 38 |
$popup = $network->get('popup'); |
| 39 |
|
| 40 |
if ($popup) |
| 41 |
{ |
| 42 |
$tag = 'data-popup'; |
| 43 |
$dimensions = $network->get('popup_dimensions'); |
| 44 |
$dimensions = array('width' => $dimensions[0], 'height' => $dimensions[1]); |
| 45 |
$itemObj->$tag = htmlentities(json_encode($dimensions)); |
| 46 |
} |
| 47 |
|
| 48 |
/* Not working like this. |
| 49 |
|
| 50 |
if ($network->forMobile() ) |
| 51 |
{ |
| 52 |
$itemObj->class .= ' for-mobile '; |
| 53 |
} |
| 54 |
if ($network->forDesktop() ) |
| 55 |
{ |
| 56 |
$itemObj->class .= ' for-desktop '; |
| 57 |
} |
| 58 |
*/ |
| 59 |
|
| 60 |
/* |
| 61 |
$btn_display = ''; |
| 62 |
|
| 63 |
$buttons = array(); |
| 64 |
|
| 65 |
$active = $blockdata['active']; |
| 66 |
$networks = $admin->get_networks($active); |
| 67 |
//echo "<PRE>"; print_R($domObj); echo "</PRE>"; |
| 68 |
|
| 69 |
foreach($networks as $index => $network) |
| 70 |
{ |
| 71 |
|
| 72 |
|
| 73 |
} |
| 74 |
|
| 75 |
foreach($buttons as $button) |
| 76 |
{ |
| 77 |
// $button->reloadData(); |
| 78 |
$button_id = $button->getID(); |
| 79 |
|
| 80 |
$document_id = $button->getDocumentID(); |
| 81 |
|
| 82 |
$default_args = array("mode" => "normal", |
| 83 |
"compile" => false, |
| 84 |
"load_type" => "footer", |
| 85 |
); |
| 86 |
$button_args = wp_parse_args($args, $default_args); |
| 87 |
$button_args["load_css"] = $button_args["load_type"]; |
| 88 |
|
| 89 |
$button_args["echo"] = false; // non-optional. |
| 90 |
|
| 91 |
if (isset($args['preview']) && $args["preview"]) |
| 92 |
$button_args["mode"] = "preview"; // buttons work with mode. |
| 93 |
|
| 94 |
$btn_display .= "<span class='mb-collection-item item-$button_id ' data-doc-id='" . $document_id . "'>" . $button->display($button_args) . "</span>"; |
| 95 |
|
| 96 |
|
| 97 |
} |
| 98 |
|
| 99 |
$collectionObj->innertext = $btn_display; |
| 100 |
$domObj->load($domObj->save()); |
| 101 |
*/ |
| 102 |
return $domObj; |
| 103 |
|
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
/** Get data from the page to use in sharing. Like page title, url and image |
| 110 |
|
| 111 |
**/ |
| 112 |
public function getShareData() |
| 113 |
{ |
| 114 |
return $this->setShareData(); |
| 115 |
} |
| 116 |
|
| 117 |
public function setShareData() |
| 118 |
{ |
| 119 |
if (! is_null($this->share_data)) |
| 120 |
return $this->share_data; |
| 121 |
|
| 122 |
$url = ''; |
| 123 |
$title = ''; |
| 124 |
$img = ''; |
| 125 |
|
| 126 |
$hook = $this->collection->getHook(); |
| 127 |
|
| 128 |
$share_setting = 'auto'; |
| 129 |
|
| 130 |
if ( isset($this->data['display'][$hook . '_options'])) |
| 131 |
{ |
| 132 |
$options = $this->data['display'][$hook . '_options']; |
| 133 |
|
| 134 |
$share_setting = isset($options['share_setting']) ? $options['share_setting'] : 'auto'; |
| 135 |
$custom = isset($options['share_custom_url']) ? $options['share_custom_url'] : ''; |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
switch($share_setting) |
| 140 |
{ |
| 141 |
case "home": |
| 142 |
$url = get_home_url(); |
| 143 |
$title = get_bloginfo('name'); |
| 144 |
$img = $this->getImage(null, true); |
| 145 |
break; |
| 146 |
case "custom-url"; |
| 147 |
$url = $custom; |
| 148 |
$img = $this->getImage(); |
| 149 |
break; |
| 150 |
case "current-post": |
| 151 |
$url = get_permalink(); |
| 152 |
$title = get_the_title(); |
| 153 |
$img = $this->getImage(); |
| 154 |
break; |
| 155 |
case "auto": |
| 156 |
default: |
| 157 |
|
| 158 |
|
| 159 |
/* After / before being placed on the post data, therefore by default (auto) always use the POST URL |
| 160 |
Static is being placed once(1) on the page, so get the greated to-share url ( of the whole section ) */ |
| 161 |
if ($this->collection->is_post) |
| 162 |
{ |
| 163 |
$url = get_permalink(); |
| 164 |
$title = get_the_title(); |
| 165 |
$img = $this->getImage(); |
| 166 |
|
| 167 |
} |
| 168 |
if ($this->collection->is_static) |
| 169 |
{ |
| 170 |
|
| 171 |
if (is_front_page()) |
| 172 |
{ |
| 173 |
$url = get_home_url(); |
| 174 |
$title = get_bloginfo('name'); |
| 175 |
$img = $this->getImage(null, true); |
| 176 |
} |
| 177 |
else // are there any other cases? Page / Archive should give back their main url like this. |
| 178 |
{ |
| 179 |
$url = get_permalink(); |
| 180 |
$title = get_the_title(); |
| 181 |
$img = $this->getImage(); |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
break; |
| 186 |
} |
| 187 |
$data = array("url" => $url, |
| 188 |
"title" => $title, |
| 189 |
"img" => $img); |
| 190 |
|
| 191 |
$this->share_data = $data; |
| 192 |
return $data; |
| 193 |
|
| 194 |
} |
| 195 |
|
| 196 |
protected function getImage($post_id = null, $home = false) |
| 197 |
{ |
| 198 |
if (is_admin()) |
| 199 |
return ''; // in admin all this is not set. |
| 200 |
|
| 201 |
if ($home && get_option('show_on_front') === 'page') |
| 202 |
{ |
| 203 |
$post_id = get_option('page_on_front'); |
| 204 |
|
| 205 |
} |
| 206 |
|
| 207 |
$thumb_id = get_post_thumbnail_id($post_id); // First try to find thumbnail on the content |
| 208 |
|
| 209 |
$image = false; |
| 210 |
if (is_numeric($thumb_id)) |
| 211 |
{ |
| 212 |
$image = wp_get_attachment_url($thumb_id); |
| 213 |
} |
| 214 |
|
| 215 |
if ($image !== false) |
| 216 |
return $image; |
| 217 |
|
| 218 |
// In case of no thumbnails, tries to find first image from current content. |
| 219 |
|
| 220 |
// if (! is_null($post_id)) |
| 221 |
// { |
| 222 |
$post = get_post($post_id); |
| 223 |
$content = ''; |
| 224 |
if (! is_null($post)) |
| 225 |
$content = $post->post_content; |
| 226 |
/* } |
| 227 |
else |
| 228 |
{ |
| 229 |
|
| 230 |
$content = get_the_content(); |
| 231 |
} */ |
| 232 |
$domObj = new simple_html_dom(); |
| 233 |
$domObj->load($content); |
| 234 |
|
| 235 |
$img = $domObj->find('img', 0); |
| 236 |
if ($img) |
| 237 |
{ |
| 238 |
$image = $img->src; |
| 239 |
return $image; |
| 240 |
} |
| 241 |
else |
| 242 |
return ''; |
| 243 |
} |
| 244 |
|
| 245 |
public function applyVars($string) |
| 246 |
{ |
| 247 |
$vars = $this->getShareData(); |
| 248 |
|
| 249 |
|
| 250 |
if (isset($vars["url"])) |
| 251 |
$string = str_replace("{url}", urlencode(esc_url($vars["url"])), $string); |
| 252 |
if (isset($vars["count"])) |
| 253 |
{ |
| 254 |
$count = $this->formatCount($vars["count"]); |
| 255 |
$string = str_replace("{count}", "<span class='share_count'>" . $count . "</span>", $string); |
| 256 |
$string = str_replace("{c}", "<span class='share_count'>" . $count . "</span>", $string); |
| 257 |
} |
| 258 |
if (isset($vars["title"])) |
| 259 |
$string = str_replace("{title}", $vars["title"], $string); |
| 260 |
if (isset($vars["network_name"])) |
| 261 |
$string = str_replace("{network_name}", $vars["network_name"], $string); |
| 262 |
|
| 263 |
if (isset($vars["img"])) |
| 264 |
$string = str_replace("{img}", $vars["img"], $string); |
| 265 |
|
| 266 |
return $string; |
| 267 |
} |
| 268 |
|
| 269 |
function save_fields($data, $post) |
| 270 |
{ |
| 271 |
$data = parent::save_fields($data, $post); |
| 272 |
|
| 273 |
$selection = array(); |
| 274 |
|
| 275 |
$button = MB()->getClass("button"); |
| 276 |
|
| 277 |
$network_active = isset($post['network_item_active']) ? $post['network_item_active'] : null; |
| 278 |
|
| 279 |
// new user, give default networks |
| 280 |
$collection_id = $this->collection->getID(); |
| 281 |
if (count($network_active) == 0 && $collection_id == 0) |
| 282 |
$network_active = array('facebook', 'twitter', 'linkedin'); |
| 283 |
|
| 284 |
$data[$this->blockname]["network_active"] = $network_active; |
| 285 |
return $data; |
| 286 |
} |
| 287 |
|
| 288 |
|
| 289 |
public function do_meta_boxes($content, $post) |
| 290 |
{ |
| 291 |
$admin = mbSocial()->admin(); |
| 292 |
|
| 293 |
$metadata = $this->get_block_meta_data($post->ID); |
| 294 |
|
| 295 |
$url = new maxField(); |
| 296 |
$url->id = 'share_url'; |
| 297 |
$url->name = $url->id; |
| 298 |
$url->label = __('URL to Share', 'mbsocial'); |
| 299 |
$url->note = __('By default the URL of the post is being shared. Leave empty for default', 'mbsocial'); |
| 300 |
$url->value = isset($metadata['share_url']) ? $metadata['share_url'] : ''; |
| 301 |
$url->placeholder = 'https://'; |
| 302 |
|
| 303 |
$admin->addField($url, 'start', 'end'); |
| 304 |
|
| 305 |
$fields = $admin->display_fields(true, true); |
| 306 |
|
| 307 |
|
| 308 |
$content['share'] = array('title' => __('Share Options', 'mbsocial'), |
| 309 |
'icon' => 'alt-share', |
| 310 |
'content' => $fields, |
| 311 |
); |
| 312 |
|
| 313 |
return $content; |
| 314 |
|
| 315 |
} |
| 316 |
|
| 317 |
public function admin() |
| 318 |
{ |
| 319 |
$admin = mbSocial()->admin(); |
| 320 |
$blockdata = $this->data[$this->blockname]; |
| 321 |
|
| 322 |
$active_networks = isset($blockdata['network_active']) ? $blockdata['network_active'] : array(); |
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
$networks = $admin->get_networks($active_networks); |
| 327 |
|
| 328 |
$styleObj = $this->collection->getStyle(); |
| 329 |
|
| 330 |
?> |
| 331 |
<div class='help-side'> |
| 332 |
<h3><?php _e('Social Networks','mbsocial'); ?></h3> |
| 333 |
<p>Drag and drop the networks you want to use into the 'active' box. You can find additional networks under the 'more networks' tab.</p> |
| 334 |
<p>Hold the mouse pointer over the network icon to see the network's name</p> |
| 335 |
</div> |
| 336 |
<div class='options networks option-container' id='networkBlock' data-refresh='styleBlock'> |
| 337 |
<?php |
| 338 |
$field_obj = new maxField('hidden'); |
| 339 |
$field_obj->id = 'network_trigger_change'; |
| 340 |
$field_obj->name = $field_obj->id; |
| 341 |
$field_obj->value = ''; |
| 342 |
$field_obj->placeholder = ''; |
| 343 |
|
| 344 |
$admin->addField($field_obj); |
| 345 |
$admin->addUpdate($field_obj); |
| 346 |
$admin->display_fields(); |
| 347 |
|
| 348 |
?> |
| 349 |
<div class='title'><?php _e('Social Networks','mbsocial') ?> </div> |
| 350 |
<div class='inside'> |
| 351 |
<div class='option active_network'> |
| 352 |
<label><?php _e('Active','mbsocial'); ?></label> |
| 353 |
<div class='drag-area input' data-area='active'> |
| 354 |
<?php if (isset($networks['selected'])): foreach($networks['selected'] as $index => $network): ?> |
| 355 |
|
| 356 |
<?php $network_name = $network->get('network'); |
| 357 |
$icon_output = $styleObj->renderIcon($network); |
| 358 |
|
| 359 |
?> |
| 360 |
<span class="item <?php echo $network_name ?>"> |
| 361 |
<?php echo $icon_output ?> |
| 362 |
|
| 363 |
<input type='hidden' name='network_item_active[]' value='<?php echo $network_name ?>'> |
| 364 |
|
| 365 |
</span> |
| 366 |
|
| 367 |
<?php endforeach; endif; ?> |
| 368 |
</div> |
| 369 |
</div> |
| 370 |
|
| 371 |
|
| 372 |
<div class='option inactive_network'> |
| 373 |
<label><?php _e('Inactive', 'mbsocial'); ?></label> |
| 374 |
<div class='drag-area input' data-area='inactive'> |
| 375 |
<?php if (isset($networks['unselected'])): foreach($networks['unselected'] as $index => $network): ?> |
| 376 |
|
| 377 |
<?php $network_name = $network->get('network'); |
| 378 |
$icon_output = $styleObj->renderIcon($network); |
| 379 |
?> |
| 380 |
<span class="item <?php echo $network_name ?>"> |
| 381 |
<?php echo $icon_output ?> |
| 382 |
<input type='hidden' name='network_item[]' value='<?php echo $network_name ?>'> |
| 383 |
</i> |
| 384 |
</span> |
| 385 |
|
| 386 |
<?php endforeach; endif; ?> |
| 387 |
</div> |
| 388 |
</div> |
| 389 |
|
| 390 |
<div class='option'> |
| 391 |
<label> </label> |
| 392 |
<div class='see-more toggle input'><span class='title'><?php _e('more networks', 'mbsocial'); ?></span> |
| 393 |
<span class='see-more-wrap option toggle-target'> |
| 394 |
|
| 395 |
<div class='drag-area'> |
| 396 |
<?php if (isset($networks['readmore'])): foreach($networks['readmore'] as $index => $network): ?> |
| 397 |
|
| 398 |
<?php $network_name = $network->get('network'); |
| 399 |
$icon_output = $styleObj->renderIcon($network); |
| 400 |
?> |
| 401 |
|
| 402 |
<span class="item <?php echo $network_name ?>" title="<?php echo $network_name ?>"> |
| 403 |
<?php echo $icon_output ?> |
| 404 |
<input type='hidden' name='network_item[]' value='<?php echo $network_name ?>'> |
| 405 |
|
| 406 |
|
| 407 |
</span> |
| 408 |
|
| 409 |
<?php endforeach; endif; ?> |
| 410 |
</div> |
| 411 |
<p><?php _e("Need another network?","mbsocial"); ?> |
| 412 |
<a href='mailto:support@maxfoundry.com'><?php _e('Email us','mbsocial'); ?> </a></p> |
| 413 |
</span> |
| 414 |
|
| 415 |
</div> |
| 416 |
|
| 417 |
</div> |
| 418 |
</div> <!-- inside --> |
| 419 |
</div> <!-- option-container --> |
| 420 |
<?php |
| 421 |
} |
| 422 |
|
| 423 |
} |
| 424 |
|
| 425 |
?> |
| 426 |
|