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
<?php
namespace hji\membership\services;
class SocialGraph
{
function __construct()
{
}
function init()
{
$this->setActions();
}
protected function setActions()
{
add_filter('wpseo_metadesc', array($this, 'filterWpSeoMetaDescription'));
add_filter('wpseo_opengraph_type', array($this, 'filterOGtype'));
add_action('wpseo_opengraph', array($this, 'addOGplaceCoordinates'));
}
function filterWpSeoMetaDescription($metadesc)
{
global $post;
if ($this->_isListingPost($post))
{
$meta = array();
if ($post->listing->listPrice)
{
$listPrice = filter_var($post->listing->listPrice, FILTER_SANITIZE_NUMBER_INT);
$meta[] = '$' . number_format($listPrice);
}
if ($post->listing->beds)
{
$meta[] = $post->listing->beds . '-beds';
}
if ($post->listing->baths_total)
{
$meta[] = $post->listing->baths_total . '-baths';
}
$metadesc = implode(' • ', $meta) . '.';
if ($post->listing->description)
{
$metadesc .= ' ' . $post->listing->description;
}
}
$metadesc = apply_filters('hji_socialgraph_metadesc', $metadesc, $post);
return $metadesc;
}
public function filterOGtype($type)
{
global $post;
if ($this->_isListingPost($post))
{
$type = 'place';
}
return $type;
}
public function addOGplaceCoordinates()
{
global $post;
if ($this->_isListingPost($post) && isset($GLOBALS['wpseo_og']))
{
if ($post->listing->coordinates_latitude &&
$post->listing->coordinates_longitude)
{
$GLOBALS['wpseo_og']->og_tag('place:location:latitude', $post->listing->coordinates_latitude);
$GLOBALS['wpseo_og']->og_tag('place:location:longitude', $post->listing->coordinates_longitude);
}
else if ($post->listing->coordinates)
{
$GLOBALS['wpseo_og']->og_tag('place:location:latitude', $post->listing->coordinates->latitude);
$GLOBALS['wpseo_og']->og_tag('place:location:longitude', $post->listing->coordinates->longitude);
}
}
}
protected function _isListingPost($post)
{
if ($post && is_main_query() &&
isset($post->listing) && in_array($post->post_type, $this->_getSupportedPostTypes()))
{
return true;
}
return false;
}
protected function _getSupportedPostTypes()
{
$supportedTypes = array('post', 'nc-listing');
$wpseo_opt = $this->_getWPseoOptions();
if (isset($wpseo_opt[ 'metadesc-nc-listing']) && !empty($wpseo_opt['metadesc-nc-listing']))
{
if ($key = array_search('nc-listing', $supportedTypes))
{
unset($supportedTypes[$key]);
}
}
return $supportedTypes;
}
protected function _getWPseoOptions()
{
$options = array();
if (class_exists('\WPSEO_Options'))
{
$options = \WPSEO_Options::get_all();
}
return $options;
}
}