function enp_btns_HTML($args) { // get the user $enp_user = new Enp_Button_User(); $enp_btn_HTML = ''; // get the button objects $enp_btns = enp_get_all_btns($args); if (empty($enp_btns)) { return false; // quit now if there aren't any buttons } // classes array for outputting in our HTML $classes = array("enp-btns"); if ($args['btn_type'] === 'comment') { $btn_type = 'comment'; } else { $btn_type = 'post'; } $classes[] = 'enp-btns-' . $btn_type . '-' . $args['post_id']; // check if logged in is set $enp_btn_clickable = enp_btn_clickable(); // check if the first one is full of null values if (enp_button_exists($enp_btns[0])) { // check on icon status $enp_btn_icons = get_option('enp_button_icons'); if ($enp_btn_icons === '0') { $enp_btn_icon_class = 'no-enp-icon-state'; $display_enp_btn_icons = false; } else { $enp_btn_icon_class = 'enp-icon-state'; $display_enp_btn_icons = true; } $enp_btn_HTML = '<div id="enp-btns-wrap-' . $btn_type . '-' . $args['post_id'] . '" class="enp-btns-wrap--disabled enp-btns-wrap ' . $enp_btn_icon_class . '" data-btn-type="' . $btn_type . '"> <ul class="'; foreach ($classes as $class) { $enp_btn_HTML .= $class . ' '; } $enp_btn_HTML .= '">'; foreach ($enp_btns as $enp_btn) { if (enp_button_exists($enp_btn)) { $enp_btn_HTML .= enp_btn_append_btn_HTML($enp_btn, $args, $enp_btn_clickable, $enp_user, $display_enp_btn_icons); } // process button names to pass to the Promote option later $enp_btn_names[] = $enp_btn->get_btn_name(); } $enp_btn_HTML .= '</ul>'; $enp_btn_HTML .= enp_user_clicked_buttons_HTML($enp_user, $enp_btns, $btn_type, $args['post_id']); if ($enp_btn_clickable === false) { // append a login link and message // redirect them back to this button section if ($btn_type === 'post') { $redirect = get_permalink() . '/#enp-btns-wrap-' . $btn_type . '-' . $args['post_id']; } elseif ($btn_type === 'comment') { $redirect = get_comment_link($args['post_id']); } else { // hmmm... which type are they on? $redirect = site_url(); } $enp_btn_HTML .= '<p class="enp-btn-hint enp-hint--please-log-in">Please <a href="' . wp_login_url($redirect) . '">Log In</a> to click the buttons</p>'; } if ($btn_type === 'post' && !empty($enp_btn_names)) { $return = true; $enp_btn_HTML .= promote_enp_HTML($enp_btn_names, $return); // true returns instead of echos } // no script reference $enp_btn_HTML .= '<noscript><p class="enp-btn-hint">Enable Javascript to click a button</p></noscript>'; $enp_btn_HTML .= '</div>'; // close enp-btns-wrap } return $enp_btn_HTML; }
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; }