/wp-content/plugins/projects-by-woothemes/classes/class-projects-integrations.php

https://gitlab.com/mostafame/team_website · PHP · 341 lines · 216 code · 70 blank · 55 comment · 27 complexity · 737127d0e00238f4614c9f91b7119c5b MD5 · raw file

  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly.
  3. /**
  4. * WooThemes Projects Integrations Class
  5. *
  6. * All functionality pertaining to the integration between projects and other plugins.
  7. *
  8. * @package WordPress
  9. * @subpackage Projects
  10. * @category Plugin
  11. * @since 1.2.0
  12. */
  13. class Projects_Integrations {
  14. private $token;
  15. private $post_type;
  16. /**
  17. * Constructor function.
  18. *
  19. * @access public
  20. * @since 1.2.0
  21. * @return void
  22. */
  23. public function __construct() {
  24. $this->token = 'projects';
  25. $this->post_type = 'project';
  26. // Testimonials Integration
  27. add_action( 'init', array( $this, 'projects_testimonials_init' ) );
  28. // WooCommerce Integration
  29. add_action( 'init', array( $this, 'projects_woocommerce_init' ) );
  30. } // End __construct()
  31. /**
  32. * Init function for the Testimonials plugin integration.
  33. * @since 1.1.0
  34. * @return void
  35. */
  36. public function projects_testimonials_init() {
  37. if ( class_exists( 'Woothemes_Testimonials' ) ) {
  38. // Add custom fields
  39. add_filter( 'projects_custom_fields', array( $this, 'testimonials_custom_fields' ) );
  40. // Enqueue admin JavaScript
  41. add_action( 'admin_enqueue_scripts', array( $this, 'testimonials_admin_scripts' ) );
  42. add_action( 'wp_ajax_get_testimonials', array( $this, 'get_testimonials_callback' ) );
  43. add_action( 'admin_footer', array( $this, 'testimonials_javascript' ) );
  44. }
  45. } // End projects_testimonials_init()
  46. public function testimonials_admin_scripts () {
  47. wp_enqueue_script( 'jquery-ui-autocomplete', null, array( 'jquery' ), null, false);
  48. } // End projects_testimonials_admin_scripts()
  49. /**
  50. * Ajax callback to search for testimonials.
  51. * @param string $query Search Query.
  52. * @since 1.1.0
  53. * @return json Search Results.
  54. */
  55. public function get_testimonials_callback() {
  56. check_ajax_referer( 'projects_ajax_get_testimonials', 'security' );
  57. $term = urldecode( stripslashes( strip_tags( $_GET['term'] ) ) );
  58. if ( !empty( $term ) ) {
  59. header( 'Content-Type: application/json; charset=utf-8' );
  60. $query_args = array(
  61. 'post_type' => 'testimonial',
  62. 'orderby' => 'title',
  63. 's' => $term,
  64. 'suppress_filters' => false
  65. );
  66. $testimonials = get_posts( $query_args );
  67. $found_testimonials = array();
  68. if ( $testimonials ) {
  69. foreach ( $testimonials as $testimonial ) {
  70. $found_testimonials[] = array(
  71. 'id' => $testimonial->ID,
  72. 'title' => $testimonial->post_title
  73. );
  74. }
  75. }
  76. echo json_encode( $found_testimonials );
  77. }
  78. die();
  79. } // End get_testimonials_callback()
  80. /**
  81. * Output Testimonials admin javascript
  82. * @since 1.1.0
  83. * @return void
  84. */
  85. public function testimonials_javascript() {
  86. global $pagenow, $post_type;
  87. if ( ( $pagenow == 'post.php' || $pagenow == 'post-new.php' ) && isset( $post_type ) && esc_attr( $post_type ) == $this->post_type ) {
  88. $ajax_nonce = wp_create_nonce( 'projects_ajax_get_testimonials' );
  89. ?>
  90. <script type="text/javascript" >
  91. jQuery(function() {
  92. jQuery( "#testimonials_search" ).autocomplete({
  93. minLength: 2,
  94. source: function ( request, response ) {
  95. jQuery.ajax({
  96. url: ajaxurl,
  97. dataType: 'json',
  98. data: {
  99. action: 'get_testimonials',
  100. security: '<?php echo $ajax_nonce; ?>',
  101. term: request.term
  102. },
  103. success: function( data ) {
  104. response( jQuery.map( data, function( item ) {
  105. return {
  106. label: item.title,
  107. value: item.id
  108. }
  109. }));
  110. }
  111. });
  112. },
  113. select: function ( event, ui ) {
  114. event.preventDefault();
  115. jQuery( "#testimonials_search" ).val( ui.item.label );
  116. jQuery( "#testimonials_id" ).val( ui.item.value );
  117. },
  118. change: function ( event, ui ) {
  119. event.preventDefault();
  120. if ( 0 == jQuery( "#testimonials_search" ).val().length ) {
  121. jQuery( "#testimonials_id" ).val( '' );
  122. }
  123. }
  124. });
  125. });
  126. </script>
  127. <?php
  128. }
  129. } // End testimonials_javascript()
  130. public function testimonials_custom_fields( $fields ) {
  131. $fields['testimonials_search'] = array(
  132. 'name' => __( 'Testimonial', 'projects-by-woothemes' ),
  133. 'description' => __( 'Search for Testimonial to link to this Project. (Optional)', 'projects-by-woothemes' ),
  134. 'type' => 'text',
  135. 'default' => '',
  136. 'section' => 'info',
  137. );
  138. $fields['testimonials_id'] = array(
  139. 'name' => __( 'Testimonial ID', 'projects-by-woothemes' ),
  140. 'description' => __( 'Holds the id of the selected testimonial.', 'projects-by-woothemes' ),
  141. 'type' => 'hidden',
  142. 'default' => 0,
  143. 'section' => 'info',
  144. );
  145. return $fields;
  146. } // End testimonials_custom_fields()
  147. /**
  148. * Init function for the WooCommerce plugin integration.
  149. * @since 1.2.0
  150. * @return void
  151. */
  152. public function projects_woocommerce_init() {
  153. if ( class_exists( 'WooCommerce' ) ) {
  154. // Add custom fields
  155. add_filter( 'projects_custom_fields', array( $this, 'woocommerce_custom_fields' ) );
  156. // Enqueue admin JavaScript
  157. add_action( 'admin_enqueue_scripts', array( $this, 'woocommerce_admin_scripts' ) );
  158. add_action( 'wp_ajax_get_products', array( $this, 'get_products_callback' ) );
  159. add_action( 'admin_footer', array( $this, 'woocommerce_javascript' ) );
  160. }
  161. } // End projects_woocommerce_init()
  162. public function woocommerce_admin_scripts () {
  163. wp_enqueue_script( 'jquery-ui-autocomplete', null, array( 'jquery' ), null, false );
  164. } // End projects_woocommerce_admin_scripts()
  165. /**
  166. * Ajax callback to search for products.
  167. * @param string $query Search Query.
  168. * @since 1.2.0
  169. * @return json Search Results.
  170. */
  171. public function get_products_callback() {
  172. check_ajax_referer( 'projects_ajax_get_products', 'security' );
  173. $term = urldecode( stripslashes( strip_tags( $_GET['term'] ) ) );
  174. if ( ! empty( $term ) ) {
  175. header( 'Content-Type: application/json; charset=utf-8' );
  176. $query_args = array(
  177. 'post_type' => 'product',
  178. 'orderby' => 'title',
  179. 's' => $term,
  180. 'suppress_filters' => false
  181. );
  182. $products = get_posts( $query_args );
  183. $found_products = array();
  184. if ( $products ) {
  185. foreach ( $products as $product ) {
  186. $_product = new WC_Product( $product->ID );
  187. if ( $_product->get_sku() ) {
  188. $identifier = $_product->get_sku();
  189. } else {
  190. $identifier = '#' . $_product->id;
  191. }
  192. $found_products[] = array(
  193. 'id' => $product->ID,
  194. 'title' => $product->post_title,
  195. 'identifier' => $identifier,
  196. );
  197. }
  198. }
  199. echo json_encode( $found_products );
  200. }
  201. die();
  202. } // End get_products_callback()
  203. /**
  204. * Output Products admin javascript
  205. * @since 1.2.0
  206. * @return void
  207. */
  208. public function woocommerce_javascript() {
  209. global $pagenow, $post_type;
  210. if ( ( $pagenow == 'post.php' || $pagenow == 'post-new.php' ) && isset( $post_type ) && esc_attr( $post_type ) == $this->post_type ) {
  211. $ajax_nonce = wp_create_nonce( 'projects_ajax_get_products' );
  212. ?>
  213. <script type="text/javascript" >
  214. jQuery(function() {
  215. jQuery( "#products_search" ).autocomplete({
  216. minLength: 2,
  217. source: function ( request, response ) {
  218. jQuery.ajax({
  219. url: ajaxurl,
  220. dataType: 'json',
  221. data: {
  222. action: 'get_products',
  223. security: '<?php echo $ajax_nonce; ?>',
  224. term: request.term
  225. },
  226. success: function( data ) {
  227. response( jQuery.map( data, function( item ) {
  228. return {
  229. label: item.identifier + ' – ' + item.title,
  230. value: item.id
  231. }
  232. }));
  233. }
  234. });
  235. },
  236. select: function ( event, ui ) {
  237. event.preventDefault();
  238. jQuery( "#products_search" ).val( ui.item.label );
  239. jQuery( "#products_id" ).val( ui.item.value );
  240. },
  241. change: function ( event, ui ) {
  242. event.preventDefault();
  243. if ( 0 == jQuery( "#products_search" ).val().length ) {
  244. jQuery( "#products_id" ).val( '' );
  245. }
  246. }
  247. });
  248. });
  249. </script>
  250. <?php
  251. }
  252. } // End woocommerce_javascript()
  253. public function woocommerce_custom_fields( $fields ) {
  254. $fields['products_search'] = array(
  255. 'name' => __( 'Product', 'projects-by-woothemes' ),
  256. 'description' => __( 'Search for Product to link to this Project. (Optional)', 'projects-by-woothemes' ),
  257. 'type' => 'text',
  258. 'default' => '',
  259. 'section' => 'info',
  260. );
  261. $fields['products_id'] = array(
  262. 'name' => __( 'Product ID', 'projects-by-woothemes' ),
  263. 'description' => __( 'Holds the id of the selected product.', 'projects-by-woothemes' ),
  264. 'type' => 'hidden',
  265. 'default' => 0,
  266. 'section' => 'info',
  267. );
  268. return $fields;
  269. } // End woocommerce_custom_fields()
  270. } // End Class
  271. new Projects_Integrations();