/** * Create an Audit * * @access public * @param array $audit_data The attributes for an audit. * @return void */ function msa_create_audit($audit_data) { // Set the transient to say that we are running an audit. set_transient('msa_running_audit', true); delete_transient('msa_schedule_audit'); // Include all the files we need. require_once plugin_dir_path(__FILE__) . 'functions/common.php'; require_once plugin_dir_path(__FILE__) . 'model/audits.php'; require_once plugin_dir_path(__FILE__) . 'model/audit-posts.php'; require_once plugin_dir_path(__FILE__) . 'functions/audit-data.php'; require_once plugin_dir_path(__FILE__) . 'functions/condition.php'; require_once plugin_dir_path(__FILE__) . 'functions/condition-category.php'; require_once plugin_dir_path(__FILE__) . 'functions/attribute.php'; require_once plugin_dir_path(__FILE__) . 'functions/score-status.php'; require_once plugin_dir_path(__FILE__) . 'functions/create-audit.php'; require_once plugin_dir_path(__FILE__) . 'functions/notifications.php'; msa_create_initial_conditions(); msa_create_initial_condition_categories(); $audit_model = new MSA_Audits_Model(); $audit_posts_model = new MSA_Audit_Posts_Model(); // Get all the data from the user. $audit = array(); $audit['name'] = $audit_data['name']; $audit['score'] = 0; $audit['date'] = date('Y-m-d H:i:s'); $audit['status'] = 'in-progress'; $audit['user'] = $audit_data['user']; $audit['num_posts'] = 0; $audit['args']['conditions'] = wp_json_encode(msa_get_conditions()); $audit['args']['before_date'] = $audit_data['after-date']; $audit['args']['before_date'] = $audit_data['before-date']; $audit['args']['post_types'] = $audit_data['post-types']; $audit['args']['max_posts'] = $audit_data['max-posts']; $audit_data['after-date'] = '' !== $audit_data['after-date'] ? strip_tags($audit_data['after-date']) : date('m/d/Y', strtotime('-1 years')); $audit_data['before-date'] = '' !== $audit_data['before-date'] ? strip_tags($audit_data['before-date']) : date('m/d/Y', strtotime('today')); $audit['args']['form_fields'] = wp_json_encode($audit_data); // Get all the posts that we are going to perform an audit on. $page = 1; $posts_per_page = 25; $total_posts = 0; $audit_score = 0; $args = array('public' => true, 'date_query' => array(array('after' => $audit_data['after-date'], 'before' => $audit_data['before-date'], 'inclusive' => true)), 'post_type' => $audit_data['post-types'], 'posts_per_page' => $posts_per_page, 'paged' => $page); $posts = get_posts($args); // Create the audit. if (count($posts) > 0) { $audit_id = $audit_model->add_data($audit); } // Only perform the audit if there are posts to perform the audit on. while (count($posts) > 0) { if ($audit_id) { foreach ($posts as $post) { if (-1 !== $audit_data['max-posts'] && $total_posts >= $audit_data['max-posts']) { break 2; } $data = msa_get_post_audit_data($post); $score = msa_calculate_score($post, $data); $data['score'] = $score['score']; // Add a new record in the audit posts table. $audit_posts_model->add_data(array('audit_id' => $audit_id, 'post' => $post, 'data' => array('score' => $score, 'values' => $data))); $audit_score += $score['score']; $total_posts++; } } $page++; $args = array('public' => true, 'date_query' => array(array('after' => $audit_data['after-date'], 'before' => $audit_data['before-date'], 'inclusive' => true)), 'post_type' => $audit_data['post-types'], 'posts_per_page' => $posts_per_page, 'paged' => $page); $posts = get_posts($args); } $audit_score = round($audit_score / $total_posts, 10); $audit['num_posts'] = $total_posts; $audit['score'] = round($audit_score, 10); $audit['status'] = 'completed'; $audit_model->update_data($audit_id, $audit); // Remove the transient once we are done with the audit. delete_transient('msa_running_audit'); /** * Runs when the audit is completed * * @param int $audit_id The Audit ID. * @param string $audit['name'] The Audit name. */ do_action('msa_audit_completed', $audit_id, $audit['name']); }
/** * Extra controls to be displayed between bulk actions and pagination * * @access protected * * @param mixed $which Which tablenav the top or bottom. */ protected function extra_tablenav($which) { $conditions = msa_get_conditions(); $attributes = msa_get_attributes(); if ('top' === $which) { ?> <div class="alignleft actions bulkactions"><?php foreach ($conditions as $key => $condition) { if (isset($condition['filter'])) { $value = ''; if (isset($_GET[$condition['filter']['name']])) { // Input var okay. $value = sanitize_text_field(wp_unslash($_GET[$condition['filter']['name']])); // Input var okay. } $options = ''; $condition = apply_filters('msa_audit_posts_filter_' . $key, $condition); if (1 === $condition['comparison']) { $options .= '<option value="less-' . $condition['min'] . '" ' . selected('less-' . $condition['min'], $value, false) . '>' . __('Less than ', 'msa') . ' ' . $condition['min'] . ' ' . $condition['units'] . '</option>'; $options .= '<option value="more-' . $condition['min'] . '" ' . selected('more-' . $condition['min'], $value, false) . '>' . __('More than ', 'msa') . ' ' . $condition['min'] . ' ' . $condition['units'] . '</option>'; } else { if (2 === $condition['comparison']) { $options .= '<option value="less-' . $condition['max'] . '" ' . selected('less-' . $condition['max'], $value, false) . '>' . __('Less than ', 'msa') . ' ' . $condition['max'] . ' ' . $condition['units'] . '</option>'; $options .= '<option value="more-' . $condition['max'] . '" ' . selected('more-' . $condition['max'], $value, false) . '>' . __('More than ', 'msa') . ' ' . $condition['max'] . ' ' . $condition['units'] . '</option>'; } else { if (3 === $condition['comparison']) { $options .= '<option value="less-' . $condition['max'] . '" ' . selected('less-' . $condition['max'], $value, false) . '>' . __('Less than ', 'msa') . ' ' . $condition['max'] . ' ' . $condition['units'] . '</option>'; $options .= '<option value="more-' . $condition['max'] . '" ' . selected('more-' . $condition['max'], $value, false) . '>' . __('More than ', 'msa') . ' ' . $condition['max'] . ' ' . $condition['units'] . '</option>'; } } } ?> <div class="msa-filter-container msa-filter-conditions-container filter-<?php esc_attr_e($key); ?> "> <!-- <label class="msa-filter-label"><?php esc_attr_e($condition['filter']['label']); ?> </label> --> <select class="msa-filter" name="<?php esc_attr_e($condition['filter']['name']); ?> "> <option value="" <?php selected('', $value, true); ?> ><?php esc_attr_e('All ' . $condition['filter']['label'], 'msa'); ?> </option> <?php echo $options; // WPCS: XSS ok. ?> </select> </div> <?php } } foreach ($attributes as $key => $attribute) { if (isset($attribute['filter'])) { $value = ''; if (isset($_GET[$attribute['filter']['name']])) { // Input var okay. $value = sanitize_text_field(wp_unslash($_GET[$attribute['filter']['name']])); // Input var okay. } $attribute['filter']['options'] = apply_filters('msa_filter_attribute_' . $key, $attribute['filter']['options'], $key); ?> <div class="msa-filter-container msa-filter-attributes-container filter-<?php esc_attr_e($key); ?> "> <!-- <label class="msa-filter-label"><?php esc_attr_e($attribute['filter']['label']); ?> </label> --> <select class="msa-filter" name="<?php esc_attr_e($attribute['filter']['name']); ?> "> <option value="" <?php selected('', $value, true); ?> ><?php esc_attr_e('All ' . $attribute['filter']['label'], 'msa'); ?> </option> <?php foreach ($attribute['filter']['options'] as $option) { ?> <option value="<?php esc_attr_e($option['value']); ?> " <?php selected($option['value'], $value, true); ?> ><?php esc_attr_e($option['name']); ?> </option> <?php } ?> </select> </div> <?php } } ?> <button class="msa-filter-button button"><?php esc_attr_e('Filter', 'msa'); ?> </button> <button class="msa-clear-filters-button button"><?php esc_attr_e('Clear Filters', 'msa'); ?> </button> </div><?php } // Output stlying for the condition categories. $condition_categories = msa_get_condition_categories(); ?> <style> <?php foreach ($condition_categories as $key => $condition_category) { ?> th#<?php esc_attr_e($key); ?> .manage-column.column-<?php esc_attr_e($key); ?> , .<?php esc_attr_e($key); ?> .column-<?php esc_attr_e($key); ?> { font-weight: bold; border-left: 1px solid #dfdfdf; background: linear-gradient(rgba(0, 0, 0, 0.02), rgba(0, 0, 0, 0.02)); } <?php } ?> </style><?php }
<?php } else { if (isset($_GET['audit']) && check_admin_referer('msa-single-audit')) { // Input var okay. $audit_id = sanitize_text_field(wp_unslash($_GET['audit'])); // Input var okay. // Get the Audit. $audit_model = new MSA_Audits_Model(); $audit = $audit_model->get_data_from_id($audit_id); $form_fields = json_decode($audit['args']['form_fields'], true); // Get the posts for an audit. $audit_posts_model = new MSA_Audit_Posts_Model(); $posts = $audit_posts_model->get_data($audit_id); // Get all the current filters. $current_filters = ''; $conditions = msa_get_conditions(); $attributes = msa_get_attributes(); foreach ($conditions as $key => $condition) { if (isset($condition['filter']['name']) && isset($_GET[$condition['filter']['name']])) { // Input var okay. $current_filters .= '&' . $condition['filter']['name'] . '=' . sanitize_text_field(wp_unslash($_GET[$condition['filter']['name']])); // Input var okay. } } foreach ($attributes as $key => $attribute) { if (isset($attribute['filter']['name']) && isset($_GET[$attribute['filter']['name']])) { // Input var okay. $current_filters .= '&' . $attribute['filter']['name'] . '=' . sanitize_text_field(wp_unslash($_GET[$attribute['filter']['name']])); // Input var okay. } }
/** * Hooks into the 'admin_print_scripts-$page' to inlcude the scripts for the all audits page * * @access public * @static * @return void */ function msa_all_audits_scripts() { msa_include_default_styles(); // Style. wp_enqueue_style('msa-all-audits-css', MY_SITE_AUDIT_PLUGIN_URL . '/css/all-audits.css'); wp_enqueue_style('msa-fontawesome-css', MY_SITE_AUDIT_PLUGIN_URL . '/includes/font-awesome/css/font-awesome.min.css'); wp_enqueue_style('msa-jquery-ui-css', MY_SITE_AUDIT_PLUGIN_URL . '/includes/jquery-datepicker/jquery-ui.min.css'); wp_enqueue_style('msa-jquery-ui-theme-css', MY_SITE_AUDIT_PLUGIN_URL . '/includes/jquery-datepicker/jquery-ui.theme.min.css'); wp_enqueue_style('msa-jquery-daterange-picker-css', MY_SITE_AUDIT_PLUGIN_URL . '/includes/jquery-daterange-picker/jquery.comiseo.daterangepicker.css'); wp_enqueue_style('media-views'); // Scripts. wp_enqueue_script('jquery'); wp_enqueue_script('jquery-ui-core'); wp_enqueue_script('jquery-ui-datepicker'); wp_enqueue_script('msa-jquery-daterange-picker-js', MY_SITE_AUDIT_PLUGIN_URL . '/includes/jquery-daterange-picker/jquery.comiseo.daterangepicker.js', array('jquery', 'jquery-ui-core', 'jquery-ui-datepicker', 'jquery-ui-button', 'jquery-ui-tabs', 'jquery-ui-menu', 'jquery-ui-widget', 'msa-moment-js')); wp_enqueue_script('msa-moment-js', MY_SITE_AUDIT_PLUGIN_URL . '/includes/moment/moment.min.js'); wp_enqueue_script('msa-all-audits-js', MY_SITE_AUDIT_PLUGIN_URL . '/js/all-audits.js'); wp_localize_script('msa-all-audits-js', 'msa_all_audits_data', array('site_url' => get_site_url(), 'admin_url' => get_admin_url(), 'audit_page' => get_admin_url() . 'admin.php?page=msa-all-audits', 'add_post_to_audit_nonce' => wp_create_nonce('add_post_to_audit_nonce'), 'update_audit_score_nonce' => wp_create_nonce('update_audit_score_nonce'), 'get_post_ids_nonce' => wp_create_nonce('get_post_ids_nonce'), 'info' => __('Your Audit is now being created and you can monitor its status from the progress bar below. Please <strong>DO NOT</strong> refresh this page as that will stop the audit. If you want to stop this audit for any reason, then click <a href="' . get_admin_url() . 'admin.php?page=msa-all-audits" class="msa-force-stop">Force Stop</a>.', 'msa'), 'success_message' => __('Your Audit has been created! See it ', 'msa'))); if (false === ($show_columns = get_option('msa_show_columns_' . get_current_user_id()))) { $show_columns = array(); } wp_enqueue_script('msa-single-audit-js', MY_SITE_AUDIT_PLUGIN_URL . '/js/single-audit.js'); wp_localize_script('msa-single-audit-js', 'msaSingleAuditData', array('audit_page' => isset($_GET['audit']) ? msa_get_single_audit_link(wp_unslash($_GET['audit'])) : '', 'show_columns' => $show_columns, 'attribute_title' => __('Attributes', 'msa'), 'conditions' => msa_get_conditions(), 'show_column_nonce' => wp_create_nonce('msa_show_column'), 'condition_categories' => msa_get_condition_categories())); wp_enqueue_script('msa-single-post-js', MY_SITE_AUDIT_PLUGIN_URL . '/js/single-post.js'); }
/** * Get total weight of all registered conditions * * @access public * @return int $weight The total condition weight. */ function msa_get_total_conditions_weight() { $conditions = msa_get_conditions(); $weight = 0; foreach ($conditions as $condition) { $weight += $condition['weight']; } return $weight; }
/** * Get all the conditions from a specific category * * @access public * @param mixed $category The condition category. * @return mixed $conditions_in_cat All of the conditions within a category. */ function msa_get_conditions_from_category($category) { if (!isset($category) || '' === $category) { return array(); } $conditions = msa_get_conditions(); $conditions_in_cat = array(); foreach ($conditions as $key => $condition) { if (isset($condition['category']) && $category === $condition['category']) { $conditions_in_cat[$key] = $condition; } } return apply_filters('msa_get_conditions_from_category', $conditions_in_cat); }
/** * Filter the posts for the all posts table * * @access public * @param mixed $posts The original array of WP_Post objects. * @return mixed $posts The filtered array of WP_Post objects. */ function msa_filter_posts($posts) { // Score. if (isset($_GET['score-low']) && '' !== $_GET['score-low'] && isset($_GET['score-high']) && '' !== $_GET['score-high']) { // Input var okay. $score_low = floatval(sanitize_text_field(wp_unslash($_GET['score-low']))); // Input var okay. $score_high = floatval(sanitize_text_field(wp_unslash($_GET['score-high']))); // Input var okay. foreach ($posts as $key => $item) { if ($item['data']['values']['score'] < $score_low || $item['data']['values']['score'] > $score_high) { unset($posts[$key]); } } } // Conditions. $conditions = msa_get_conditions(); foreach ($conditions as $condition) { if (isset($condition['filter']) && isset($_GET[$condition['filter']['name']]) && '' !== $_GET[$condition['filter']['name']]) { // Input var okay. $name = $condition['filter']['name']; $atts = array(); if (isset($_GET[$name])) { // Input var okay. $atts = explode('-', sanitize_text_field(wp_unslash($_GET[$name]))); // Input var okay. } $compare = $atts[0]; $value = $atts[1]; foreach ($posts as $key => $item) { $post_value = $item['data']['values'][$name]; // Compare. if ('more' === $compare) { if (isset($post_value) && $post_value < $value) { unset($posts[$key]); } } else { if ('less' === $compare) { if (isset($post_value) && $post_value > $value) { unset($posts[$key]); } } else { if ('equal' === $compare) { if (isset($post_value) && $post_value !== $value) { unset($posts[$key]); } } else { if ('notequal' === $compare) { if (isset($post_value) && $post_value === $value) { unset($posts[$key]); } } } } } } } } // Attributes. $attributes = msa_get_attributes(); foreach ($attributes as $attribute) { if (isset($attribute['filter']) && isset($_GET[$attribute['filter']['name']]) && '' !== $_GET[$attribute['filter']['name']]) { // Input var okay. $name = $attribute['filter']['name']; $get_name = null; if (isset($_GET[$name])) { // Input var okay. $get_name = sanitize_text_field(wp_unslash($_GET[$name])); // Input var okay. } $posts = apply_filters('msa_filter_by_attribute', $posts, $name, $get_name); } } return $posts; }