PageRenderTime 24ms CodeModel.GetById 49ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/interactions/classes/class-cp-interactions-cluster.php

https://gitlab.com/dev73/clusterpress
PHP | 345 lines | 204 code | 37 blank | 104 comment | 18 complexity | 247b4829de4e95bd5f76bb88a8e91fa2 MD5 | raw file
  1. <?php
  2. /**
  3. * Interactions Cluster.
  4. *
  5. * @package ClusterPress\interactions\classes
  6. * @subpackage interactions-cluster
  7. *
  8. * @since 1.0.0
  9. */
  10. // Exit if accessed directly
  11. defined( 'ABSPATH' ) || exit;
  12. /**
  13. * The Interactions CLuster Class.
  14. *
  15. * @since 1.0.0
  16. */
  17. class CP_Interactions_Cluster extends CP_Cluster {
  18. /**
  19. * The constructor
  20. *
  21. * @since 1.0.0
  22. *
  23. * @param array args The Interactions Cluster arguments.
  24. * {@see CP_Cluster::__construct() for a detailled list of available arguments}
  25. */
  26. public function __construct( $args = array() ) {
  27. parent::__construct( $args );
  28. }
  29. /**
  30. * Add the Interactions Cluster to ClusterPress main instance.
  31. *
  32. * @since 1.0.0
  33. */
  34. public static function start() {
  35. $cp = clusterpress();
  36. if ( empty( $cp->interactions ) ) {
  37. $cp->interactions = new self( array(
  38. 'id' => 'interactions',
  39. 'name' => __( 'Intéractions', 'clusterpress' ),
  40. 'dir' => 'interactions',
  41. ) );
  42. }
  43. return $cp->interactions;
  44. }
  45. /**
  46. * Set specific hooks for the interactions Cluster.
  47. *
  48. * @since 1.0.0
  49. */
  50. public function set_cluster_hooks() {
  51. // If the site cluster is not enabled, set the Loop global to be a property of this class.
  52. if ( ! cp_cluster_is_enabled( 'site' ) ) {
  53. add_filter( 'cp_site_get_loop_global', array( $this, 'set_likes_loop_global' ), 10, 1 );
  54. }
  55. }
  56. /**
  57. * Include the needed files.
  58. *
  59. * NB: It Only includes files for the active
  60. * features.
  61. *
  62. * @since 1.0.0
  63. *
  64. * @param array $files The list of files and class files
  65. */
  66. public function load_cluster( $files = array() ) {
  67. $files['files'] = array();
  68. $files['class_files'] = array();
  69. // Defaults to no interactions
  70. $interactions_enabled = false;
  71. if ( cp_interactions_are_mentions_enabled() ) {
  72. $interactions_enabled = true;
  73. $files['files'] = array_merge( $files['files'], array(
  74. 'mentions/functions',
  75. 'mentions/tags',
  76. 'mentions/actions',
  77. ) );
  78. $files['class_files'] = array_merge( $files['class_files'], array(
  79. 'interactions-mentions-loop',
  80. ) );
  81. }
  82. if ( cp_interactions_is_like_enabled() ) {
  83. $interactions_enabled = true;
  84. $files['files'] = array_merge( $files['files'], array(
  85. 'likes/functions',
  86. 'likes/tags',
  87. 'likes/actions',
  88. ) );
  89. $files['class_files'] = array_merge( $files['class_files'], array(
  90. 'interactions-likes-shortcode',
  91. 'interactions-liked-posts-widget',
  92. 'interactions-liked-comments-widget',
  93. ) );
  94. }
  95. /**
  96. * Make sure content loops and tags are available even if
  97. * the site's Cluster is disabled.
  98. */
  99. if ( $interactions_enabled && ! cp_cluster_is_enabled( 'site' ) ) {
  100. $files['files'] = array_merge( $files['files'], array(
  101. '../site/tags',
  102. ) );
  103. $files['class_files'] = array_merge( $files['class_files'], array(
  104. '../site/site-posts-loop',
  105. '../site/site-comments-loop',
  106. ) );
  107. }
  108. parent::load_cluster( $files );
  109. }
  110. /**
  111. * Define cache groups for the cluster
  112. *
  113. * @since 1.0.0
  114. */
  115. public function set_cluster_cache_groups() {
  116. if ( cp_interactions_are_mentions_enabled() ) {
  117. // Global groups.
  118. wp_cache_add_global_groups( array(
  119. 'cp_user_mentions',
  120. ) );
  121. }
  122. parent::set_cluster_cache_groups();
  123. }
  124. /**
  125. * Return the Interactions settings
  126. *
  127. * @since 1.0.0.
  128. *
  129. * @return array The list of settings for the Cluster.
  130. */
  131. public function get_settings() {
  132. $settings = array(
  133. 'sections' => array(
  134. 'interactions_cluster_main_settings' => array(
  135. 'title' => __( 'Réglages principaux', 'clusterpress' ),
  136. 'callback' => 'cp_core_main_settings_callback',
  137. ),
  138. 'interactions_cluster_slug_settings' => array(
  139. 'title' => __( 'Personnalisation des URLs', 'clusterpress' ),
  140. 'callback' => 'cp_core_slug_settings_callback',
  141. ),
  142. ),
  143. 'fields' => array(
  144. 'interactions_cluster_main_settings' => array(
  145. 'clusterpress_interactions_mentions_enabled' => array(
  146. 'title' => __( 'Mentions utilisateurs', 'clusterpress' ),
  147. 'callback' => 'cp_interactions_settings_mentions_feature',
  148. 'sanitize_callback' => 'intval',
  149. ),
  150. 'clusterpress_interactions_like_enabled' => array(
  151. 'title' => __( '&quot;Likes&quot; pour les contenus', 'clusterpress' ),
  152. 'callback' => 'cp_interactions_settings_likes_feature',
  153. 'sanitize_callback' => 'intval',
  154. ),
  155. ),
  156. 'interactions_cluster_slug_settings' => array(
  157. 'clusterpress_user_mentions_slug' => array(
  158. 'title' => __( 'Portion d\'URL pour la page de profil présentant les mentions de l\'utilisateur.', 'clusterpress' ),
  159. 'callback' => 'cp_interactions_settings_mentions_slug',
  160. 'sanitize_callback' => 'cp_sanitize_slug',
  161. ),
  162. 'clusterpress_user_likes_slug' => array(
  163. 'title' => __( 'Portion d\'URL pour la page de profil présentant les &quot;likes&quot; de l\'utilisateur.', 'clusterpress' ),
  164. 'callback' => 'cp_interactions_settings_likes_slug',
  165. 'sanitize_callback' => 'cp_sanitize_slug',
  166. ),
  167. ),
  168. ),
  169. );
  170. // Remove the Slugs settings if not using pretty URLs
  171. if ( ! clusterpress()->permalink_structure || ( ! cp_interactions_are_mentions_enabled() && ! cp_interactions_is_like_enabled() ) ) {
  172. unset( $settings['sections']['interactions_cluster_slug_settings'] );
  173. }
  174. // Remove the Mentions Slug setting if mentions are not enabled.
  175. if ( ! cp_interactions_are_mentions_enabled() ) {
  176. unset( $settings['fields']['interactions_cluster_slug_settings']['clusterpress_user_mentions_slug'] );
  177. }
  178. // Remove the Likes Slug setting if likes are not enabled.
  179. if ( ! cp_interactions_is_like_enabled() ) {
  180. unset( $settings['fields']['interactions_cluster_slug_settings']['clusterpress_user_likes_slug'] );
  181. }
  182. return $settings;
  183. }
  184. /**
  185. * Add Features to the "Post" post type.
  186. *
  187. * Using the WordPress Post Type feature is pretty interesting as it gives
  188. * the opportunity to add likes or mentions to any post types.
  189. *
  190. * @since 1.0.0
  191. *
  192. * @param array $post_types The list of Post Type names.
  193. * @param array $features The list of features to add to the list of Post types.
  194. */
  195. public function add_post_types_support( $post_types = array(), $features = array() ) {
  196. $post_types = array( 'post' );
  197. if ( cp_interactions_are_mentions_enabled() ) {
  198. $features = array_merge( $features, array( 'clusterpress_post_mentions', 'clusterpress_comment_mentions' ) );
  199. }
  200. if ( cp_interactions_is_like_enabled() ) {
  201. $features = array_merge( $features, array( 'clusterpress_post_likes', 'clusterpress_comment_likes' ) );
  202. }
  203. if ( ! empty( $features ) ) {
  204. parent::add_post_types_support( $post_types, $features );
  205. }
  206. }
  207. /**
  208. * Define Rest API routes for the Likes feature.
  209. *
  210. * @since 1.0.0
  211. *
  212. * @return array The list of Rest API Routes.
  213. */
  214. public function get_routes() {
  215. if ( ! cp_interactions_is_like_enabled() ) {
  216. return array();
  217. }
  218. $likes_slug = cp_user_get_likes_slug();
  219. $user_endpoint = sprintf( '%1$s/(?P<id>\d+)/%2$s', cp_user_get_slug(), $likes_slug );
  220. $user_delete_endpoint = $user_endpoint . '/(?P<like_id>\d+)' ;
  221. $args = array(
  222. 'id' => array(
  223. 'validate_callback' => function( $param, $request, $key ) {
  224. return is_numeric( $param );
  225. }
  226. ),
  227. );
  228. return array(
  229. array(
  230. 'endpoint' => $user_endpoint,
  231. 'methods' => 'GET',
  232. 'callback' => 'cp_interactions_get_user_likes',
  233. 'permission_callback' => 'cp_interactions_user_likes_can_get',
  234. 'args' => $args,
  235. ),
  236. array(
  237. 'endpoint' => $user_endpoint,
  238. 'methods' => 'POST',
  239. 'callback' => 'cp_interactions_post_user_like',
  240. 'permission_callback' => 'cp_interactions_user_likes_can_post',
  241. 'args' => $args,
  242. ),
  243. array(
  244. 'endpoint' => $user_delete_endpoint,
  245. 'methods' => 'DELETE',
  246. 'callback' => 'cp_interactions_delete_user_like',
  247. 'permission_callback' => 'cp_interactions_user_likes_can_delete',
  248. 'args' => $args,
  249. ),
  250. array(
  251. 'endpoint' => sprintf( 'post/(?P<id>\d+)/%s', $likes_slug ),
  252. 'methods' => 'GET',
  253. 'callback' => 'cp_interactions_get_post_likes',
  254. 'args' => $args,
  255. ),
  256. array(
  257. 'endpoint' => sprintf( 'comment/(?P<id>\d+)/%s', $likes_slug ),
  258. 'methods' => 'GET',
  259. 'callback' => 'cp_interactions_get_comment_likes',
  260. 'args' => $args,
  261. ),
  262. );
  263. }
  264. /**
  265. * Return the specific feedbacks for the interactions Cluster.
  266. *
  267. * @since 1.0.0
  268. *
  269. * @return array The list of feedbacks specific to this Cluster.
  270. */
  271. public function get_feedbacks() {
  272. return array(
  273. 'mention-01' => array( 'cluster' => 'user', 'message' => __( 'La mention est introuvable.', 'clusterpress' ) ),
  274. 'mention-02' => array( 'cluster' => 'user', 'message' => __( 'La mention n\'a pas été marquée comme lue.', 'clusterpress' ) ),
  275. 'mention-03' => array( 'cluster' => 'user', 'message' => __( 'Les mentions sont introuvables.', 'clusterpress' ) ),
  276. 'mention-04' => array( 'cluster' => 'user', 'message' => __( 'Les mentions n\'ont pas été marquées comme lues.', 'clusterpress' ) ),
  277. 'mention-05' => array( 'cluster' => 'user', 'message' => __( 'Les mentions ont été marquées comme lues.', 'clusterpress' ) ),
  278. );
  279. }
  280. /**
  281. * Return the specific cap maps for the interactions Cluster.
  282. *
  283. * @since 1.0.0
  284. *
  285. * @return array The list cap maps specific to this Cluster.
  286. */
  287. public function get_caps_map() {
  288. if ( ! cp_interactions_is_like_enabled() ) {
  289. return array();
  290. }
  291. return array(
  292. 'cp_read_user_likes' => 'cp_interactions_read_user_likes',
  293. 'cp_post_user_like' => 'cp_interactions_post_user_likes',
  294. 'cp_delete_user_like' => 'cp_interactions_delete_user_likes',
  295. );
  296. }
  297. /**
  298. * Use the Interactions Cluster Class to globalize content loops (Posts & Comments).
  299. *
  300. * @since 1.0.0
  301. * @param null $global As the Sites Cluster is not available,
  302. * The Site Cluster Class is not set.
  303. * @return CP_Interactions_Cluster The current Cluster Class.
  304. */
  305. public function set_likes_loop_global( $global = null ) {
  306. return $this;
  307. }
  308. }