PageRenderTime 34ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/jetpack/modules/publicize.php

https://gitlab.com/Gashler/sg
PHP | 340 lines | 223 code | 59 blank | 58 comment | 44 complexity | 0f099d0b3dee682b624fff74ee7c74cc MD5 | raw file
  1. <?php
  2. /**
  3. * Module Name: Publicize
  4. * Module Description: Share new posts on social media networks automatically.
  5. * Sort Order: 10
  6. * Recommendation Order: 7
  7. * First Introduced: 2.0
  8. * Requires Connection: Yes
  9. * Auto Activate: Yes
  10. * Module Tags: Social, Recommended
  11. * Feature: Recommended
  12. */
  13. class Jetpack_Publicize {
  14. var $in_jetpack = true;
  15. function __construct() {
  16. global $publicize_ui;
  17. $this->in_jetpack = ( class_exists( 'Jetpack' ) && method_exists( 'Jetpack', 'enable_module_configurable' ) ) ? true : false;
  18. if ( $this->in_jetpack && method_exists( 'Jetpack', 'module_configuration_load' ) ) {
  19. Jetpack::enable_module_configurable( __FILE__ );
  20. Jetpack::module_configuration_load( __FILE__, array( $this, 'jetpack_configuration_load' ) );
  21. add_action( 'init', array( $this, 'sync_posts_init' ), 999 );
  22. }
  23. require_once dirname( __FILE__ ) . '/publicize/publicize.php';
  24. if ( $this->in_jetpack )
  25. require_once dirname( __FILE__ ) . '/publicize/publicize-jetpack.php';
  26. else {
  27. require_once dirname( dirname( __FILE__ ) ) . '/mu-plugins/keyring/keyring.php';
  28. require_once dirname( __FILE__ ) . '/publicize/publicize-wpcom.php';
  29. }
  30. require_once dirname( __FILE__ ) . '/publicize/ui.php';
  31. $publicize_ui = new Publicize_UI();
  32. $publicize_ui->in_jetpack = $this->in_jetpack;
  33. // Jetpack specific checks / hooks
  34. if ( $this->in_jetpack) {
  35. add_action( 'jetpack_activate_module_publicize', array( $this, 'module_state_toggle' ) );
  36. add_action( 'jetpack_deactivate_module_publicize', array( $this, 'module_state_toggle' ) );
  37. add_filter( 'jetpack_sync_post_module_custom_data', array( $this, 'sync_post_module_custom_data' ), 10, 2 );
  38. // if sharedaddy isn't active, the sharing menu hasn't been added yet
  39. $active = Jetpack::get_active_modules();
  40. if ( in_array( 'publicize', $active ) && !in_array( 'sharedaddy', $active ) )
  41. add_action( 'admin_menu', array( &$publicize_ui, 'sharing_menu' ) );
  42. }
  43. }
  44. function sync_posts_init() {
  45. $post_types = array( 'post', 'page' );
  46. $all_post_types = get_post_types();
  47. foreach ( $all_post_types as $post_type ) {
  48. // sync Custom Post Types that support publicize
  49. if ( post_type_supports( $post_type, 'publicize' ) ) {
  50. $post_types[] = $post_type;
  51. }
  52. }
  53. Jetpack_Sync::sync_posts( __FILE__, array(
  54. 'post_types' => $post_types,
  55. ) );
  56. }
  57. function sync_post_module_custom_data( $custom_data, $post ) {
  58. if ( post_type_supports( get_post_type( $post ), 'publicize' ) ) {
  59. $custom_data['cpt_publicizeable'] = true;
  60. }
  61. return $custom_data;
  62. }
  63. function module_state_toggle() {
  64. // extra check that we are on the JP blog, just incase
  65. if ( class_exists( 'Jetpack' ) && $this->in_jetpack ) {
  66. $jetpack = Jetpack::init();
  67. $jetpack->sync->register( 'noop' );
  68. }
  69. }
  70. function jetpack_configuration_load() {
  71. wp_safe_redirect( menu_page_url( 'sharing', false ) );
  72. exit;
  73. }
  74. }
  75. global $publicize_ui;
  76. new Jetpack_Publicize;
  77. /**
  78. * Helper functions for shared use in the services files
  79. */
  80. class Publicize_Util {
  81. /**
  82. * Truncates a string to be shorter than or equal to the length specified
  83. * Attempts to truncate on word boundaries
  84. *
  85. * @param string $string
  86. * @param int $length
  87. * @return string
  88. */
  89. public static function crop_str( $string, $length = 256 ) {
  90. $string = Publicize_Util::sanitize_message( $string );
  91. $length = absint( $length );
  92. if ( mb_strlen( $string, 'UTF-8' ) <= $length ) {
  93. return $string;
  94. }
  95. // @see wp_trim_words()
  96. if ( 'characters' == _x( 'words', 'word count: words or characters?', 'jetpack' ) ) {
  97. return trim( mb_substr( $string, 0, $length - 1, 'UTF-8' ) ) . "\xE2\x80\xA6"; // ellipsis
  98. }
  99. $words = explode( ' ', $string );
  100. $return = '';
  101. while ( strlen( $word = array_shift( $words ) ) ) {
  102. $new_return = $return ? "$return $word" : $word;
  103. $new_return_length = mb_strlen( $new_return, 'UTF-8' );
  104. if ( $new_return_length < $length - 1 ) {
  105. $return = $new_return;
  106. continue;
  107. } elseif ( $new_return_length == $length - 1 ) {
  108. $return = $new_return;
  109. break;
  110. }
  111. if ( !$return ) {
  112. $return = mb_substr( $new_return, 0, $length - 1, 'UTF-8' );
  113. }
  114. break;
  115. }
  116. return "$return\xE2\x80\xA6"; // ellipsis
  117. }
  118. /**
  119. * Returns an array of DOMNodes that are comments (including recursing child nodes)
  120. *
  121. * @param DOMNode $node
  122. * @return array
  123. */
  124. function get_comment_nodes( $node ) {
  125. $comment_nodes = array();
  126. foreach ( $node->childNodes as $child ) {
  127. if ( XML_COMMENT_NODE === $child->nodeType ) {
  128. $comment_nodes[] = $child;
  129. }
  130. if ( $child->hasChildNodes() ) {
  131. $child_comment_nodes = self::get_comment_nodes( $child );
  132. $comment_nodes = array_merge( $comment_nodes, $child_comment_nodes );
  133. }
  134. }
  135. return $comment_nodes;
  136. }
  137. /**
  138. * Truncates HTML so that its textContent (text without markup) is shorter than or equal to the length specified.
  139. * The length of the returned string may be larger than the specified length due to the markup.
  140. * Attempts to truncate on word boundaries.
  141. *
  142. * @param string $string
  143. * @param int $length
  144. * @param array $allowed_tags KSES input
  145. * @return string
  146. */
  147. function crop_html( $string, $length = 256, $allowed_tags = array() ) {
  148. $tags = $GLOBALS['allowedtags']; // Markup allowed in comments...
  149. $tags['img'] = array( // ... plus images ...
  150. 'alt' => true,
  151. 'height' => true,
  152. 'src' => true,
  153. 'width' => true,
  154. );
  155. // ... and some other basics
  156. $tags['p'] = array();
  157. $tags['ul'] = array();
  158. $tags['ol'] = array();
  159. $tags['li'] = array();
  160. $tags['br'] = array();
  161. $tags = array_merge( $tags, $allowed_tags );
  162. // Clean up, then KSES to really lock it down
  163. $string = trim( (string) $string );
  164. $string = preg_replace( '@<(script|style)[^>]*?>.*?</\\1>@si', '', $string );
  165. $string = wp_kses( $string, $tags );
  166. $string = mb_convert_encoding( $string, 'HTML-ENTITIES', 'UTF-8' );
  167. $dom = new DOMDocument( '1.0', 'UTF-8' );
  168. @$dom->loadHTML( "<html><body>$string</body></html>" ); // suppress parser warning
  169. // Strip comment nodes, if any
  170. $comment_nodes = self::get_comment_nodes( $dom->documentElement );
  171. foreach ( $comment_nodes as &$comment_node ) {
  172. $comment_node->parentNode->removeChild( $comment_node );
  173. }
  174. if ( $comment_nodes ) {
  175. // Update the $string (some return paths work from just $string)
  176. $string = $dom->saveHTML();
  177. $string = preg_replace( '/^<!DOCTYPE.+?>/', '', $string );
  178. $string = str_replace( array('<html>', '</html>', '<body>', '</body>' ), array( '', '', '', '' ), $string );
  179. $string = trim( $string );
  180. }
  181. // Find the body
  182. $body = false;
  183. foreach ( $dom->childNodes as $child ) {
  184. if ( XML_ELEMENT_NODE === $child->nodeType && 'html' === strtolower( $child->tagName ) ) {
  185. $body = $child->firstChild;
  186. break;
  187. }
  188. }
  189. if ( !$body ) {
  190. return self::crop_str( $string, $length );
  191. }
  192. // If the text (without the markup) is shorter than $length, just return
  193. if ( mb_strlen( $body->textContent, 'UTF-8' ) <= $length ) {
  194. return $string;
  195. }
  196. $node = false;
  197. do {
  198. $node = self::remove_innermost_last_child( $body, $node_removed_from );
  199. $new_string_length = mb_strlen( $body->textContent, 'UTF-8' );
  200. } while ( $new_string_length > $length );
  201. $new_string = $dom->saveHTML( $body );
  202. $new_string = mb_substr( $new_string, 6, -7, 'UTF-8' ); // 6: <body>, 7: </body>
  203. if ( !$node ) {
  204. return $new_string ? $new_string : self::crop_str( $string, $length );
  205. }
  206. $append_string_length = $length - $new_string_length;
  207. if ( !$append_string_length ) {
  208. return $new_string;
  209. }
  210. if ( $append_string_length > 1 && XML_TEXT_NODE === $node->nodeType ) { // 1: ellipsis
  211. $append_string = self::crop_str( $node->textContent, $append_string_length ); // includes ellipsis
  212. $append_node = $dom->createTextNode( $append_string );
  213. $node_removed_from->appendChild( $append_node );
  214. $new_string = $dom->saveHTML( $body );
  215. $new_string = mb_substr( $new_string, 6, -7, 'UTF-8' );
  216. } elseif ( $append_string_length > 9 && XML_ELEMENT_NODE === $node->nodeType && 'p' == strtolower( $node->nodeName ) ) { // 9: '<p>X{\xE2\x80\xA6}</p>'
  217. $new_string .= '<p>' . self::crop_str( $node->textContent, $append_string_length - 8 ) . '</p>';
  218. }
  219. // Clean up any empty Paragraphs that might have occurred after removing their children
  220. return trim( preg_replace( '#<p>\s*</p>#i', '', $new_string ) );
  221. }
  222. function remove_innermost_last_child( $node, &$node_removed_from ) {
  223. $node_removed_from = $node;
  224. if ( !$node->lastChild ) {
  225. return false;
  226. }
  227. if ( $node->lastChild->hasChildNodes() ) {
  228. return self::remove_innermost_last_child( $node->lastChild, $node_removed_from );
  229. }
  230. $innermost_last_child = $node->lastChild;
  231. $node->removeChild( $innermost_last_child );
  232. return $innermost_last_child;
  233. }
  234. function bump_stats_extras_publicize_url( $bin, $post_id ) {
  235. static $done = array();
  236. if ( isset( $done[$post_id] ) ) {
  237. return;
  238. }
  239. $done[$post_id] = true;
  240. if ( function_exists( 'bump_stats_extras' ) )
  241. bump_stats_extras( 'publicize_url', $bin );
  242. }
  243. public static function build_sprintf( $args ) {
  244. $search = array();
  245. $replace = array();
  246. foreach ( $args as $k => $arg ) {
  247. if ( 0 == $k ) {
  248. $string = $arg;
  249. continue;
  250. }
  251. $search[] = "%$arg%";
  252. $replace[] = "%$k\$s";
  253. }
  254. return str_replace( $search, $replace, $string );
  255. }
  256. public static function sanitize_message( $message ) {
  257. $message = preg_replace( '@<(script|style)[^>]*?>.*?</\\1>@si', '', $message );
  258. $message = wp_kses( $message, array() );
  259. $message = preg_replace('/[\r\n\t ]+/', ' ', $message);
  260. $message = trim( $message );
  261. $message = htmlspecialchars_decode( $message, ENT_QUOTES );
  262. return $message;
  263. }
  264. }
  265. if( ! ( defined( 'IS_WPCOM' ) && IS_WPCOM ) && ! function_exists( 'publicize_init' ) ) {
  266. /**
  267. * Helper for grabbing a Publicize object from the "front-end" (non-admin) of
  268. * a site. Normally Publicize is only loaded in wp-admin, so there's a little
  269. * set up that you might need to do if you want to use it on the front end.
  270. * Just call this function and it returns a Publicize object.
  271. *
  272. * @return Publicize Object
  273. */
  274. function publicize_init() {
  275. global $publicize;
  276. if ( ! class_exists( 'Publicize' ) ) {
  277. require_once dirname( __FILE__ ) . '/publicize/publicize.php';
  278. }
  279. return $publicize;
  280. }
  281. }