function promote_enp_HTML($enp_btn_names = false, $return = false) { // check to see if promote_enp is set to true. If it's not, get outta here if (promote_enp() !== true) { return false; } if ($enp_btn_names === false || empty($enp_btn_names)) { // we're in the comments section... gotta find all our button names $args = array('btn_type' => 'comment'); // get all buttons that are active for comments $enp_btns = enp_get_all_btns($args); if ($enp_btns === null) { return false; // quit now if it's all null } // check to make sure it's not just null values // check to make sure it's not just null values if (enp_button_exists($enp_btns[0])) { foreach ($enp_btns as $enp_btn) { if (enp_button_exists($enp_btn)) { $enp_btn_names[] = $enp_btn->get_btn_name(); } } } } // Return Array of buttons being displayed $enp_btn_name_text = ''; if (!empty($enp_btn_names)) { $enp_btn_name_text = enp_build_name_text($enp_btn_names); $names_count = count($enp_btn_names); if ($names_count === 1 || $names_count === 0) { $button_pluralize = ''; } else { $button_pluralize = 's'; } } $promote_HTML = '<p class="enp-promote">' . $enp_btn_name_text . ' Button' . $button_pluralize . ' powered by the <a href="http://engagingnewsproject.org">Engaging News Project</a></p>'; if ($return === true) { return $promote_HTML; // return for appending to HTML } else { echo $promote_HTML; // echo for action hooks } }
function enp_process_update_button_count($pid, $btn_slug, $btn_type, $operator, $user_id) { // Instantiate WP_Ajax_Response $response = new WP_Ajax_Response(); // Verify Nonces if (wp_verify_nonce($_REQUEST['nonce'], 'enp_button_' . $btn_type . '_' . $btn_slug . '_' . $pid)) { global $wpdb; if ($btn_type === 'post') { // set our function names $get_meta = 'get_post_meta'; $update_meta = 'update_post_meta'; } elseif ($btn_type === 'comment') { // set our function names $get_meta = 'get_comment_meta'; $update_meta = 'update_comment_meta'; } else { // wait, what kind of post is it then? return; } // get post or comment meta and update it $prev_clicks = $get_meta($pid, 'enp_button_' . $btn_slug, true); // increase the click by one $new_clicks = changeClickCount($prev_clicks, $operator); // update the post or comment meta $update_meta($pid, 'enp_button_' . $btn_slug, $new_clicks); // switch the operator for the next post if ($operator === '+') { $new_operator = '-'; } else { $new_operator = '+'; } // update the user if there's an ID to use $enp_clicked_btn_HTML = ''; if ($user_id !== '0') { // get their previous clicks $user_clicks = get_user_meta($user_id, 'enp_button_' . $btn_type . '_' . $btn_slug, true); if (empty($user_clicks)) { // if it's empty, it'll return an empty string, but we want an array $user_clicks = array(); } // are we increasing or decreasing? if ($operator === '+') { // add the Button ID to the Array $key = array_search($pid, $user_clicks); if ($key === false) { // They haven't clicked this one before, so add it $user_clicks[] = $pid; } } else { // search the array and return the key/index $key = array_search($pid, $user_clicks); if ($key !== false) { // remove this one from the array array_splice($user_clicks, $key, 1); } } update_user_meta($user_id, 'enp_button_' . $btn_type . '_' . $btn_slug, $user_clicks); // Build button message text $args = array('post_id' => $pid, 'btn_type' => $btn_type); $enp_btns = enp_get_all_btns($args); $enp_user = new Enp_Button_User(array('user_id' => $user_id)); $enp_clicked_btn_HTML = enp_user_clicked_buttons_HTML($enp_user, $enp_btns, $btn_type, $pid); } else { $enp_clicked_btn_HTML = ''; // we need to check localStorage for button clicks } // update our rebuild flag update_option('enp_rebuild_popular_data', '1'); $response->add(array('data' => 'success', 'supplemental' => array('pid' => $pid, 'slug' => $btn_slug, 'type' => $btn_type, 'message' => 'The click on ' . $pid . ' has been registered!', 'count' => $new_clicks, 'old_count' => $prev_clicks, 'new_operator' => $new_operator, 'user_clicked_message' => $enp_clicked_btn_HTML))); } else { $response->add(array('data' => 'error', 'supplemental' => array('pid' => $pid, 'slug' => $btn_slug, 'type' => $btn_type, 'message' => 'We couldn\'t update the ' . ucfirst($btn_slug) . ' button count. Reload this page and try again.'))); } // Send the response back $response->send(); // Always end with an exit on ajax exit; }