Forum Replies Created

Viewing 1 replies (of 1 total)
  • Thread Starter aabella

    (@aabella)

    wordpress 4.4

    4, 'btn_slug' => 'respect', 'btn_type' => 'post' ); $test_btn = new Enp_Button($args); var_dump($test_btn);*/ //public function __construct($slug = false, $is_comment = false) { public function __construct($args = array()) { // arguments for querying the Enp_Button object $default_args = array( 'post_id' => false, // set to post or comment id 'btn_slug' => false, // set to slug string or array of strings, "respect", "recommend", "important". also accepts array 'btn_type' => false // slug of the post type. post, page, comment, or cpt slug ); $args = array_merge($default_args, $args); if($args['btn_slug'] === false) { // return all buttons if they didn't specify which one // USAGE: $enp_btns = new Enp_Button(); // $enp_btns = $enp_btns->get_btns(); $this->get_btns($args); } else { // get the one they asked for // USAGE: $enp_btn = new Enp_Button('respect'); $enp_btn = $this->set_btn($args); } } /* NOT IN USE public function get_btn($args) { $enp_btn_values = get_option('enp_buttons_'.$slug); return $this->set_btn($enp_btn_values); }*/ protected function set_btn($args) { // try/catch to return exception if no button is found try { // get the data from wp_options if($args['btn_slug'] !== false) { $slug = $args['btn_slug']; $enp_btn = get_option('enp_button_'.$slug); } else { $enp_btn = false; throw new Exception('Enp_Button: No btn_slug set.'); } if($enp_btn !== false) { $this->btn_type = $this->set_btn_type($enp_btn, $args); // check to see if the button types return true if($this->btn_type !== false) { $this->btn_slug = $this->set_btn_slug($enp_btn); // set all the attributes $this->btn_name = $this->set_btn_name($enp_btn); $this->btn_count = $this->set_btn_count($enp_btn['btn_slug'], $args); $this->btn_lock = $this->set_btn_lock(); } else { // throw new Exception('Enp_Button: No button found for that btn_type'); } } else { throw new Exception('Enp_Button: No button found'); } } catch(Exception $e) { // return our exception echo $e->getMessage(); } } /* * set the button slug for the Enp_Button object */ protected function set_btn_slug($enp_btn) { $slug = false; if(isset($enp_btn['btn_slug'])) { $slug = $enp_btn['btn_slug']; } return $slug; } /* * * set the button name for the Enp_Button object * */ protected function set_btn_name($enp_btn) { $name = false; if(isset($enp_btn['btn_name'])) { $name = $enp_btn['btn_name']; } return $name; } /* * * set the button type for the current Enp_Button object * as an array of types - ie - ['btn_type'] => array('comment' => false, 'posts' => true) * */ protected function set_btn_type($enp_btn, $args) { $btn_type = false; if(isset($enp_btn['btn_type'])) { $btn_type = $enp_btn['btn_type']; } // check if our args match at all. If there's a match, then we can return the object if($btn_type !== false && $args['btn_type'] !== false) { $btn_type_match = false; // this is a string, so we can check if that btn_type is set to true // because this will check for $btn_type['comment'] = '1', etc if( $btn_type[$args['btn_type']] !== false ) { $btn_type_match = true; } } else { $btn_type_match = true; // because there's nothing requested (false), // not really a match but we should return the object } if($btn_type_match === true) { return $btn_type; } else { return false; } // TODO? // If a custom post type gets added, this will throw a PHP notice // that $btn_type['custom_post'] is not set // The way to set it would be loop through ALL active post types // with registeredContentTypes() and set any post types that aren't // set as false. It's an extra check for something that's not a big // deal though, so I'm not sure if it's worth the resources or not } /* * * Set the btn count value * */ protected function set_btn_count($btn_slug, $args) { $enp_btn_count = false; if($args['btn_type'] === 'comment') { if($args['post_id'] !== false) { $comment_id = $args['post_id']; } else { global $comment; $comment_id = $comment->comment_ID; } $enp_btn_count = get_comment_meta($comment_id, 'enp_button_'.$btn_slug, true); } elseif(!is_admin()) { $post_id = false; if($args['post_id'] !== false) { $post_id = $args['post_id']; } else { global $post; $post_id = $post->ID; } if($post_id !== false) { // individual post button $enp_btn_count = get_post_meta($post_id,'enp_button_'.$btn_slug, true); } else { // TODO: get a global count? // $enp_btn_count = get_option('enp_button_'.$enp_btn['btn_slug']); } } // default 0 if nothing is found/posted yet $count = 0; if($enp_btn_count !== false) { $count = (int) $enp_btn_count; } return $count; } /* * * Set the btn lock value. if count is 0 or greater, lock it * */ protected function set_btn_lock() { $lock = false; // if btn_count is greater than 0, lock it if($this->btn_count > 0) { $lock = true; } return $lock; } /* * * returns the button slug for the current Enp_Button object * USAGE: $enp_btn = new Enp_Button('respect'); * $enp_btn->get_btn_slug; // 'respect' * */ public function get_btn_slug() { return $this->btn_slug; } /* * * returns the button name for the current Enp_Button object * USAGE: $enp_btn = new Enp_Button('respect'); * $enp_btn->get_btn_name; // 'Respect' * */ public function get_btn_name() { return $this->btn_name; } public function get_btn_types() { return $this->btn_type; } public function get_btn_count() { return $this->btn_count; } public function get_formatted_btn_count() { $formatted_count = $this->btn_count; if(1000 <= $formatted_count ){ if(1000000 <= $formatted_count) { $format_symbol = 'm'; $format_divide = 100000; } else { $format_symbol = 'k'; $format_divide = 1000; } $formatted_count = $formatted_count/$format_divide; $formatted_count = floor($formatted_count * 10) / 10; // get our decimal places (ie 2.5) $formatted_count = number_format($formatted_count, 1); // removes .0 from end of number, if it's a .0 // ex- 12.0 becomes 12 $formatted_count = $formatted_count + 0; $formatted_count_html = $formatted_count.''.$format_symbol.''; } else { $formatted_count_html = number_format($formatted_count); } return apply_filters('enp_formatted_btn_count', $formatted_count_html); } public function get_btn_lock() { return $this->btn_lock; } /* * * get an individual button type * returns array of types - ie - array('comment' => false, 'posts' => true) * */ public function get_btn_type($type = false) { $btn_type = $this->btn_type; $get_btn_type = false; if($type !== false && isset($btn_type[$type])) { $get_btn_type = $btn_type[$type]; } return $get_btn_type; } /* * * Return all button slugs from enp_button_slugs in an array * Used by function get_btns() * */ public function get_btn_slugs() { $enp_button_slugs = get_option('enp_button_slugs'); return $enp_button_slugs; } /* * Return all buttons as an array of individual objects * (ie- $this->get_btns() = array([0]=> object(Enp_Button){[btn_slug]=>'', [btn_name]=>''}, * [1]=> object(Enp_Button){[btn_slug]=>'', [btn_name]=>''}); * * USAGE * $enp_btns = new Enp_Button(); * $enp_btns = $enp_btns->get_btns(); * foreach($enp_btns as $enp_btn) { * echo '
    '.$enp_btn->get_btn_name().'
    '; * // Outputs button name (ie- Recommend, Respect, Important, ...) * } * */ public function get_btns($args = false) { $enp_btns = $this->get_btn_slugs(); if($enp_btns === false) { return false; // no slugs set yet } $enp_btns_obj = array(); foreach($enp_btns as $slug) { $args['btn_slug'] = $slug; $enp_btns_obj[] = new Enp_Button($args); } if($enp_btns_obj !== null) { $i = 0; foreach($enp_btns_obj as $obj) { // remove any null objects if($obj->btn_slug === NULL) { unset($enp_btns_obj[$i]); //removes the array at given index $reindex = array_values($enp_btns_obj); //normalize index $enp_btns_obj = $reindex; //update variable } $i++; } } return $enp_btns_obj; } } ?> '; } /* * * Register and enqueue style sheet & scripts. * */ public function enp_btn_register_scripts() { $version = '1.0.3'; // get our style choice from the database $enp_btn_style = get_option('enp_button_style'); if(!empty($enp_btn_style)) { $style_path = plugins_url( 'engaging-buttons/front-end/css/enp-button-'.$enp_btn_style.'.min.css' ); } else { $style_path = plugins_url( 'engaging-buttons/front-end/css/enp-button-plain-styles.min.css' ); } wp_register_style( 'enp-button-style', $style_path, array(), $version); wp_enqueue_style( 'enp-button-style' ); // add custom button color CSS, if necessary $enp_button_css = get_option('enp_button_color_css'); if($enp_button_css !== false && !empty($enp_button_css) ) { wp_add_inline_style( 'enp-button-style', $enp_button_css ); } $enp_button_font = get_option('enp_button_font'); if($enp_button_font === 'open_sans') { wp_register_style( 'open_sans', 'https://fonts.googleapis.com/css?family=Open+Sans:600'); wp_enqueue_style( 'open_sans' ); // yeah, 14.28px is weird for a font size, but it's what WordPress Admin computes it to // and we want it to match what people see on the admin panel $enp_open_sans_css = ' body .enp-btns-wrap .enp-btn { font-family: "Open Sans", Helvetica Neue, Helvetica, Arial, sans-serif; font-weight: 600; font-size: 14.28px; } body .enp-btns-wrap .enp-btn-wrap { margin-top: 6px; }'; wp_add_inline_style( 'enp-button-style', $enp_open_sans_css ); } wp_register_script( 'enp-button-scripts', plugins_url( 'engaging-buttons/front-end/js/scripts.min.js' ), array( 'jquery' ), $version, true); wp_enqueue_script( 'enp-button-scripts' ); // in JavaScript, object properties are accessed as enp_button_params.ajax_url, enp_button_params.attr_name // This writes the params to the document // Get the protocol of the current page $protocol = isset( $_SERVER['HTTPS'] ) ? 'https://' : 'https://'; $is_enp_btn_clickable = enp_btn_clickable(); if($is_enp_btn_clickable === false) { $is_enp_btn_clickable = 0; } else { $is_enp_btn_clickable = 1; } $user_id = get_current_user_id(); // will return 0 if none found $login_url = wp_login_url( get_permalink() ); wp_localize_script( 'enp-button-scripts', 'enp_button_params', array( 'ajax_url' => admin_url( 'admin-ajax.php', $protocol ), 'enp_btn_clickable'=> $is_enp_btn_clickable, 'enp_login_url'=>$login_url, 'enp_btn_user_id'=> $user_id) ); } } // fire up our styles and scripts $init = new Enp_Button_Loader(); ?> get_current_user_id(), // try to set the current user id 'btn_slug' => false, // set to slug string or array of strings, "respect", "recommend", "important". also accepts array 'btn_type' => false // post or comment. we don't care if it's a page or cpt ); // merge the default_args and args arrays $args = array_merge($default_args, $args); $this->user_id = $args['user_id']; // sets both $this->clicked_posts and $this->clicked_comments $this->set_user_clicks($args); } protected function set_user_clicks($args) { $btn_slugs = $this->set_btn_slugs($args); if($btn_slugs === false) { return false; // no slugs, get outta } $btn_types = $this->set_btn_types($args); $clicked_btns = array(); // if we have a string, lets turn it into an array to keep our processing // code more DRY if(!is_array($btn_slugs)) { $btn_slugs = array($btn_slugs); } if(!is_array($btn_types)) { $btn_types = array($btn_types); } foreach($btn_slugs as $btn_slug) { // create an empty array for this btn_slug ${'clicked_'.$btn_slug} = array(); // loop through the slugs foreach($btn_types as $btn_type) { // for each btn_type, push the user meta for that button to the clicked_btn_slug array as an associative array ${'clicked_'.$btn_slug}[$btn_type] = get_user_meta($this->user_id, 'enp_button_'.$btn_type.'_'.$btn_slug, true); } // dynamic class variable name... ugly, but awesome // sets $this->recommend or $this->respect, etc, as the // associative array we created $this->$btn_slug = ${'clicked_'.$btn_slug}; } } protected function set_btn_slugs($args) { $btn_slugs = $args['btn_slug']; if($btn_slugs === false) { $btn_slugs = get_option('enp_button_slugs'); } return $btn_slugs; } protected function set_btn_types($args) { $btn_types = $args['btn_type']; if($btn_types === false) { // set it as both post and comment $btn_types = array('post', 'comment'); } return $btn_types; } /* * A function for getting the clicks you want * returns an array of all clicks for that button for the requested user * * USAGE: $btn_user = new Enp_Button_User(); * $respect_post_clicks = $btn_user->get_user_clicks('respect', 'post'); * $respect_clicks = $btn_user->get_user_clicks('respect'); * */ public function get_user_clicks($btn_slug, $btn_type = false) { //var_dump($this); if($btn_type !== false && $btn_slug !== null) { // This gives us an illegal offset error // $user_clicks = $this->$btn_slug[$btn_type]; // But settings the user_clicks, then getting user_clicks[$btn_type] // doesn't throw the error for some reason $user_clicks = $this->$btn_slug; $user_clicks = $user_clicks[$btn_type]; } else { $user_clicks = $this->$btn_slug; } return $user_clicks; } /* * * Bool check to see if a user has clicked a button or not * */ public function has_user_clicked($btn, $args) { // check to see if there's even a user and that the btn is active if($this->user_id == 0 ) { return false; } $default_args = array( 'btn_slug' => false, // set to slug string or array of strings, "respect", "recommend", "important". also accepts array 'btn_type' => false, // post or comment. we don't care if it's a page or cpt 'post_id' => false // the post id of the button you want to see if they've clicked ); // merge the default_args and args arrays $args = array_merge($default_args, $args); if($this->is_btn_active($btn, $args) === false) { return false; } // get the user's clicks $user_clicks = $this->get_user_clicks($args['btn_slug'], $args['btn_type']); if(empty($user_clicks)) { // there's no value set yet return false; }; // see if the post id is in their array for those button clicks return in_array($args['post_id'], $user_clicks); } public function is_btn_active($btn, $args) { if($btn->btn_type === false) { return false; } $is_active = false; if($args['btn_type'] === 'comment') { // check if the comments are active if($btn->btn_type['comment'] === '1') { $is_active = true; } } else { // check if ANY btn_type is active $is_active = in_array('1', $btn->btn_type); } return $is_active; } } ?> 'Respect', 'btn_type'=>'comment' ); $pop_posts = new Enp_Popular_Buttons($args); // object{ 'btn_slug' => 'respect'; 'btn_name' => 'Respect'; 'btn_past_tense_name' => 'Respected'; 'btn_type' => 'comment'; 'popular_posts' =array( // this array is useful for basic foreach loops array('post_id'=>1, 'btn_count'=>89 ), array('post_id'=>19, 'btn_count'=>59 ), array('post_id'=>6, 'btn_count'=>16 ) ); 'popular_posts_by_id' = array([1]=>1, // this array is useful for wp_query loops [2]=>19, [3]=>6) } */ class Enp_Popular_Buttons { public function __construct($args = array()) { $default_args = array( 'btn_slug' => false, // set to slug string or array of strings, "respect", "recommend", "important". also accepts array 'btn_type' => 'all_post_types', // slug of the post type. post, page, comment, or cpt slug 'comments' => false // flag to get comments of a post type ); // merge the default args $args = array_merge($default_args, $args); // set our label ('comments' or 'posts') $this->label = $this->get_label_type($args); if($args['btn_slug'] === false) { // If btn_slug == false, get active button slugs, pass to the construct, // and return array of all popular post objects by slug $this->get_all_popular_buttons($args); } else { $this->set_popular_button($args); } // remove the label from the object, since we don't need to pass it in public //unset($this->label); } public function the_content($content) { return $content . '

    Thanks!
    '; } /* * Main setter function. Pass it the filled in args * and this will hook it all up */ protected function set_popular_button($args) { $this->btn_slug = $args['btn_slug']; $this->btn_type = $args['btn_type']; $this->btn_name = $this->set_btn_name($this->btn_slug); $this->btn_past_tense_name = $this->set_past_tense_btn_name($this->btn_slug); $this->{'popular_'.$this->label} = $this->set_popular_posts($args); $this->{'popular_'.$this->label.'_by_id'} = $this->set_popular_posts_by_id($args); } /* * Grab the array straight from the database. * All the hard work has already been done */ protected function set_popular_posts($args) { try{ if($this->btn_slug === false) { throw new Exception('Enp_Popular_Buttons: No btn_slug set.'); } $pop_posts = false; // figure out which option value we need to grab // if all posts type and we want posts if($args['btn_type'] === 'all_post_types' && $this->label === 'posts') { $pop_posts = get_option( 'enp_button_popular_'.$args['btn_slug'] ); } // we have btn_slug and individual button type that's not comments, so return that elseif($args['btn_type'] !== 'all_post_types' && $this->label === 'posts') { $pop_posts = get_option( 'enp_button_popular_'.$args['btn_slug'].'_'.$args['btn_type'] ); } // if we have a specific btn_type and want comments elseif($args['btn_type'] !== 'all_post_types' && $this->label === 'comments') { $pop_posts = get_option( 'enp_button_popular_'.$args['btn_slug'].'_'.$args['btn_type'].'_comments' ); } // if we want all comments regardless of post type elseif($args['btn_type'] === 'all_post_types' && $this->label === 'comments') { $pop_posts = get_option( 'enp_button_popular_'.$args['btn_slug'].'_comments' ); } else { throw new Exception('Enp_Popular_Buttons: We could not find any values to return.'); } return $pop_posts; } catch(Exception $e) { // return our exception echo $e->getMessage(); } } /* * Process the popular_posts array and just grab * all the Ids into one array */ protected function set_popular_posts_by_id($args) { $pop_posts_by_id = array(); // check to make sure the popular_$label field has values if(!empty($this->{'popular_'.$this->label})) { // set the dynamic array key label $singular_label = $this->get_singular_label(); // loop through popular_$label and push all the IDs into an array foreach($this->{'popular_'.$this->label} as $pop_post) { $pop_posts_by_id[] = $pop_post[$singular_label.'_id']; } } // return the array return $pop_posts_by_id; } protected function get_label_type($args) { if($args['btn_type'] === 'comment' || $args['comments'] === true) { $label = 'comments'; } else { $label = 'posts'; } return $label; } protected function get_singular_label() { if($this->label === 'comments') { $singular_label = 'comment'; } else { $singular_label = 'post'; } return $singular_label; } protected function set_btn_name($btn_slug) { return ucfirst($btn_slug); } protected function set_past_tense_btn_name($btn_slug) { if($btn_slug === 'respect') { $pt_name = 'Respected'; } elseif($btn_slug === 'recommend') { $pt_name = 'Recommended'; } elseif($btn_slug === 'important') { $pt_name = 'Important'; } else if($btn_slug === 'thoughtful') { $pt_name = 'Thoughtful'; } else if($btn_slug === 'useful') { $pt_name = 'Useful'; } return ucfirst($pt_name); } /* * If no slug was passed, we need to join all of them into * an array of popular button objects * USAGE: $pop_btns = new Enp_Popular_Buttons(); * var_dump($pop_btns->get_all_popular_buttons()); */ public function get_all_popular_buttons($args = array()) { $btn_slugs = get_option('enp_button_slugs'); $pop_posts_objs = array(); try { if(empty($btn_slugs)) { throw new Exception('Enp_Button: There are no button slugs saved in the database. Please go to Engaging Buttons settings and create and save a button.'); } } catch(Exception $e) { // return our exception echo $e->getMessage(); return false; } $i = 0; foreach($btn_slugs as $slug) { $args['btn_slug'] = $slug; $pop_posts_objs[] = new Enp_Popular_Buttons($args); $i++; } return $pop_posts_objs; } } //new Enp_Popular_Buttons(); ?> set_defaults(); // sets the data $this->site_url = site_url(); // if we want to move back to loops by default // $this->send_all_engaging_data(); } /* * * Sets a default array of all data to send when batch processing from mysql * */ protected function set_batch_data_defaults($row, $slug) { $defaults = array( 'site_url' => $this->site_url, 'meta_id' => $row->meta_id, 'button' => $slug, 'clicks' => $row->meta_value, 'post_id' => null, 'comment_id' => null, 'post_type' => null, 'button_url' => null, ); return $defaults; } public function send_click_data($data) { global $wpdb; try { if(empty($data['type'])) { throw new Exception('Enp_Send_Data: No $data[\'type\'] set in send_click_data.'); } $row = $wpdb->get_row( 'SELECT * FROM '.$wpdb->{$data['type'].'meta'}.' WHERE '.$data['type'].'_id = "'.$data['button_id'].'" AND meta_key = "enp_button_'.$data['slug'].'" LIMIT 1'); if($row === null) { throw new Exception('Enp_Send_Data: Row not found in send_click_data.'); } // get comment or post data if($data['type'] === 'comment') { $comment_id = $data['button_id']; $post_id = $wpdb->get_var('SELECT comment_post_ID FROM '.$wpdb->comments.' WHERE comment_ID = "'.$comment_id.'" LIMIT 1'); if($post_id === null) { throw new Exception('Enp_Send_Data: $post_id not found by get_var in send_click_data.'); } $post_type = 'comment'; } else { $post_id = $data['button_id']; $comment_id = '0'; $post_type = get_post_type($post_id); } $send_data = array( 'site_url' => $this->site_url, 'meta_id' => $row->meta_id, 'button' => $data['slug'], 'clicks' => $row->meta_value, 'post_id' => $post_id, 'comment_id' => $comment_id, 'post_type' => $post_type, 'button_url' => $data['button_url'] ); // send data to web service $send = $this->send_data($send_data); } catch(Exception $e) { $this->send_error($e->getMessage(), $data); } } /* * * Main processing function. Gets all active button slugs and * */ public function send_all_engaging_data() { $slugs = get_option('enp_button_slugs'); // active slugs if($slugs != false) { foreach($slugs as $slug) { $this->build_and_send_post_data($slug); $this->build_and_send_comment_data($slug); } } else { return false; } } /* * * Builds and sends all post data * */ protected function build_and_send_post_data($slug) { global $wpdb; $meta_rows = $wpdb->get_results( 'SELECT * FROM '.$wpdb->postmeta.' WHERE meta_key = "enp_button_'.$slug.'"'); foreach($meta_rows as $row) { // set the post ID $post_id = $row->post_id; $post_type = get_post_type($post_id); // our unique data for this row $post_data = array( 'post_id' => $post_id, 'comment_id' => 0, 'post_type' => $post_type, 'button_url' => $this->set_post_button_url($post_type, $post_id), ); // send data $this->process_and_send_data($row, $post_data, $slug); } } /* * * Builds and sends all comment data * */ protected function build_and_send_comment_data($slug) { global $wpdb; $meta_rows = $wpdb->get_results( 'SELECT * FROM '.$wpdb->commentmeta.' WHERE meta_key = "enp_button_'.$slug.'"'); foreach($meta_rows as $row) { // set the post ID $comment_id = $row->comment_id; // get post ID from comment table $post_id = $wpdb->get_var('SELECT comment_post_ID FROM '.$wpdb->comments.' WHERE comment_ID = "'.$comment_id.'" LIMIT 1'); // our unique data for this row $comment_data = array( 'post_id' => $post_id, 'comment_id' => $comment_id, 'post_type' => 'comment', 'button_url' => $this->set_comment_button_url($comment_id, $post_id), ); // send data $this->process_and_send_data($row, $comment_data, $slug); } } protected function set_post_button_url($post_type, $post_id) { if($post_type === 'post') { $url_slug = 'p'; } elseif($post_type === 'page') { $url_slug = 'page_id'; } else { $url_slug = 'post_type='.$post_type.'&p'; } return $this->site_url .'/?'.$url_slug.'='.$post_id; } protected function set_comment_button_url($comment_id, $post_id) { $post_type = get_post_type($post_id); // build the url off of our post_button_url function $post_url = $this->set_post_button_url($post_type, $post_id); // append the comment link return $post_url.'#comment-'.$comment_id; } protected function process_and_send_data($row, $data, $slug) { // get our default array $data_defaults = $this->set_batch_data_defaults($row, $slug); // merge our default and our row data $merged_data = array_merge($data_defaults, $data); // send data to web service $this->send_data($merged_data); } /* * * Error handling * */ protected function send_error($error_message, $data = array('no data found')) { $error_data = array( 'error' => $error_message, 'timestamp'=> date("Y-m-d H:i:s") ); if(is_array($data)) { $error_data = array_merge($error_data, $data); } // TODO: I would love if there was just one curl connection since this is duplicate code // But it's too easy to get stuck in a never ending loop of error/post if we reuse the send_data function // send the data $data_json = json_encode($error_data); $ch = curl_init(); // local // curl_setopt($ch, CURLOPT_URL, 'https://dev/enp-api/api.php'); // live curl_setopt($ch, CURLOPT_URL, 'https://fda668417f344263bdb9e66a5904eaf5.engagingnewsproject.org/api.php'); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_POSTFIELDS, $data_json); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json', 'Content-Length: ' . strlen($data_json)) ); curl_exec($ch); curl_close($ch); } public function send_data($data = false) { try { if($data === false) { throw new Exception('Enp_Send_Data: No $data in send_data.'); } // encode to json $data_json = json_encode($data); // if our data is not valid, quit if($data_json === false) { throw new Exception('Enp_Send_Data: Couldn\'t encode $data to json in send_data.'); } // open connection $ch = curl_init(); // local // curl_setopt($ch, CURLOPT_URL, 'https://dev/enp-api/api.php'); // live curl_setopt($ch, CURLOPT_URL, 'https://fda668417f344263bdb9e66a5904eaf5.engagingnewsproject.org/api.php'); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_POSTFIELDS, $data_json); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json', 'Content-Length: ' . strlen($data_json)) ); $json_result = curl_exec($ch); // decode results $result = json_decode($json_result); $curl_error = false; // check for errors if(curl_errno($ch)) { $curl_error = curl_error($ch); } // close our connection curl_close($ch); // throw error. We can't throw this error before we close the connection, // otherwise we don't have access to the error or we problematically // leave the connection open if($curl_error !== false) { throw new Exception($curl_error); } return $result; } catch(Exception $e) { $this->send_error($e->getMessage(), $data); } } } ?> $value['btn_slug'], 'btn_name' => $value['btn_name'], 'btn_type' => $value['btn_type'], // 'locked' => false, // TODO: Set to true if count is more than 0 (has clicks) ); // update the database entry update_option('enp_button_'.$value['btn_slug'], ${'enp_button_'.$value['btn_slug']}); } } /* * * Process values for updating enp_popular_slugs * */ function update_enp_display_popular_slugs($values) { $enp_popular_slugs = array(); // loop through the posted array and add JUST the button slugs foreach($values as $value) { // check to see if it's set if(isset($value['display_popular'])) { // check to see if they want to display the popular posts from that slug if($value['display_popular'] === '1') { $enp_popular_slugs[] = $value['btn_slug']; } } } update_option('enp_display_popular_slugs', $enp_popular_slugs); } /* * * If a button type wasn't selected, it doesn't get added to the array as false * This function sets all unset values as false so we squash php notices * */ function set_unset_btn_type_values($values) { // get all the registered content types as an array $registered_content_types = registeredContentTypes(); $i = 0; $set_options = array(); foreach($values as $value) { // check each content type to see if it's set // if it's not, then set it to false foreach($registered_content_types as $type) { // set it to false if it wasn't set if(!isset($values[$i]['btn_type'][$type['slug']])) { // update the original $values too $values[$i]['btn_type'][$type['slug']] = false; } else { // flag if NONE were set and return an error message $set_options[$i] = $values[$i]['btn_slug']; } } $i++; } if(count($set_options) !== count($values)) { // return notification to pleeeease turn this value on add_settings_error( 'enp-no-button-type', 'enp-nag', 'You did not select where to use one or more of your buttons.', 'error' ); } return $values; } /* * * Add name value for enp_button_$slug * */ function add_enp_button_names($values) { $i = 0; // loop through values and add the btn_name in foreach($values as $value) { $values[$i]['btn_name'] = ucfirst($values[$i]['btn_slug']); $i++; } return $values; } /* * * Gets all registered content types (posts, pages, custom posts) * Need to set the btn_type variable before saving * Returns an array( [0] => array('slug'=>'comment', 'label_name'=>'Comments'), * [1] => array('slug'=>'post', 'label_name'=>'Posts'), * [2] => array(...) * ) * */ function registeredContentTypes() { // only get public post types $post_type_args = array('public' => 'true'); // this will get us all post type objects $post_types = get_post_types( $post_type_args, 'objects' ); // set our empty array value $registered_content_types = array(); // add in the comments array $registered_content_types[] = array( 'slug'=>'comment', 'label_name'=>'Comments' ); // Loop through all active post types foreach ( $post_types as $post_type ) { // we don't need the attachment (media) post type if($post_type->name != 'attachment') { // build our array to return the slug and singular name of each active post type $registered_content_types[] = array( "slug" => $post_type->name, "label_name" => $post_type->labels->name, ); } } return $registered_content_types; } function set_enp_button_icons($value) { if(empty($value)) { $value = 0; } return $value; } // we're setting it to open_sans for now, // but we could expand this to a dropdown later and // offer multiple font choices function set_enp_button_font($value) { if(empty($value)) { $value = 'theme_font'; } else { $value = 'open_sans'; } return $value; } function set_enp_button_allow_data_tracking($value) { if(empty($value)) { // return notification to pleeeease turn this value on add_settings_error( 'enp-nag-for-data', 'enp-nag', 'Please turn on "Allow data collection" so that we can continue to provide high-quality, open-source plugins. We will only use your Engaging Button data anonymously for research with the Engaging News Project.', 'error' ); } elseif ($value === '1') { // check to see if it already was on 1 $old_allow_data_tracking_value = get_option('enp_button_allow_data_tracking'); // if it doesn't equal 1, then we should send it over if($old_allow_data_tracking_value != 1) { // send all current data since they just turned it on $send_data = new Enp_Send_Data(); $send_data->send_all_engaging_data(); } } return $value; } function set_enp_button_color($value) { $hex = false; // validate the hex value if(enp_validate_color($value) === true) { $hex = $value; } return $hex; } function set_enp_button_color_clicked($value) { $hex = enp_hex_check_and_return_color($value, -0.25); return $hex; } function set_enp_button_color_active($value) { $hex = enp_hex_check_and_return_color($value, 0.15); return $hex; } function set_enp_button_color_css($value) { if(empty($value)) { // checks to see if there's a button style wanted, // and creates the stylesheet options if so $value = enp_create_button_css(); } return $value; } ?> $value) { // check if the button type is active if($value === '1') { $popular_args = array( 'meta_key' => $meta_key, 'orderby' => 'meta_value_num', 'order' => 'DESC' ); // if it's a comment, we need to process it differently if($key === 'comment') { $process_comments = true; } else { // reset the array with the new post type $popular_post_args = array( 'post_type' => $key, 'posts_per_page'=> 20 // TODO: Override this via Admin option; ); $popular_post_args = array_merge($popular_args, $popular_post_args); enp_popular_posts_save($btn_slug, $popular_post_args); } } // end if } // end foreach btn_type if($process_comments === true) { // strip out popular args post type enp_popular_comments_save($btn_slug, $btn_info['btn_type'], $popular_args); } // pass the slug to our all posts save function enp_all_popular_posts_save($btn_slug, $btn_info); } // end foreach btn_slug } // function enp_popular_comments_save($btn_slug, $post_types, $args) { // all comments by btn slug (combines pages, posts, etc. anywhere the button is shown) $comment_args = array( 'fields' => 'ids', 'status' => 'approve', 'number' => 20 // TODO: Override this via Admin option ); $args = array_merge($comment_args, $args); $comments_query = new WP_Comment_Query; $comments = $comments_query->query( $args ); $popular_comments = enp_build_popular_array($btn_slug, $comments, 'comment'); update_option('enp_button_popular_'.$btn_slug.'_comments', $popular_comments); // Loop through all the passed post_types and // save all comments by post type // ex: enp_button_popular_respect_page_comments foreach($post_types as $key=>$value) { // check if the button type is active if($value === '1' && $key !== 'comment') { // build the arguments $post_type_args = array('post_type'=>$key); $post_type_args = array_merge($args, $post_type_args); // generate the query $comments_query = new WP_Comment_Query; $comments = $comments_query->query( $post_type_args ); // build the array of popular ids and counts $popular_comments = enp_build_popular_array($btn_slug, $comments, 'comment'); // save the array update_option('enp_button_popular_'.$btn_slug.'_'.$key.'_comments', $popular_comments); } } } /* * Save most clicked by slug and post type in wp_options * 'enp_button_popular_'.$btn_slug.'_'.$post_type */ function enp_popular_posts_save($btn_slug, $args) { $pop_posts = get_posts( $args ); $post_type = $args['post_type']; $popular_posts = enp_build_popular_array($btn_slug, $pop_posts, $post_type); update_option('enp_button_popular_'.$btn_slug.'_'.$post_type, $popular_posts); // Restore original Post Data wp_reset_postdata(); } /* * Loop through the returned ids, get the count, and return * an array of arrays of ids + button count */ function enp_build_popular_array($btn_slug, $pop_posts, $post_type) { $popular_array = array(); foreach ($pop_posts as $pop) { if($post_type === 'comment') { $id = $pop; $label = 'comment'; } else { $id = $pop->ID; $label = 'post'; } $btn_count_args = array('post_id' => $id,'btn_slug' => $btn_slug,'btn_type' => $post_type); $btn_count = get_single_btn_count($btn_count_args); $popular_array[] = array( $label.'_id' => $id, 'btn_count' => $btn_count ); } return $popular_array; } /* * Save most clicked overall slug (all post types, no comments) in wp_options * 'enp_button_popular_'.$btn_slug * Build the field based on the saved popular posts in wp_options */ function enp_all_popular_posts_save($btn_slug, $btn_info) { // loop through each button type $all_popular_posts = array(); foreach($btn_info['btn_type'] as $key=>$value) { // check if the button type is active and is not comments if($value === '1' && $key !== 'comment') { // get the arrays $pop_posts = get_option('enp_button_popular_'.$btn_slug.'_'.$key); if(!empty($pop_posts)) { $all_popular_posts = array_merge($all_popular_posts, $pop_posts); } } } // rearrange the array to be in order of most clicks to least if(!empty($all_popular_posts)) { usort($all_popular_posts, 'enp_sort_popular_post_order'); // enp_sort_popular_post_order is a function, oddly } update_option( 'enp_button_popular_'.$btn_slug, $all_popular_posts ); } // Sorts multidemensional array from high to low by btn_count value function enp_sort_popular_post_order($a, $b) { return ($a['btn_count'] < $b['btn_count']) ? 1 : -1; } ?> __( 'Five Minutes', 'textdomain' ), 'interval' => 300, ); return $schedules; } add_filter( 'cron_schedules', 'enp_custom_cron_job_recurrence' ); // Schedule enp cron jobs function enp_create_build_button_data_cron() { //Use wp_next_scheduled to check if the event is already scheduled $timestamp = wp_next_scheduled( 'enp_build_button_data' ); //If $timestamp == false schedule daily backups since it hasn't been done previously if( $timestamp == false ){ wp_schedule_event( time(), 'fiveminutes', 'enp_build_button_data' ); } } add_action( 'enp_build_button_data', 'enp_build_popular_button_data' ); function enp_build_popular_button_data() { // check to see if anything has changed $rebuild_popular_data = get_option('enp_rebuild_popular_data'); if($rebuild_popular_data !== '1') { return false; // Stop the process. Nothing has changed. } // build the popular button data enp_popular_button_save(); // reset our flag for if we should rebuild the popular data or not update_option('enp_rebuild_popular_data', '0'); } function enp_remove_cron_jobs() { wp_clear_scheduled_hook( 'enp_build_button_data' ); // leave this here until the plugin has been deactived on the enp dev site wp_clear_scheduled_hook( 'enp_send_data' ); } ?> '', 'btn_type'=>'')); // create an empty array for it to squasy bugs $formHTML .= buttonCreateFormHTML($enp_buttons, $registered_content_types); } else { $i = 0; foreach($enp_buttons as $enp_button) { $args['btn_slug'] = $enp_button['btn_slug']; $enp_btn_obj = new Enp_Button($args); $formHTML .= buttonCreateFormHTML($enp_buttons, $registered_content_types, $i, $enp_btn_obj); $i++; } // if we want to add buttons later, we'd add more after this loop // $formHTML .= buttonCreateFormHTML($enp_buttons, $registered_content_types, $i, $enp_btn_obj); } echo $formHTML; } function buttonCreateFormHTML($enp_buttons, $registered_content_types, $i = 0, $enp_btn_obj = false ) { $formHTML = '
    Button
    ' .buttonCreateSlug($enp_buttons, $i, $enp_btn_obj). '
    Where to Use this Button
    '. buttonCreateBtnType($enp_buttons, $i, $registered_content_types) .'

    Where do you want this button to display?
    Show most Clicked posts list.
    '. buttonCreateDisplayPopular($enp_buttons, $i) .'

    Display a list of the top 5 Clicked posts at the bottom of each post, page, and custom post type.
    '; return $formHTML; } function buttonCreateSlug($enp_buttons, $i = 0, $enp_btn_obj) { $buttonSlugHTML = ''; $buttonSlugHTML .= buttonCreateSlugHTML($enp_buttons, $i, $enp_btn_obj); return $buttonSlugHTML; } function buttonCreateDisplayPopular($enp_buttons, $i) { $displayPopularHTML = ''; // set our default value to false $checked_val = false; if(isset($enp_buttons[$i]['display_popular'])) { // set the value $checked_val = $enp_buttons[$i]['display_popular']; } $displayPopularHTML = ' Display Most Clicked Posts List '; return $displayPopularHTML; } function buttonCreateSlugHTML($enp_buttons, $i = 0, $enp_btn_obj) { // if there's no object or there are if($enp_btn_obj === false || $enp_btn_obj->btn_lock === false) { $buttonSlugHTML =' Respect Recommend Important Thoughtful Useful

    Which button do you want to use on your site?

    Have an idea for other button text options? Let us know! [email protected]'; } else { // the button object exists and it's locked, so we can't let people change it // without resetting everything to 0 $buttonSlugHTML = ' '.$enp_btn_obj->get_btn_name() .'

    This button is locked because people have already clicked on it.

    You have to delete it and create a new button to change the button name.
    '; } return $buttonSlugHTML; } function buttonCreateBtnType($enp_buttons, $i, $registered_content_types) { $checklist_html = ''; foreach($registered_content_types as $content_type) { $checklist_html .= buttonCreateBtnTypeHTML($enp_buttons, $i, $content_type); } return $checklist_html; } function buttonCreateBtnTypeHTML($enp_buttons, $i, $content_type) { $checklist_html =''; $name = 'enp_buttons['.$i.'][btn_type]['.$content_type['slug'].']'; // set our default value to false $checked_val = false; // this is absurdly convoluted, but it works... Improvements are welcome if(isset($enp_buttons[$i]['btn_type'][$content_type['slug']])) { // set the value $checked_val = $enp_buttons[$i]['btn_type'][$content_type['slug']]; } $checklist_html .= ' '.$content_type['label_name'].' '; return $checklist_html; } ?>
    Thanks for activating the Engaging Buttons plugin!

    The Engaging News Project created Engaging Buttons after we found that having a "Respect" button in comment sections could have business and democratic benefits.

    Help us improve the Engaging Buttons plugin by allowing your click data to be sent to the Engaging News Project. You will also be helping us continue to provide free, high-quality research.

    No thanks, just take me to the settings page.

    Parse error: syntax error, unexpected '}' in /var/www/html/wp2/wp-content/plugins/engaging-buttons/admin/widgets/popular-button-widget.php on line 129

Viewing 1 replies (of 1 total)