Skip to content
This repository
tree: d7ecf1aed0
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 230 lines (195 sloc) 5.619 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 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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
<?php

/**
* Enable user commenting for a URL
*
* @since 1.1
* @link https://developers.facebook.com/docs/reference/plugins/comments/ Facebook Comments Box
*/
class Facebook_Comments_Box {

/**
* Element and class name used in markup builders
*
* @since 1.1
* @var string
*/
const ID = 'comments';

/**
* Override the URL related to this comment.
* Default is og:url or link[rel=canonical] or document.URL
*
* @since 1.1
* @var string
*/
protected $href;

/**
* Define a custom width in whole pixels
*
* @since 1.1
* @var int
*/
protected $width;

/**
* Choose a light or dark color scheme to match your site style
*
* @since 1.1.11
* @param string
*/
protected $colorscheme;

/**
* Use a light or dark color scheme
*
* @since 1.1.11
* @var array
*/
public static $colorscheme_choices = array( 'light', 'dark' );

/**
* The number of comments to show by default
*
* @since 1.1
* @var int
*/
protected $num_posts;

/**
* Should we force the mobile-optimized version?
* Default is auto-detect
*/
protected $mobile;

/**
* I am a comments box
*
* @since 1.1
*/
public function __toString() {
return 'Facebook Comments Box Social Plugin';
}

/**
* Setter for href attribute
*
* @since 1.1
* @param string $url absolute URL
* @return Facebook_Comments_Box support chaining
*/
public function setURL( $url ) {
$url = esc_url_raw( $url, array( 'http', 'https' ) );
if ( $url )
$this->href = $url;
return $this;
}

/**
* Width of the like button
* Should be greater than the minimum width of layout + send button (if enabled) + recommend text (if chosen)
*
* @since 1.1
* @param int $width width in whole pixels
* @return Facebook_Comments_Box support chaining
*/
public function setWidth( $width ) {
// narrowest recommended width is 400
if ( is_int( $width ) && $width > 400 )
$this->width = $width;
return $this;
}

/**
* Choose a light or dark color scheme
*
* @since 1.1
* @see self::colorscheme_choices
* @param string $color_scheme light|dark
* @return Facebook_Comments_Box support chaining
*/
public function setColorScheme( $color_scheme ) {
if ( is_string( $color_scheme ) && in_array( $color_scheme, self::$colorscheme_choices, true ) )
$this->colorscheme = $color_scheme;
return $this;
}

/**
* The maximum number of comments to display by default
*
* @since 1.1
* @param int $num positive number of comments
* @return Facebook_Comments_Box support chaining
*/
public function setNumPosts( $num ) {
if ( is_int( $num ) && $num > 0 )
$this->num_posts = $num;
return $this;
}

/**
* Force the mobile view of comments
*
* @return Facebook_Comments_Box support chaining
*/
public function forceMobile() {
$this->mobile = true;
return $this;
}

/**
* convert an options array into an object
*
* @since 1.1
* @param array $values associative array
* @return Facebook_Comments_Box comments box object
*/
public static function fromArray( $values ) {
if ( ! is_array( $values ) || empty( $values ) )
return;

$comments_box = new Facebook_Comments_Box();

if ( isset( $values['href'] ) && is_string( $values['href'] ) )
$comments_box->setURL( $values['href'] );

if ( isset( $values['width'] ) )
$comments_box->setWidth( absint( $values['width'] ) );

if ( isset( $values['num_posts'] ) )
$comments_box->setNumPosts( absint( $values['num_posts'] ) );

if ( isset( $values['colorscheme'] ) )
$comments_box->setColorScheme( $values['colorscheme'] );

if ( isset( $values['mobile'] ) && ( $values['mobile'] === true || $values['mobile'] === 'true' || $values['mobile'] == 1 ) )
$comments_box->forceMobile();

return $comments_box;
}

/**
* Convert the class to data-* attribute friendly associative array
* will become data-key="value"
* Exclude values if default
*
* @return array associative array
*/
public function toHTMLDataArray() {
$data = array();

if ( isset( $this->href ) )
$data['href'] = $this->href;

if ( isset( $this->width ) && is_int( $this->width ) && $this->width > 0 )
$data['width'] = $this->width;

if ( isset( $this->colorscheme ) && $this->colorscheme !== 'light' )
$data['colorscheme'] = $this->colorscheme;

if ( isset( $this->num_posts ) && is_int( $this->num_posts ) && $this->num_posts > 0 && $this->num_posts !== 10 )
$data['num-posts'] = $this->num_posts;

if ( isset( $this->mobile ) && $this->mobile === true )
$data['mobile'] = 'true';

return $data;
}

/**
* Output Like button with data-* attributes
*
* @param array $div_attributes associative array. customize the returned div with id, class, or style attributes. social plugin parameters in data.
* @return string HTML div or empty string
*/
public function asHTML( $div_attributes=array() ) {
if ( ! class_exists( 'Facebook_Social_Plugin' ) )
require_once( dirname(__FILE__) . '/class-facebook-social-plugin.php' );

$div_attributes = Facebook_Social_Plugin::add_required_class( 'fb-' . self::ID, $div_attributes );
$div_attributes['data'] = $this->toHTMLDataArray();

return Facebook_Social_Plugin::div_builder( $div_attributes );
}

/**
* Output XFBML element with attributes
*
* @since 1.1
* @return string XFBML element or empty string
*/
public function asXFBML() {
if ( ! class_exists( 'Facebook_Social_Plugin' ) )
require_once( dirname(__FILE__) . '/class-facebook-social-plugin.php' );

return Facebook_Social_Plugin::xfbml_builder( self::ID, $this->toHTMLDataArray() );
}
}

?>
Something went wrong with that request. Please try again.