Your IP : 216.73.217.143


Current Path : /home/ereika83/public_html/wp-content/plugins/project-panorama/lib/pro/
Upload File :
Current File : /home/ereika83/public_html/wp-content/plugins/project-panorama/lib/pro/psp-notifications.php

<?php

    // E-mail Notifications


    // Add the option to the publish metabox
    add_action( 'post_submitbox_misc_actions', 'psp_notify_metabox' );
    function psp_notify_metabox() {
        global $post;

        if(get_post_type($post) == 'psp_projects') {
            if (get_field('allowed_users', $post->ID)) {
                ?>
                <div class="misc-pub-section misc-pub-section-last" style="border-top: 1px solid #eee;">
                    <?php wp_nonce_field(plugin_basename(__FILE__), 'article_or_box_nonce'); ?>
                    <input type="checkbox" name="psp-notify-users" id="psp-notify-users" value="yes"/> <label
                        for="psp-notify-users" class="select-it"><?php _e('Notify users of update', 'psp_projects'); ?>
                        <a href="#psp-notification-modal"
                           class="psp-notification-edit"><?php _e('Edit', 'psp_projects'); ?></a></label>
                </div>

                <div class="psp-notification-modal" id="psp-notification-modal">

                    <div class="psp-notify-warning">
                        <p><?php _e("Save this project to notify recently added users.", 'psp_projects'); ?></p>
                    </div>

                    <p>
                        <strong><?php _e("Select which users you'd like to notify upon save.", "psp_projects"); ?></strong>
                    </p>

                    <ul class="psp-notify-list">
                        <li class="all-line"><input type="checkbox" class="all-checkbox" name="psp-notify-all"
                                                    value="all"> All Users
                        </li>

                        <?php

                        // Get the project ID
                        $project_id = $post->ID;

                        while (has_sub_field('allowed_users', $project_id)) {

                            $user = get_sub_field('user');
                            $fullname = $user["user_firstname"] . ' ' . $user["user_lastname"];

                            if ($fullname == ' ') {
                                $username = $user["display_name"];
                            } else {
                                $username = $fullname;
                            }

                            echo '<li><input type="checkbox" name="psp-user[]" value="' . $user["ID"] . '" class="psp-notify-user-box"> ' . $username . '</li>';

                        }

                        ?>

                    </ul>

                    <p><label for="psp-notification-subject"><?php _e('Subject', 'psp_projects'); ?></label></p>

                    <p><input type="text" id="psp-notification-subject" name="psp-notification-subject"
                              value="<?php echo get_option('psp_default_subject'); ?>"></p>

                    <p><label for="psp-notify-message"><?php _e('Message', 'psp_projects'); ?></label></p>

                    <p><textarea name="psp-notify-message"
                                 class="psp-notify-message"><?php echo get_option('psp_default_message'); ?></textarea>
                    </p>

                    <p><?php _e('Selected users will be sent this notice next time you save this project.','psp_projects'); ?></p>

                    <p><a class="button-primary psp-notify-ok" href="#"><?php _e('OK', 'psp_projects'); ?></a></p>

                </div>

            <?php
            } else { ?>

                <div class="misc-pub-section misc-pub-section-last" style="border-top: 1px solid #eee;">
                    <?php wp_nonce_field(plugin_basename(__FILE__), 'article_or_box_nonce'); ?>
                    <input type="checkbox" name="psp-notify-users" id="psp-notify-users" value="yes" disabled /> <label for="psp-notify-users" class="select-it psp-disabled"><?php _e('Notify users of update', 'psp_projects'); ?>
                       <a href="#psp-no-users" class="psp-notification-help"><?php _e('Help','psp-projects'); ?></a></label>
                </div>
                <div class="psp-notification-modal" id="psp-notification-modal">
                        <p><strong><?php _e('Users must be assigned to this project before you can notify them.','psp-projects'); ?></strong></p>
                        <p><?php _e('Assign users to this project by through the access tab under project overview. Once you save the project you can notify users on future updates.'); ?></p>
                        <p><?php _e('Example:','psp-projects'); ?></p>
                        <p><img src="<?php echo site_url(); ?>/wp-content/plugins/project-panorama/assets/images/help/notification-help.png"></p>
                </div>


        <?php }

        }

    }

    /**
     * Looks to see if notifications are turned on and if so, notify the users
     *
     *
     * @param integer $post_id post ID
     * @param integer $post Post Object
     * @return NULL
     **/

    add_action('save_post','psp_notify_users',0);
    function psp_notify_users($post_id) {

        // Bail if we're doing an auto save
        if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;

        // if our current user can't edit this post, bail
        if( !current_user_can( 'edit_post', $post_id ) ) return;

        if(get_post_type($post_id) == 'psp_projects') {

            // Check to see if notifications were turned on
            if(isset( $_POST['psp-notify-users'])) {

                // Get an array of users
                $user_ids = $_POST['psp-user'];

                $progress = psp_compute_progress($post_id);

                $subject = stripslashes($_POST['psp-notification-subject']);
                $message = stripslashes($_POST['psp-notify-message']);

                foreach($user_ids as $user_id) {

                    psp_send_notification($user_id,$subject,$message,$post_id,$progress);

                }



            }

        }

    }

	function psp_set_mail_content_type() {

		return 'text/html';

	}

    function psp_send_notification($to,$subject,$message,$post_id,$progress = NULL) {

		// If a POST ID isn't set, let's use the current page

		if(empty($post_id)) { 
			global $post;
			$post_id = $post->ID;
		}

        // Set the senders name
        if(get_option('psp_from_name')) {
            $from = get_option('psp_from_name');
        } else {
            $from = 'Project Panorama';
        }

        // Set the senders e-email
        if(get_option('psp_from_email')) {
            $email_from = get_option('psp_from_email');
        } else {
            $email_from = get_option('admin_email');
        }

        // Are we including a logo?
        if(get_option('psp_include_logo')) {
            $logo = get_option('psp_logo');
        }

        //Set the Headers
        $headers = 'From: '.$from.' <'.$email_from.'>' . "\r\n";

        // Get the users ID and E-Mail
        $user = get_user_by('id',$to);
        $user_email = $user->user_email;

        add_filter( 'wp_mail_content_type', 'psp_set_mail_content_type' );

        ob_start();

		include(psp_template_hierarchy('/parts/email.php'));

        $message = ob_get_clean();

        wp_mail($user_email, $subject, $message, $headers);



        remove_filter( 'wp_mail_content_type', 'psp_set_mail_content_type' );

    }