示例#1
0
 public function testNotification()
 {
     if (isset($_POST['cred_form_id']) && isset($_POST['cred_test_notification_data'])) {
         $notification = $_POST['cred_test_notification_data'];
         $form_id = intval($_POST['cred_form_id']);
         CRED_Loader::load('CLASS/Notification_Manager');
         $results = CRED_Notification_Manager::sendTestNotification($form_id, $notification);
         echo json_encode($results);
         die;
     }
     echo json_encode(array('error' => 'not allowed'));
     die;
 }
示例#2
0
 public static function init()
 {
     add_filter('wpcf_exclude_meta_boxes_on_post_type', array('StaticClass', 'my_cred_exclude'), 10, 1);
     //        add_filter('get_items_with_flag', array(__CLASS__, "item_filter"), 10, 1);
     // plugin init
     // NOTE Early Init, in order to catch up with early hooks by 3rd party plugins (eg CRED Commerce)
     add_action('init', array('CRED_CRED', '_init_'), 1);
     CRED_Loader::load('CLASS/Notification_Manager');
     CRED_Notification_Manager::init();
     /*
      if (isset($_GET['cred-edit-form'])&&!empty($_GET['cred-edit-form'])) {
      add_action('the_content', 'try_to_remove_view_shortcode', 0);
      add_action('the_content', 'try_to_add_view_shortcode', 9);
      }
     */
     // try to catch user shortcodes (defined by [...]) and solve shortcodes inside shortcodes
     // adding filter with priority before do_shortcode and other WP standard filters
     add_filter('the_content', 'cred_do_shortcode', 9);
     //https://onthegosystems.myjetbrains.com/youtrack/issue/cred-131#
     //if (!is_admin()) add_filter('cf_fields_value_save', array('CRED_CRED', 'cf_sanitize_values_on_save'));
 }
 public static function triggerNotifications($post_id, $data, $attachedData = null)
 {
     $form_id = $data['form_id'];
     $is_user_form = self::get_form_type($form_id) == CRED_USER_FORMS_CUSTOM_POST_NAME;
     if ($is_user_form) {
         $post = get_userdata($post_id)->data;
     } else {
         if (isset($data['post'])) {
             $post = $data['post'];
         } else {
             $post = get_post($post_id);
         }
     }
     if (!$post) {
         return;
     }
     $model = CRED_Loader::get($is_user_form ? 'MODEL/UserForms' : 'MODEL/Forms');
     if (empty($attachedData)) {
         $attachedData = $model->getAttachedData($post_id);
     }
     // trigger for this event, if set
     if (isset($data['event'])) {
         self::$event = $data['event'];
     } else {
         self::$event = false;
     }
     $notification = isset($data['notification']) ? $data['notification'] : false;
     if (!$attachedData && !$is_user_form || !$notification || !isset($notification->enable) || !$notification->enable || empty($notification->notifications)) {
         return;
     }
     $notificationsToSent = array();
     foreach ($notification->notifications as $ii => $notif) {
         $send_notification = false;
         if (isset($notif['event'])) {
             $conditionFields = array();
             $_conditionFields = array();
             if (isset($notif['event']['condition']) && !empty($notif['event']['condition'])) {
                 foreach ($notif['event']['condition'] as $jj => $condition) {
                     $conditionFields[] = $condition['field'];
                 }
                 $_conditionFields = $model->getPostFields($post_id, $conditionFields);
             }
             $send_notification = self::evaluate(array('form_id' => $form_id, 'post' => $post, 'notification' => $notif, 'fields' => $_conditionFields, 'snapshot' => isset($attachedData[$form_id]) ? $attachedData[$form_id]['current'] : array()));
         }
         if ($send_notification) {
             $notificationsToSent[] = $notif;
         }
     }
     //cred_log($notificationsToSent);
     // removed but it's necessary further debugging 'Notification is being sent when visit the post edit screen'
     // if (!is_admin()&&!empty($notificationsToSent))
     if (!empty($notificationsToSent)) {
         self::sendNotifications($post_id, $form_id, $notificationsToSent);
     }
 }
 /**
  * notifications for the CRED post expiration scheduled task
  */
 function cred_pe_schedule_event_notifications()
 {
     if ($this->_post_expiration_enabled) {
         global $wpdb;
         /*
          $posts_for_notifications = $wpdb->get_results( $wpdb->prepare(
          "
          SELECT m1.post_id, m1.meta_value AS expiration_time, m2.meta_value AS notifications
          FROM $wpdb->postmeta m1 INNER JOIN $wpdb->postmeta m2
          ON m1.post_id = m2.post_id AND m1.meta_key = %s AND m2.meta_key = %s
          WHERE m1.meta_value > 0
          ",
          $this->_post_expiration_time_field,
          $this->_post_expiration_notifications_field
          ) );
         */
         $posts_for_notifications = $wpdb->get_results($wpdb->prepare("\n\t\t\t\t\tSELECT m1.post_id, m1.meta_value AS expiration_time, m2.meta_value AS notifications\n\t\t\t\t\tFROM {$wpdb->postmeta} m1 INNER JOIN {$wpdb->postmeta} m2\n\t\t\t\t\t\tON m1.post_id = m2.post_id AND m1.meta_key = %s AND m2.meta_key = %s\n\t\t\t\t\tWHERE m1.meta_value IS NOT NULL\n\t\t\t\t", $this->_post_expiration_time_field, $this->_post_expiration_notifications_field));
         $now = time();
         $posts_ids_for_notifications = array();
         foreach ($posts_for_notifications as $post_meta) {
             $post_meta->notifications = $remaining_notifications = maybe_unserialize($post_meta->notifications);
             // check wicth notification is to be activated
             foreach ($post_meta->notifications as $key => $notification) {
                 $notification_time = $post_meta->expiration_time - $notification['event']['expiration_date'] * DAY_IN_SECONDS;
                 if ($notification_time <= $now) {
                     // notify
                     $posts_ids_for_notifications[] = $post_meta->post_id;
                     $form_id = isset($notification['form_id']) ? $notification['form_id'] : NULL;
                     if (isset($notification['to']['author']) && 'author' == $notification['to']['author']) {
                         $_POST['form_' . $form_id . '_referrer_post_id'] = $post_meta->post_id;
                     }
                     // get Notification Manager object
                     CRED_Loader::load('CLASS/Notification_Manager');
                     // add extra plceholder codes
                     add_filter('cred_subject_notification_codes', array(&$this, 'extraSubjectNotificationCodes'), 5, 3);
                     add_filter('cred_body_notification_codes', array(&$this, 'extraBodyNotificationCodes'), 5, 3);
                     // send notification now
                     CRED_Notification_Manager::sendNotifications($post_meta->post_id, $form_id, array($notification));
                     // remove extra plceholder codes
                     remove_filter('cred_subject_notification_codes', array(&$this, 'extraSubjectNotificationCodes'), 5, 3);
                     remove_filter('cred_body_notification_codes', array(&$this, 'extraBodyNotificationCodes'), 5, 3);
                     // remove notification from list
                     unset($remaining_notifications[$key]);
                 }
             }
             // update notifications list
             if (empty($remaining_notifications)) {
                 delete_post_meta($post_meta->post_id, $this->_post_expiration_notifications_field);
             } else {
                 sort($remaining_notifications);
                 update_post_meta($post_meta->post_id, $this->_post_expiration_notifications_field, $remaining_notifications);
             }
         }
     }
 }
示例#5
0
 private function notify($post_id, $attachedData = null)
 {
     $form =& $this->_formData;
     $_fields = $form->getFields();
     // init notification manager if needed
     if (isset($_fields['notification']->enable) && $_fields['notification']->enable && !empty($_fields['notification']->notifications)) {
         // add extra plceholder codes
         add_filter('cred_subject_notification_codes', array(&$this, 'extraSubjectNotificationCodes'), 5, 3);
         add_filter('cred_body_notification_codes', array(&$this, 'extraBodyNotificationCodes'), 5, 3);
         CRED_Loader::load('CLASS/Notification_Manager');
         if ($form->getForm()->post_type == CRED_USER_FORMS_CUSTOM_POST_NAME) {
             CRED_Notification_Manager::set_user_fields();
         }
         // add the post to notification management
         CRED_Notification_Manager::add($post_id, $form->getForm()->ID, $_fields['notification']->notifications);
         // send any notifications now if needed
         CRED_Notification_Manager::triggerNotifications($post_id, array('event' => 'form_submit', 'form_id' => $form->getForm()->ID, 'notification' => $_fields['notification']), $attachedData);
         // remove extra plceholder codes
         remove_filter('cred_subject_notification_codes', array(&$this, 'extraSubjectNotificationCodes'), 5, 3);
         remove_filter('cred_body_notification_codes', array(&$this, 'extraBodyNotificationCodes'), 5, 3);
     }
 }
示例#6
0
 public function onOrderChange($data)
 {
     // HOOKS API
     //do_action('cred_commerce_before_send_notifications', $data);
     // send notifications
     if (!isset($data['new_status']) || !in_array($data['new_status'], array('pending', 'failed', 'processing', 'completed', 'on-hold', 'cancelled', 'refunded'))) {
         return;
     }
     // not spam with useless notifications ;)
     if (isset($data['cred_meta']) && $data['cred_meta']) {
         $model = CREDC_Loader::get('MODEL/Main');
         CRED_Loader::load('CLASS/Notification_Manager');
         foreach ($data['cred_meta'] as $ii => $meta) {
             if (!isset($meta['cred_form_id'])) {
                 continue;
             }
             $form_id = $meta['cred_form_id'];
             $form_slug = '';
             $cred_form_post = get_post($form_id);
             if ($cred_form_post) {
                 $form_slug = $cred_form_post->post_name;
             }
             $post_id = $meta['cred_post_id'];
             $form = $model->getForm($form_id, true);
             if ($form->isCommerce && isset($form->fields['notification'])) {
                 $this->_data = array('order_id' => $data['order_id'], 'previous_status' => $data['previous_status'], 'new_status' => $data['new_status'], 'cred_meta' => $meta);
                 add_filter('cred_custom_notification_event', array(&$this, 'notificationOrderEvent'), 1, 4);
                 CRED_Notification_Manager::triggerNotifications($post_id, array('event' => 'order_modified', 'form_id' => $form_id, 'notification' => $form->fields['notification']));
                 remove_filter('cred_custom_notification_event', array(&$this, 'notificationOrderEvent'), 1, 4);
                 $this->_data = false;
             }
         }
     }
     // HOOKS API
     do_action('cred_commerce_after_send_notifications_form_' . $form_slug, $data);
     do_action('cred_commerce_after_send_notifications', $data);
 }