PageRenderTime 30ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/themes/jannah/framework/functions/foxpush.php

https://bitbucket.org/satheesh_krossark/news
PHP | 581 lines | 297 code | 140 blank | 144 comment | 52 complexity | d5c9e891c57a251c1f84c169d7cb552e MD5 | raw file
  1. <?php
  2. /**
  3. * FoxPush Class
  4. *
  5. * @package Jannah
  6. */
  7. if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
  8. if( ! class_exists( 'TIE_FOXPUSH' )){
  9. class TIE_FOXPUSH{
  10. /**
  11. * __construct
  12. *
  13. */
  14. function __construct(){
  15. }
  16. /**
  17. * run
  18. *
  19. * Call our filter and action hooks.
  20. */
  21. function run(){
  22. # Actions ----------
  23. add_action( 'wp_ajax_tie-foxpush-send-campaign', array( $this, '_create_campaign' ) );
  24. add_action( 'wp_ajax_tie-foxpush-show-campaign', array( $this, '_show_campaigns' ) );
  25. add_action( 'wp_footer', array( $this, '_get_embed_code' ) );
  26. add_action( 'add_meta_boxes', array( $this, '_post_meta_boxes' ) );
  27. }
  28. /**
  29. * _connect
  30. *
  31. * Connect FoxPush API
  32. */
  33. /*
  34. function _connect(){
  35. # Check the requried fields ----------
  36. if( empty( $_REQUEST['domain'] ) || empty( $_REQUEST['apiKey'] ) ){
  37. esc_html_e( 'The Domain and API Key fields are required.', 'jannah' );
  38. die;
  39. }
  40. $domain = $_REQUEST['domain'];
  41. $api_key = $_REQUEST['apiKey'];
  42. $args = array(
  43. 'headers' => array(
  44. 'FOXPUSH_DOMAIN' => $domain,
  45. 'FOXPUSH_TOKEN' => $api_key,
  46. ),
  47. 'sslverify' => false,
  48. );
  49. $api_url = 'https://api.foxpush.com/v1/publisher/code';
  50. $request = wp_remote_get( $api_url, $args );
  51. if ( is_wp_error( $request ) ) {
  52. echo esc_html( $request->get_error_message() );
  53. die;
  54. }
  55. $request = wp_remote_retrieve_body( $request );
  56. $request = json_decode( $request, true );
  57. if( ! empty( $request['user_code'] ) && ! empty( $request['subdomain'] ) ){
  58. $code = $request['subdomain'] .'_'. $request['user_code'];
  59. update_option( 'jannah_foxpush_code', $code );
  60. echo 1;
  61. }
  62. else{
  63. esc_html_e( 'ERROR', 'jannah' );
  64. if( ! empty( $request['error_message'] ) ){
  65. echo ': '. $request['error_message'];
  66. }
  67. }
  68. die;
  69. }
  70. */
  71. /**
  72. * _get_embed_code
  73. *
  74. * Get the Js Embed Code
  75. */
  76. function _get_embed_code(){
  77. # If the Web notificatiosn is enabled ----------
  78. if( ! jannah_get_option( 'web_notifications' ) || jannah_is_bot() ){
  79. return false;
  80. }
  81. # Get codes and data ----------
  82. $foxpush_domain = jannah_get_option( 'foxpush_domain' );
  83. $foxpush_apikey = jannah_get_option( 'foxpush_api' );
  84. if( empty( $foxpush_domain ) || empty( $foxpush_apikey ) ){
  85. return false;
  86. }
  87. $foxpush_domain = str_replace( '.', '', $foxpush_domain );
  88. $code = "
  89. var _foxpush = _foxpush || [];
  90. _foxpush.push(['_setDomain', '$foxpush_domain']);
  91. (function(){
  92. var foxscript = document.createElement('script');
  93. foxscript.src = '//cdn.foxpush.net/sdk/foxpush_SDK_min.js';
  94. foxscript.type = 'text/javascript';
  95. foxscript.async = 'true';
  96. var fox_s = document.getElementsByTagName('script')[0];
  97. fox_s.parentNode.insertBefore(foxscript, fox_s);})();
  98. ";
  99. jannah_add_inline_script( 'jannah-scripts', apply_filters( 'jannah_foxpush_embedcode', $code ) );
  100. }
  101. /**
  102. * _post_meta_boxes
  103. *
  104. * Post meta boxes
  105. */
  106. function _post_meta_boxes(){
  107. # Make some checks on before showing the boxes ----------
  108. if( jannah_get_option( 'web_notifications' ) && jannah_get_option( 'foxpush_domain' ) && jannah_get_option( 'foxpush_api' ) ){
  109. # Campagins States ----------
  110. if( 'publish' === get_post_status( get_the_ID() ) ){
  111. $get_campaigns = get_post_meta( get_the_ID(), 'foxpush_campaigns_data', true );
  112. if( $get_campaigns && is_array( $get_campaigns ) ){
  113. add_meta_box(
  114. 'foxpush-get-campaigns',
  115. esc_html__( 'Web Notification Campaigns', 'jannah' ),
  116. array( $this, '_get_meta_box_content' ),
  117. 'post',
  118. 'side'
  119. );
  120. }
  121. }
  122. # Send notifications meta box ----------
  123. add_meta_box(
  124. 'foxpush-create-campaign',
  125. esc_html__( 'Send a Web Notification', 'jannah' ),
  126. array( $this, '_send_meta_box_content' ),
  127. 'post',
  128. 'side'
  129. );
  130. }
  131. }
  132. /**
  133. * _get_meta_box_content
  134. *
  135. * Show the campaigns data
  136. */
  137. function _get_meta_box_content( $post_id = false, $meta_box_atts = false ){
  138. $post_id = ! empty( $post_id->ID ) ? $post_id->ID : $post_id;
  139. # Show the Campaigns ----------
  140. $get_campaigns = get_post_meta( $post_id, 'foxpush_campaigns_data', true );
  141. if( $get_campaigns && is_array( $get_campaigns ) ){
  142. if( $meta_box_atts ){
  143. echo '<div class="campaigns-statistics">'. esc_html__( 'Statistics (Updated Hourly)', 'jannah' ) .' <a href="#" id="update-campaign-status" class="button">'. esc_html__( 'Update', 'jannah' ) .'</a></div>';
  144. }
  145. echo '<div id="campaigns-statistics-tables">';
  146. foreach ( $get_campaigns as $campaign_id => $campaign_data ) {
  147. if( empty( $campaign_data['timeout'] ) || ! $meta_box_atts || ( ! empty( $campaign_data['timeout'] ) && ( time() - $campaign_data['timeout'] ) > HOUR_IN_SECONDS ) ){
  148. $campaign_data = $this->_get_campaign_data( $campaign_id );
  149. if( ! empty( $campaign_data ) ){
  150. # If it waiting or Pending we need to recall the API to get the data ----------
  151. if( $campaign_data['status'] == 'done' ){
  152. $campaign_data['timeout'] = time();
  153. }
  154. $get_campaigns[ $campaign_id ] = $campaign_data;
  155. }
  156. }
  157. echo '
  158. <br />
  159. <table class="wp-list-table widefat striped">
  160. <thead>
  161. <tr>
  162. <th colspan="3"><strong>#'. $campaign_id .'</strong> <span class="campaign-status status-'.$campaign_data['status'].'">'. $campaign_data['status'] .'</span></th>
  163. </tr>
  164. </thead>
  165. <tbody>
  166. <tr>
  167. <td colspan="3">'. esc_html__( 'Date', 'jannah' ) .': '. $campaign_data['created_time'] .'</td>
  168. </tr>
  169. <tr>
  170. <td>'. esc_html__( 'Sent', 'jannah' ) .'</td>
  171. <td>'. esc_html__( 'Views', 'jannah' ) .'</td>
  172. <td>'. esc_html__( 'Clicks', 'jannah' ) .'</td>
  173. </tr>
  174. <tr>
  175. <td>'. $campaign_data['sent'] .'</td>
  176. <td>'. $campaign_data['views'] .'</td>
  177. <td>'. $campaign_data['clicks'] .'</td>
  178. </tr>
  179. </tbody>
  180. </table>
  181. ';
  182. }
  183. echo '</div>';
  184. echo '<span class="foxpush-spinner spinner"></span>';
  185. update_post_meta( $post_id, 'foxpush_campaigns_data', $get_campaigns );
  186. if( ! $meta_box_atts ) die;
  187. }
  188. }
  189. /**
  190. * _send_meta_box_content
  191. *
  192. * Send a Push notification directly from the post page
  193. */
  194. function _send_meta_box_content(){
  195. if( ! get_option( 'tie_token_'.JANNAH_THEME_ENVATO_ID ) ){
  196. jannah_theme_option(
  197. array(
  198. 'text' => esc_html__( 'You need to validated your license to use this feature.', 'jannah' ),
  199. 'type' => 'error',
  200. ));
  201. return;
  202. }
  203. /*
  204. elseif( ! jannah_get_option( 'foxpush_domain' ) || ! jannah_get_option( 'foxpush_api' ) ){
  205. jannah_theme_option(
  206. array(
  207. 'text' => sprintf( esc_html__( 'You need to configure your %1sFoxPush account%2s first', 'jannah' ), '<a target="_blank" href="'. admin_url( 'admin.php?page=tie-theme-options#tie-options-tab-web-notifications-target' ) .'">', '</a>' ),
  208. 'type' => 'error',
  209. ));
  210. return;
  211. }
  212. */
  213. elseif( 'publish' !== get_post_status( get_the_ID() ) ){
  214. jannah_theme_option(
  215. array(
  216. 'text' => esc_html__( 'You need to publish the post first.', 'jannah' ),
  217. 'type' => 'message',
  218. ));
  219. return;
  220. }
  221. echo '<div id="send-notification-options">';
  222. jannah_custom_post_option(
  223. array(
  224. 'placeholder' => esc_html__( 'Title', 'jannah' ),
  225. 'default' => jannah_get_title( 49, 'chars' ),
  226. 'id' => 'tie_foxpush_title',
  227. 'type' => 'text',
  228. ));
  229. jannah_custom_post_option(
  230. array(
  231. 'placeholder' => esc_html__( 'Message', 'jannah' ),
  232. 'id' => 'tie_foxpush_msg',
  233. 'type' => 'text',
  234. ));
  235. jannah_custom_post_option(
  236. array(
  237. 'custom_text' => esc_html__( 'Upload Icon', 'jannah' ),
  238. 'hint' => sprintf( esc_html__( 'Recommended size is %1spx x %2spx', 'jannah' ), 250, 250 ),
  239. 'id' => 'tie_foxpush_icon',
  240. 'type' => 'upload',
  241. ));
  242. echo '
  243. <input type="hidden" id="foxpush_post_id" name="foxpush_post_id" value="'. get_the_ID() .'" />
  244. <div class="clear"></div>
  245. <div id="send-notification-actions">
  246. <a id="send-notification" class="button button-primary button-large">'. esc_html__( 'Send', 'jannah' ) .'</a>
  247. </div>
  248. ';
  249. echo '</div>';
  250. echo '<span class="foxpush-spinner spinner"></span>';
  251. }
  252. /**
  253. * get_statistics
  254. *
  255. * FoxPush Statistics
  256. */
  257. function get_statistics( $type = 'chart' ){
  258. $foxpush_domain = jannah_get_option( 'foxpush_domain' );
  259. $foxpush_apikey = jannah_get_option( 'foxpush_api' );
  260. if( empty( $foxpush_domain ) || empty( $foxpush_apikey ) ){
  261. return false;
  262. }
  263. # Get stored data ----------
  264. if( $type == 'stats' ){
  265. $data = get_transient( 'jannah_foxpush_stats' );
  266. $api_path = 'stats';
  267. }
  268. else{
  269. $data = get_transient( 'jannah_foxpush_chart' );
  270. $api_path = 'daily_chart';
  271. }
  272. # Get new data ----------
  273. if( empty( $data )){
  274. $args = array(
  275. 'headers' => array(
  276. 'FOXPUSH_DOMAIN' => jannah_remove_spaces( $foxpush_domain ),
  277. 'FOXPUSH_TOKEN' => jannah_remove_spaces( $foxpush_apikey ),
  278. )
  279. );
  280. add_filter( 'https_ssl_verify', '__return_false' );
  281. $api_url = 'https://api.foxpush.com/v1/publisher/'.$api_path;
  282. $request = wp_remote_get( $api_url , $args );
  283. $request = wp_remote_retrieve_body( $request );
  284. $request = json_decode( $request, true );
  285. # Store the new data ----------
  286. if( $type == 'stats' ){
  287. if( ! empty( $request['total_subscribers'] )){
  288. $data = $request;
  289. set_transient( 'jannah_foxpush_stats', $data, HOUR_IN_SECONDS );
  290. }
  291. }
  292. else{
  293. if( ! empty( $request['chart'] )){
  294. $data = $request['chart'];
  295. set_transient( 'jannah_foxpush_chart', $data, HOUR_IN_SECONDS );
  296. }
  297. }
  298. }
  299. return ! empty( $data ) ? $data : '';
  300. }
  301. /**
  302. * create_campaign
  303. *
  304. * Send a Post Campaign
  305. */
  306. function _create_campaign(){
  307. $domain = jannah_get_option( 'foxpush_domain' );
  308. $api_key = jannah_get_option( 'foxpush_api' );
  309. # check ----------
  310. if( ! $domain || ! $api_key || empty( $_REQUEST['title'] ) || empty( $_REQUEST['message'] ) || empty( $_REQUEST['id'] ) ){
  311. jannah_theme_option(
  312. array(
  313. 'text' => esc_html__( 'Requried data Missing', 'jannah' ),
  314. 'type' => 'error',
  315. ));
  316. die;
  317. }
  318. # Prepare the request data ---------
  319. $args = array(
  320. 'headers' => array(
  321. 'FOXPUSH_DOMAIN' => $domain,
  322. 'FOXPUSH_TOKEN' => $api_key,
  323. ),
  324. 'body' => array(
  325. 'name' => esc_html( $_REQUEST['title'] ),
  326. 'title' => esc_html( $_REQUEST['title'] ),
  327. 'message' => esc_html( $_REQUEST['message'] ),
  328. 'url' => get_permalink( $_REQUEST['id'] ),
  329. ),
  330. );
  331. # Attach the image if it exists ----------
  332. if( ! empty( $_REQUEST['image'] ) ){
  333. $args['body']['icon'] = $_REQUEST['image'];
  334. $args['body']['check_image'] = 1;
  335. }
  336. # Go ----------
  337. $api_url = 'https://api.foxpush.com/v1/campaigns/create/';
  338. $request = wp_remote_post( $api_url, $args );
  339. # Check if there is an error ----------
  340. if ( is_wp_error( $request ) ) {
  341. jannah_theme_option(
  342. array(
  343. 'text' => esc_html( $request->get_error_message() ),
  344. 'type' => 'error',
  345. ));
  346. die;
  347. }
  348. # No? then get the body response ----------
  349. $request = wp_remote_retrieve_body( $request );
  350. $request = json_decode( $request, true );
  351. if( ! empty( $request['code'] ) ){
  352. if( $request['code'] == '411' && $request['error_message'] == 'hotlink_image' ){
  353. jannah_theme_option(
  354. array(
  355. 'text' => esc_html__( 'Can not access the image from your server, send the Campaign from your account on FoxPush.com', 'jannah' ),
  356. 'type' => 'error',
  357. ));
  358. die;
  359. }
  360. elseif( $request['code'] == '200' && ! empty( $request['campaign_id'] ) ){
  361. $campaign_id = $request['campaign_id'];
  362. # Get All Stored Campaigns ----------
  363. $get_campaigns = get_post_meta( $_REQUEST['id'], 'foxpush_campaigns_data', true );
  364. $get_campaigns = ( empty( $get_campaigns ) || ! array( $get_campaigns ) ) ? array() : $get_campaigns;
  365. # Store the data of the new Campaign ----------
  366. $get_campaigns[ $campaign_id ] = '';
  367. # Update the Stored Campaigns ----------
  368. $update_campaigns = update_post_meta( $_REQUEST['id'], 'foxpush_campaigns_data', $get_campaigns );
  369. jannah_theme_option(
  370. array(
  371. 'text' => esc_html__( 'Campaign has been sent', 'jannah' ),
  372. 'type' => 'success',
  373. ));
  374. die;
  375. }
  376. }
  377. }
  378. /**
  379. * _show_campaigns
  380. *
  381. * Show campaigns via AJAX
  382. */
  383. function _show_campaigns(){
  384. if( empty( $_REQUEST['id'] ) ){
  385. return false;
  386. }
  387. $this->_get_meta_box_content( $_REQUEST['id'], false );
  388. }
  389. /**
  390. * _get_campaign_data
  391. *
  392. * Get the campaigns data from the API
  393. */
  394. function _get_campaign_data( $campaign_id ){
  395. # Request the campaign data ----------
  396. $args = array(
  397. 'headers' => array(
  398. 'FOXPUSH_DOMAIN' => jannah_get_option( 'foxpush_domain' ),
  399. 'FOXPUSH_TOKEN' => jannah_get_option( 'foxpush_api' ),
  400. )
  401. );
  402. # Go ----------
  403. $api_url = 'https://api.foxpush.com/v1/campaigns/get/'. $campaign_id;
  404. $request = wp_remote_get( $api_url, $args );
  405. # Check if there is an error ----------
  406. if ( is_wp_error( $request ) ) {
  407. echo esc_html( $request->get_error_message() );
  408. die;
  409. }
  410. $request = wp_remote_retrieve_body( $request );
  411. $request = json_decode( $request, true );
  412. if( ! empty( $request['campaign'] ) ){
  413. $campaign = $request['campaign'];
  414. # Remove the unwanted data ----------
  415. unset( $campaign['message'] );
  416. unset( $campaign['name'] );
  417. unset( $campaign['image'] );
  418. unset( $campaign['url'] );
  419. return $campaign;
  420. }
  421. }
  422. }
  423. # Instantiate the class ----------
  424. $foxpush = new TIE_FOXPUSH();
  425. $foxpush->run();
  426. }