PageRenderTime 41ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/endomorphosis/falkenstein
PHP | 407 lines | 262 code | 61 blank | 84 comment | 76 complexity | 5ad09e55b2cc59ed2adaefa1a69e5a98 MD5 | raw file
  1. <?php
  2. abstract class Publicize_Base {
  3. /**
  4. * Services that are currently connected to the given user
  5. * through publicize.
  6. */
  7. var $connected_services = array();
  8. /**
  9. * Sservices that are supported by publicize. They don't
  10. * neccessarly need to be connected to the current user.
  11. */
  12. var $services;
  13. /**
  14. * key names for post meta
  15. */
  16. var $ADMIN_PAGE = 'wpas';
  17. var $POST_MESS = '_wpas_mess';
  18. var $POST_SKIP = '_wpas_skip_'; // connection id appended to indicate that a connection should NOT be publicized to
  19. var $POST_DONE = '_wpas_done_'; // connection id appended to indicate a connection has already been publicized to
  20. var $USER_AUTH = 'wpas_authorize';
  21. var $USER_OPT = 'wpas_';
  22. var $PENDING = '_publicize_pending'; // ready for Publicize to do its thing
  23. var $POST_SERVICE_DONE = '_publicize_done_external'; // array of external ids where we've Publicized
  24. /**
  25. * default pieces of the message used in constructing the
  26. * content pushed out to other social networks
  27. */
  28. var $default_prefix = '';
  29. var $default_message = '%title%';
  30. var $default_suffix = ' %url%';
  31. /**
  32. * What WP capability is require to create/delete global connections?
  33. * All users with this cap can unglobalize all other global connections, and globalize any of their own
  34. * Globalized connections cannot be unselected by users without this capability when publishing
  35. */
  36. var $GLOBAL_CAP = 'edit_others_posts';
  37. /**
  38. * Sets up the basics of Publicize
  39. */
  40. function __construct() {
  41. $this->default_message = Publicize_Util::build_sprintf( array(
  42. apply_filters( 'wpas_default_message', $this->default_message ),
  43. 'title',
  44. 'url',
  45. ) );
  46. $this->default_prefix = Publicize_Util::build_sprintf( array(
  47. apply_filters( 'wpas_default_prefix', $this->default_prefix ),
  48. 'url',
  49. ) );
  50. $this->default_suffix = Publicize_Util::build_sprintf( array(
  51. apply_filters( 'wpas_default_suffix', $this->default_suffix ),
  52. 'url',
  53. ) );
  54. $this->GLOBAL_CAP = apply_filters( 'jetpack_publicize_global_connections_cap', $this->GLOBAL_CAP );
  55. // stage 1 and 2 of 3-stage Publicize. Flag for Publicize on creation, save meta,
  56. // then check meta and publicze based on that. stage 3 implemented on wpcom
  57. add_action( 'transition_post_status', array( $this, 'flag_post_for_publicize' ), 10, 3 );
  58. add_action( 'save_post', array( &$this, 'save_meta' ), 20, 2 );
  59. // Connection test callback
  60. add_action( 'wp_ajax_test_publicize_conns', array( $this, 'test_publicize_conns' ) );
  61. }
  62. /**
  63. * Functions to be implemented by the extended class (publicize-wpcom or publicize-jetpack)
  64. */
  65. abstract function get_connection_id( $connection );
  66. abstract function connect_url( $service_name );
  67. abstract function disconnect_url( $service_name, $id );
  68. abstract function get_connection_meta( $connection );
  69. abstract function get_services( $filter );
  70. abstract function get_connections( $service, $_blog_id = false, $_user_id = false );
  71. abstract function get_connection( $service, $id, $_blog_id = false, $_user_id = false );
  72. abstract function flag_post_for_publicize( $new_status, $old_status, $post );
  73. abstract function test_connection( $service_name, $connection );
  74. /**
  75. * Shared Functions
  76. */
  77. /**
  78. * Returns an external URL to the connection's profile
  79. */
  80. function get_profile_link( $service_name, $c ) {
  81. $cmeta = $this->get_connection_meta( $c );
  82. if ( isset( $cmeta['connection_data']['meta']['link'] ) ) {
  83. return $cmeta['connection_data']['meta']['link'];
  84. } elseif ( 'facebook' == $service_name && isset( $cmeta['connection_data']['meta']['facebook_page'] ) ) {
  85. return 'http://facebook.com/' . $cmeta['connection_data']['meta']['facebook_page'];
  86. } elseif ( 'facebook' == $service_name ) {
  87. return 'http://www.facebook.com/' . $cmeta['external_id'];
  88. } elseif ( 'tumblr' == $service_name && isset( $cmeta['connection_data']['meta']['tumblr_base_hostname'] ) ) {
  89. return 'http://' . $cmeta['connection_data']['meta']['tumblr_base_hostname'];
  90. } elseif ( 'twitter' == $service_name ) {
  91. return 'http://twitter.com/' . substr( $cmeta['external_display'], 1 ); // Has a leading '@'
  92. } elseif ( 'google_plus' == $service_name && isset( $cmeta['connection_data']['meta']['google_plus_page'] ) ) {
  93. return 'https://plus.google.com/' . $cmeta['connection_data']['meta']['google_plus_page'];
  94. } elseif ( 'google_plus' == $service_name ) {
  95. return 'https://plus.google.com/' . $cmeta['external_id'];
  96. } else if ( 'linkedin' == $service_name ) {
  97. if ( !isset( $cmeta['connection_data']['meta']['profile_url'] ) ) {
  98. return false;
  99. }
  100. $profile_url_query = parse_url( $cmeta['connection_data']['meta']['profile_url'], PHP_URL_QUERY );
  101. wp_parse_str( $profile_url_query, $profile_url_query_args );
  102. if ( isset( $profile_url_query_args['key'] ) ) {
  103. $id = $profile_url_query_args['key'];
  104. } elseif ( isset( $profile_url_query_args['id'] ) ) {
  105. $id = $profile_url_query_args['id'];
  106. } else {
  107. return false;
  108. }
  109. return esc_url_raw( add_query_arg( 'id', urlencode( $id ), 'http://www.linkedin.com/profile/view' ) );
  110. } else {
  111. return false; // no fallback. we just won't link it
  112. }
  113. }
  114. /**
  115. * Returns a display name for the connection
  116. */
  117. function get_display_name( $service_name, $c ) {
  118. $cmeta = $this->get_connection_meta( $c );
  119. if ( isset( $cmeta['connection_data']['meta']['display_name'] ) ) {
  120. return $cmeta['connection_data']['meta']['display_name'];
  121. } elseif ( $service_name == 'tumblr' && isset( $cmeta['connection_data']['meta']['tumblr_base_hostname'] ) ) {
  122. return $cmeta['connection_data']['meta']['tumblr_base_hostname'];
  123. } elseif ( $service_name == 'twitter' ) {
  124. return $cmeta['external_display'];
  125. } else {
  126. $connection_display = $cmeta['external_display'];
  127. if ( empty( $connection_display ) )
  128. $connection_display = $cmeta['external_name'];
  129. return $connection_display;
  130. }
  131. }
  132. public static function get_service_label( $service_name ) {
  133. switch ( $service_name ) {
  134. case 'linkedin':
  135. return 'LinkedIn';
  136. break;
  137. case 'google_plus':
  138. return 'Google+';
  139. break;
  140. case 'twitter':
  141. case 'facebook':
  142. case 'tumblr':
  143. default:
  144. return ucfirst( $service_name );
  145. break;
  146. }
  147. }
  148. function show_options_popup( $service_name, $c ) {
  149. $cmeta = $this->get_connection_meta( $c );
  150. // always show if no selection has been made for facebook
  151. if ( 'facebook' == $service_name && empty( $cmeta['connection_data']['meta']['facebook_profile'] ) && empty( $cmeta['connection_data']['meta']['facebook_page'] ) )
  152. return true;
  153. // always show if no selection has been made for tumblr
  154. if ( 'tumblr' == $service_name && empty ( $cmeta['connection_data']['meta']['tumblr_base_hostname'] ) )
  155. return true;
  156. // if we have the specific conncetion info..
  157. if ( isset( $_GET['id'] ) ) {
  158. if ( $cmeta['connection_data']['id'] == $_GET['id'] )
  159. return true;
  160. } else {
  161. // otherwise, just show if this is the completed step / first load
  162. if ( !empty( $_GET['action'] ) && 'completed' == $_GET['action'] && !empty( $_GET['service'] ) && $service_name == $_GET['service'] && ! in_array( $_GET['service'], array( 'facebook', 'tumblr' ) ) )
  163. return true;
  164. }
  165. return false;
  166. }
  167. function user_id() {
  168. global $current_user;
  169. return $current_user->ID;
  170. }
  171. function blog_id() {
  172. return get_current_blog_id();
  173. }
  174. /**
  175. * Returns true if a user has a connection to a particular service, false otherwise
  176. */
  177. function is_enabled( $service, $_blog_id = false, $_user_id = false ) {
  178. if ( !$_blog_id )
  179. $_blog_id = $this->blog_id();
  180. if ( !$_user_id )
  181. $_user_id = $this->user_id();
  182. $connections = $this->get_connections( $service, $_blog_id, $_user_id );
  183. return ( is_array( $connections ) && count( $connections ) > 0 ? true : false );
  184. }
  185. /**
  186. * Fires when a post is saved, checks conditions and saves state in postmeta so that it
  187. * can be picked up later by @see ::publicize_post()
  188. */
  189. function save_meta( $post_id, $post ) {
  190. $cron_user = null;
  191. $submit_post = true;
  192. if ( ! $this->post_type_is_publicizeable( $post->post_type ) )
  193. return;
  194. // Don't Publicize during certain contexts:
  195. // - import
  196. if ( defined( 'WP_IMPORTING' ) && WP_IMPORTING )
  197. $submit_post = false;
  198. // - on quick edit, autosave, etc but do fire on p2, quickpress, and instapost ajax
  199. if (
  200. defined( 'DOING_AJAX' )
  201. &&
  202. DOING_AJAX
  203. &&
  204. !did_action( 'p2_ajax' )
  205. &&
  206. !did_action( 'wp_ajax_json_quickpress_post' )
  207. &&
  208. !did_action( 'wp_ajax_instapost_publish' )
  209. &&
  210. !did_action( 'wp_ajax_post_reblog' )
  211. ) {
  212. $submit_post = false;
  213. }
  214. // - bulk edit
  215. if ( isset( $_GET['bulk_edit'] ) )
  216. $submit_post = false;
  217. // - API/XML-RPC Test Posts
  218. if (
  219. (
  220. defined( 'XMLRPC_REQUEST' )
  221. &&
  222. XMLRPC_REQUEST
  223. ||
  224. defined( 'APP_REQUEST' )
  225. &&
  226. APP_REQUEST
  227. )
  228. &&
  229. 0 === strpos( $post->post_title, 'Temporary Post Used For Theme Detection' )
  230. ) {
  231. $submit_post = false;
  232. }
  233. // only work with certain statuses (avoids inherits, auto drafts etc)
  234. if ( !in_array( $post->post_status, array( 'publish', 'draft', 'future' ) ) )
  235. $submit_post = false;
  236. // don't publish password protected posts
  237. if ( '' !== $post->post_password )
  238. $submit_post = false;
  239. // Did this request happen via wp-admin?
  240. $from_web = 'post' == strtolower( $_SERVER['REQUEST_METHOD'] ) && isset( $_POST[$this->ADMIN_PAGE] );
  241. if ( ( $from_web || defined( 'POST_BY_EMAIL' ) ) && !empty( $_POST['wpas_title'] ) )
  242. update_post_meta( $post_id, $this->POST_MESS, trim( stripslashes( $_POST['wpas_title'] ) ) );
  243. // change current user to provide context for get_services() if we're running during cron
  244. if ( defined( 'DOING_CRON' ) && DOING_CRON ) {
  245. $cron_user = (int) $GLOBALS['user_ID'];
  246. wp_set_current_user( $post->post_author );
  247. }
  248. /**
  249. * In this phase, we mark connections that we want to SKIP. When Publicize is actually triggered,
  250. * it will Publicize to everything *except* those marked for skipping.
  251. */
  252. foreach ( (array) $this->get_services( 'connected' ) as $service_name => $connections ) {
  253. foreach ( $connections as $connection ) {
  254. $connection_data = '';
  255. if ( method_exists( $connection, 'get_meta' ) )
  256. $connection_data = $connection->get_meta( 'connection_data' );
  257. elseif ( ! empty( $connection['connection_data'] ) )
  258. $connection_data = $connection['connection_data'];
  259. if ( false == apply_filters( 'wpas_submit_post?', $submit_post, $post_id, $service_name, $connection_data ) ) {
  260. delete_post_meta( $post_id, $this->PENDING );
  261. continue;
  262. }
  263. if ( !empty( $connection->unique_id ) )
  264. $unique_id = $connection->unique_id;
  265. else if ( !empty( $connection['connection_data']['token_id'] ) )
  266. $unique_id = $connection['connection_data']['token_id'];
  267. // This was a wp-admin request, so we need to check the state of checkboxes
  268. if ( $from_web ) {
  269. // We *unchecked* this stream from the admin page, or it's set to readonly, or it's a new addition
  270. if ( empty( $_POST[$this->ADMIN_PAGE]['submit'][$unique_id] ) ) {
  271. // Also make sure that the service-specific input isn't there.
  272. // If the user connected to a new service 'in-page' then a hidden field with the service
  273. // name is added, so we just assume they wanted to Publicize to that service.
  274. if ( empty( $_POST[$this->ADMIN_PAGE]['submit'][$service_name] ) ) {
  275. // Nothing seems to be checked, so we're going to mark this one to be skipped
  276. update_post_meta( $post_id, $this->POST_SKIP . $unique_id, 1 );
  277. continue;
  278. }
  279. } else {
  280. // The checkbox for this connection is explicitly checked -- make sure we DON'T skip it
  281. delete_post_meta( $post_id, $this->POST_SKIP . $unique_id );
  282. }
  283. }
  284. // Users may hook in here and do anything else they need to after meta is written,
  285. // and before the post is processed for Publicize.
  286. do_action( 'publicize_save_meta', $submit_post, $post_id, $service_name, $connection );
  287. }
  288. }
  289. if ( defined( 'DOING_CRON' ) && DOING_CRON ) {
  290. wp_set_current_user( $cron_user );
  291. }
  292. // Next up will be ::publicize_post()
  293. }
  294. /**
  295. * Is a given post type Publicize-able?
  296. *
  297. * Not every CPT lends itself to Publicize-ation. Allow CPTs to register by adding their CPT via
  298. * the publicize_post_types array filter.
  299. *
  300. * @param string $post_type The post type to check.
  301. * $return bool True if the post type can be Publicized.
  302. */
  303. function post_type_is_publicizeable( $post_type ) {
  304. if ( 'post' == $post_type )
  305. return true;
  306. return post_type_supports( $post_type, 'publicize' );
  307. }
  308. /**
  309. * Runs tests on all the connections and returns the results to the caller
  310. */
  311. function test_publicize_conns() {
  312. $test_results = array();
  313. foreach ( (array) $this->get_services( 'connected' ) as $service_name => $connections ) {
  314. foreach ( $connections as $connection ) {
  315. $id = $this->get_connection_id( $connection );
  316. $connection_test_passed = true;
  317. $connection_test_message = __( 'This connection is working correctly.' , 'jetpack' );
  318. $user_can_refresh = false;
  319. $refresh_text = '';
  320. $refresh_url = '';
  321. $connection_test_result = true;
  322. if ( method_exists( $this, 'test_connection' ) ) {
  323. $connection_test_result = $this->test_connection( $service_name, $connection );
  324. }
  325. if ( is_wp_error( $connection_test_result ) ) {
  326. $connection_test_passed = false;
  327. $connection_test_message = $connection_test_result->get_error_message();
  328. $error_data = $connection_test_result->get_error_data();
  329. $user_can_refresh = $error_data['user_can_refresh'];
  330. $refresh_text = $error_data['refresh_text'];
  331. $refresh_url = $error_data['refresh_url'];
  332. }
  333. $test_results[] = array(
  334. 'connectionID' => $id,
  335. 'serviceName' => $service_name,
  336. 'connectionTestPassed' => $connection_test_passed,
  337. 'connectionTestMessage' => esc_attr( $connection_test_message ),
  338. 'userCanRefresh' => $user_can_refresh,
  339. 'refreshText' => esc_attr( $refresh_text ),
  340. 'refreshURL' => $refresh_url
  341. );
  342. }
  343. }
  344. wp_send_json_success( $test_results );
  345. }
  346. }