Last active
July 30, 2018 02:50
-
-
Save ObeeWanKenobee/0fb9407224553ab59ec74ab4cdf6dd8b to your computer and use it in GitHub Desktop.
Limiter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
defined('ABSPATH') or die(header('Location: https://www.google.com/')); | |
/* Limiter Settings Page */ | |
class Limiter | |
{ | |
global $wpdb; | |
global $limiter = $wpdb->prefix . 'limiter'; | |
public function __construct() | |
{ | |
add_action('admin_menu', array( | |
$this, | |
'wpl_create_settings' | |
)); | |
add_action('admin_init', array( | |
$this, | |
'wpl_setup_sections' | |
)); | |
add_action('admin_init', array( | |
$this, | |
'wpl_setup_fields' | |
)); | |
add_action('init', 'limiter_post_status'); | |
add_action('init', 'create_limiter_cpt', 0); | |
add_action('init', 'create_limiter_tax'); | |
wpl_limiter_db(); | |
} | |
private function wpl_limiter_db() | |
{ | |
$limiter = $wpdb->prefix . "limiter"; | |
$urldefault = '<a href="#">~sample~</a>'; | |
$number = 1000; | |
$idlimit = 100; | |
$urllimit = 64; | |
$default = 0; | |
$charset_collate = $wpdb->get_charset_collate(); | |
$sql = "CREATE TABLE $limiter ( | |
id int(idlimit) NOT NULL AUTO_INCREMENT, | |
timestamp datetime DEFAULT '0000-00-00 00:00:00' NOT NULL, | |
limit smallint($number) DEFAULT '$default' NOT NULL, | |
noofjoins smallint($number) DEFAULT '$default' NOT NULL, | |
forurl varchar($urllimit) DEFAULT '$urldefault' NOT NULL, | |
userids smallint($number) DEFAULT '$default' NOT NULL, | |
instanceid smallint($number) DEFAULT '$default' NOT NULL, | |
PRIMARY KEY (id) | |
) $charset_collate;"; | |
} | |
private function wpl_insert_data() | |
{ | |
$limit = $_POST['limit']; //input from settings page | |
$userids = 5; //get user's id on button click and pass it here | |
$instanceid = 5; //get the instance id and pass it here | |
$_Url = $_POST['url']; //sample | |
$wpdb->update($limiter, array( | |
'timestamp' => current_time('mysql'), | |
'limit' => $limit, | |
'userids' => $userids, | |
'instanceid' => $instanceid, | |
'forurl' => $_Url, | |
)); | |
} | |
public function wpl_limiter_post_status() | |
{ | |
register_post_status('limiter', array( | |
'label' => _x('limiter', 'lmtr'), | |
'label_count' => _n_noop('limiter (%s)', 'limits (%s)', 'lmtr'), | |
'public' => true, | |
'internal' => true, | |
'private' => true, | |
'exclude_from_search' => false, | |
'show_in_admin_all_list' => true, | |
'show_in_admin_status_list' => true, | |
)); | |
} | |
// Register Taxonomy limiter | |
// Taxonomy Key: limiter | |
public function wpl_create_limiter_tax() | |
{ | |
$labels = array( | |
'name' => _x('lmts', 'taxonomy general name', 'lmtr'), | |
'singular_name' => _x('limiter', 'taxonomy singular name', 'lmtr'), | |
'search_items' => __('Search lmts', 'lmtr'), | |
'all_items' => __('All lmts', 'lmtr'), | |
'parent_item' => __('Parent limiter', 'lmtr'), | |
'parent_item_colon' => __('Parent limiter:', 'lmtr'), | |
'edit_item' => __('Edit limiter', 'lmtr'), | |
'update_item' => __('Update limiter', 'lmtr'), | |
'add_new_item' => __('Add New limiter', 'lmtr'), | |
'new_item_name' => __('New limiter Name', 'lmtr'), | |
'menu_name' => __('limiter', 'lmtr'), | |
); | |
$args = array( | |
'labels' => $labels, | |
'description' => __('Just another join function limiter', 'lmtr'), | |
'hierarchical' => true, | |
'public' => true, | |
'publicly_queryable' => true, | |
'show_ui' => true, | |
'show_in_menu' => true, | |
'show_in_nav_menus' => false, | |
'show_in_rest' => false, | |
'show_tagcloud' => true, | |
'show_in_quick_edit' => true, | |
'show_admin_column' => true, | |
); | |
register_taxonomy('limiter', array( | |
'limiting', | |
), $args); | |
} | |
// Register Custom Post Type limiter | |
// Post Type Key: limiter | |
public function wpl_create_limiter_cpt() | |
{ | |
$labels = array( | |
'name' => __('lmtrs', 'Post Type General Name', 'lmtr'), | |
'singular_name' => __('limiter', 'Post Type Singular Name', 'lmtr'), | |
'menu_name' => __('lmtrs', 'lmtr'), | |
'name_admin_bar' => __('limiter', 'lmtr'), | |
'archives' => __('limiter Archives', 'lmtr'), | |
'attributes' => __('limiter Attributes', 'lmtr'), | |
'parent_item_colon' => __('Parent limiter:', 'lmtr'), | |
'all_items' => __('All lmtrs', 'lmtr'), | |
'add_new_item' => __('Add New limiter', 'lmtr'), | |
'add_new' => __('Add New', 'lmtr'), | |
'new_item' => __('New limiter', 'lmtr'), | |
'edit_item' => __('Edit limiter', 'lmtr'), | |
'update_item' => __('Update limiter', 'lmtr'), | |
'view_item' => __('View limiter', 'lmtr'), | |
'view_items' => __('View lmtrs', 'lmtr'), | |
'search_items' => __('Search limiter', 'lmtr'), | |
'not_found' => __('Not found', 'lmtr'), | |
'not_found_in_trash' => __('Not found in Trash', 'lmtr'), | |
'featured_image' => __('Featured Image', 'lmtr'), | |
'set_featured_image' => __('Set featured image', 'lmtr'), | |
'remove_featured_image' => __('Remove featured image', 'lmtr'), | |
'use_featured_image' => __('Use as featured image', 'lmtr'), | |
'insert_into_item' => __('Insert into limiter', 'lmtr'), | |
'uploaded_to_this_item' => __('Uploaded to this limiter', 'lmtr'), | |
'items_list' => __('lmtrs list', 'lmtr'), | |
'items_list_navigation' => __('lmtrs list navigation', 'lmtr'), | |
'filter_items_list' => __('Filter lmtrs list', 'lmtr'), | |
); | |
$args = array( | |
'label' => __('limiter', 'lmtr'), | |
'description' => __('Just another join function limiter', 'lmtr'), | |
'labels' => $labels, | |
'menu_icon' => 'dashicons-admin-generic', | |
'supports' => array( | |
'title', | |
'editor', | |
'thumbnail', | |
'revisions', | |
'author', | |
'comments', | |
'page-attributes', | |
), | |
'taxonomies' => array( | |
'limiter', | |
), | |
'public' => true, | |
'show_ui' => true, | |
'show_in_menu' => false, | |
'menu_position' => 5, | |
'show_in_admin_bar' => false, | |
'show_in_nav_menus' => false, | |
'can_export' => false, | |
'has_archive' => true, | |
'hierarchical' => true, | |
'exclude_from_search' => false, | |
'show_in_rest' => false, | |
'publicly_queryable' => true, | |
'capability_type' => 'post', | |
); | |
register_post_type('limiter', $args); | |
} | |
public function wpl_settings_content() | |
{ | |
echo '<div class="wrap"><h1>Limiter</h1>'; | |
printf(settings_errors()); | |
echo '<form action="" method="POST">'; | |
printf(settings_fields('limiter'), do_settings_sections('limiter'), submit_button()); | |
echo '</form></div>'; | |
} | |
public function wpl_create_settings() | |
{ | |
$page_title = 'Button Limiter'; | |
$menu_title = 'Limiter'; | |
$capability = 'manage_links'; | |
$slug = 'limiter'; | |
$callback = array( | |
$this, | |
'wpl_settings_content' | |
); | |
add_management_page($page_title, $menu_title, $capability, $slug, $callback); | |
} | |
public function wpl_setup_sections() | |
{ | |
add_settings_section('limiter_section', 'Just another join function limiter', array(), 'limiter'); | |
} | |
public function wpl_setup_fields() | |
{ | |
$fields = array( | |
array( | |
'label' => 'Join button limiter', | |
'lmtrin' => 'lmtrinput', | |
'id' => '#limiter', | |
'type' => 'number', | |
'section' => 'limiter_section', | |
'desc' => 'This will limit the number of people who can click the button. If there are already 5 for example of people have joined, since the limit is 5, no one can now join and the button is disabled.', | |
'placeholder' => '1-10000', | |
), | |
); | |
foreach ($fields as $field) | |
{ | |
add_settings_field($field['id'], $field['label'], array( | |
$this, | |
'wpl_field_callback' | |
), 'limiter', $field['section'], $field); | |
register_setting('limiter', $field['id']); | |
} | |
} | |
public function wpl_field_callback($field) | |
{ | |
$value = get_option($field['id']); | |
switch ($field['type']) | |
{ | |
default: | |
printf('<input name="%1$s" id="%2$s" type="%3$s" placeholder="%4$s" value="%5$s" />', $field['lmtrin'], $field['id'], $field['type'], $field['placeholder'], $value); | |
} | |
if ($desc = $field['desc']) | |
{ | |
printf('<p class="description">%s </p>', $desc); | |
} | |
if(isset($_POST['lmtrin'])) | |
{ | |
$lmtronset = $_POST['lmtrin']; | |
//$wpdb->query($wpdb->prepare( //maybe? | |
$wpdb->update($limiter, array( | |
"UPDATE $wpdb->prefix . 'limiter' | |
SET limit = $lmtronset" | |
)); | |
} | |
} | |
} | |
//idunno if this works | |
new Limiter; | |
$source = $wpdb->prefix . "limiter"; | |
$joins = $wpdb->get_results("SELECT noofjoins FROM $source"); | |
$limit_r = $wpdb->get_results("SELECT limit FROM $source"); | |
echo '<p>' . $joins . '/' . $limit_r . '</p>'; | |
if (!empty($joins) || $joins < $limit_r) | |
{ | |
//keep the button enabled | |
echo '<a href="">Join!</a>'; | |
} | |
else if ($joins === $limit_r) | |
{ | |
//add disable property to the button and remove the link [href] | |
echo '<a href="" disabled>Join!</a>'; | |
} | |
//todo get post data $instanceId and $userids and $url using get_post() | |
//todo experiment and connect the dots |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment