Files
wp-jquery-datatable/wp-jquery-datatable.php
T

316 lines
14 KiB
PHP

<?php
/**
* Plugin Name: WP jQuery DataTable
* Description: Features can be set to meet your exact needs for your table implementations. like paging, ordering, searching, etc... Originally developped by biztechc (https://www.appjetty.com/).
* Author: mbanl
* Author URI: https://batssoft.nl/
* Version: 4.2.0
* WordPress Tested up to: 7.0.2
*/
if (!defined('ABSPATH')) {
die('Invalid request.');
}
register_activation_hook(__FILE__, 'wp_jdt_activate_plugin');
function wp_jdt_activate_plugin()
{
$fields = array(
'wp_jdt_info',
'wp_jdt_paging',
'wp_jdt_b_length_change',
'wp_jdt_ordering',
'wp_jdt_searching',
);
foreach ($fields as $field) {
if (get_option($field) === false) {
update_option($field, 'true');
}
}
if (get_option('wp_jdt_paging_type') === false) update_option('wp_jdt_paging_type', 'simple');
if (get_option('wp_jdt_page_length') === false) update_option('wp_jdt_page_length', '10');
if (get_option('wp_jdt_order_row') === false) update_option('wp_jdt_order_row', '0');
if (get_option('wp_jdt_order_row_sort') === false) update_option('wp_jdt_order_row_sort', 'desc');
}
add_action('admin_menu', 'wp_jdt_create_menu');
function wp_jdt_create_menu()
{
//create admin side menu
add_options_page(__('WP jQuery DataTable Settings', 'wp-jquery-datatable'), __('WP jQuery DataTable', 'wp-jquery-datatable'), 'manage_options', 'wp-jdt', 'wp_jdt_settings_page');
//call register settings function
add_action('admin_init', 'wp_jdt_settings');
}
function wp_jdt_settings()
{
//register our settings
$settings = array(
'wp_jdt_info',
'wp_jdt_paging',
'wp_jdt_page_length',
'wp_jdt_paging_type',
'wp_jdt_b_length_change',
'wp_jdt_ordering',
'wp_jdt_order_row',
'wp_jdt_order_row_sort',
'wp_jdt_searching'
);
foreach ($settings as $setting) {
register_setting('wp-jdt-settings-group', $setting);
}
}
function wp_jdt_settings_page()
{
// Admin side page options
if (!current_user_can('manage_options')) {
return;
}
$wp_jdt_info = get_option('wp_jdt_info', 'true');
$wp_jdt_paging = get_option('wp_jdt_paging', 'true');
$wp_jdt_page_length = get_option('wp_jdt_page_length', '10');
$wp_jdt_paging_type = get_option('wp_jdt_paging_type', 'simple');
$wp_jdt_b_length_change = get_option('wp_jdt_b_length_change', 'true');
$wp_jdt_ordering = get_option('wp_jdt_ordering', 'true');
$wp_jdt_order_row = get_option('wp_jdt_order_row', '0');
$wp_jdt_order_row_sort = get_option('wp_jdt_order_row_sort', 'asc');
$wp_jdt_searching = get_option('wp_jdt_searching', 'true');
?>
<div class='wrap'>
<h2><?php _e('WP jQuery DataTable Settings', 'wp-jquery-datatable'); ?></h2>
<form method='post' action='options.php'>
<?php settings_fields('wp-jdt-settings-group'); ?>
<?php do_settings_sections('wp-jdt-settings-group'); ?>
<table class='form-table'>
<tr valign='top'>
<th scope='row'><?php _e('Show Information', 'wp-jquery-datatable'); ?></th>
<td>
<fieldset>
<label title="<?php _e('Yes'); ?>"><input type="radio" <?php checked($wp_jdt_info, 'true'); ?> value="true" name="wp_jdt_info"> <span><?php _e('Yes'); ?></span></label><br>
<label title="<?php _e('No'); ?>"><input type="radio" <?php checked($wp_jdt_info, 'false'); ?> value="false" name="wp_jdt_info"> <span><?php _e('No'); ?></span></label>
</fieldset>
</td>
</tr>
<tr valign='top'>
<th scope='row'><?php _e('Pagination'); ?></th>
<td>
<fieldset>
<label title="<?php _e('Yes'); ?>"><input type="radio" <?php checked($wp_jdt_paging, 'true'); ?> value="true" name="wp_jdt_paging"> <span><?php _e('Yes'); ?></span></label><br>
<label title="<?php _e('No'); ?>"><input type="radio" <?php checked($wp_jdt_paging, 'false'); ?> value="false" name="wp_jdt_paging"> <span><?php _e('No'); ?></span></label>
</fieldset>
</td>
</tr>
<tr valign='top'>
<th scope='row'><?php _e('Pagination Type', 'wp-jquery-datatable'); ?></th>
<td>
<fieldset>
<label title="<?php _e('Simple', 'wp-jquery-datatable'); ?>"><input type="radio" <?php checked($wp_jdt_paging_type, 'simple'); ?> value="simple" name="wp_jdt_paging_type"> <span><?php _e('Simple', 'wp-jquery-datatable'); ?></span></label><br>
<label title="<?php _e('Simple Numbers', 'wp-jquery-datatable'); ?>"><input type="radio" <?php checked($wp_jdt_paging_type, 'simple_numbers'); ?> value="simple_numbers" name="wp_jdt_paging_type"> <span><?php _e('Simple Numbers', 'wp-jquery-datatable'); ?></span></label><br>
<label title="<?php _e('Full', 'wp-jquery-datatable'); ?>"><input type="radio" <?php checked($wp_jdt_paging_type, 'full'); ?> value="full" name="wp_jdt_paging_type"> <span><?php _e('Full', 'wp-jquery-datatable'); ?></span></label><br>
<label title="<?php _e('Full Numbers', 'wp-jquery-datatable'); ?>"><input type="radio" <?php checked($wp_jdt_paging_type, 'full_numbers'); ?> value="full_numbers" name="wp_jdt_paging_type"> <span><?php _e('Full Numbers', 'wp-jquery-datatable'); ?></span></label>
</fieldset>
</td>
</tr>
<tr valign='top'>
<th scope='row'><?php _e('Rows Per Page', 'wp-jquery-datatable'); ?></th>
<td>
<input type="number" class="small-text" value="<?php echo $wp_jdt_page_length; ?>" min="1" step="1" name="wp_jdt_page_length">
</td>
</tr>
<tr valign='top'>
<th scope='row'><?php _e('Show Per Page DropDown', 'wp-jquery-datatable'); ?></th>
<td>
<fieldset>
<label title="<?php _e('Yes'); ?>"><input type="radio" <?php checked($wp_jdt_b_length_change, 'true'); ?> value="true" name="wp_jdt_b_length_change"> <span><?php _e('Yes'); ?></span></label><br>
<label title="<?php _e('No'); ?>"><input type="radio" <?php checked($wp_jdt_b_length_change, 'false'); ?> value="false" name="wp_jdt_b_length_change"> <span><?php _e('No'); ?></span></label>
</fieldset>
</td>
</tr>
<tr valign='top'>
<th scope='row'><?php _e('Ordering', 'wp-jquery-datatable'); ?></th>
<td>
<fieldset>
<label title="<?php _e('Yes'); ?>"><input type="radio" <?php checked($wp_jdt_ordering, 'true'); ?> value="true" name="wp_jdt_ordering"> <span><?php _e('Yes'); ?></span></label><br>
<label title="<?php _e('No'); ?>"><input type="radio" <?php checked($wp_jdt_ordering, 'false'); ?> value="false" name="wp_jdt_ordering"> <span><?php _e('No'); ?></span></label>
</fieldset>
</td>
</tr>
<tr valign='top'>
<th scope='row'><?php _e('Specific Column Order', 'wp-jquery-datatable'); ?></th>
<td>
<input type="number" class="small-text" value="<?php echo $wp_jdt_order_row; ?>" min="0" step="1" name="wp_jdt_order_row"><br>
<br>
<fieldset>
<label title="<?php _e('Ascending'); ?>"><input type="radio" <?php checked($wp_jdt_order_row_sort, 'asc'); ?> value="asc" name="wp_jdt_order_row_sort"> <span><?php _e('Ascending'); ?></span></label><br>
<label title="<?php _e('Descending'); ?>"><input type="radio" <?php checked($wp_jdt_order_row_sort, 'desc'); ?> value="desc" name="wp_jdt_order_row_sort"> <span><?php _e('Descending'); ?></span></label>
</fieldset>
</td>
</tr>
<tr valign='top'>
<th scope='row'><?php _e('Searching', 'wp-jquery-datatable'); ?></th>
<td>
<fieldset>
<label title="<?php _e('Yes'); ?>"><input type="radio" <?php checked($wp_jdt_searching, 'true'); ?> value="true" name="wp_jdt_searching"> <span><?php _e('Yes'); ?></span></label><br>
<label title="<?php _e('No'); ?>"><input type="radio" <?php checked($wp_jdt_searching, 'false'); ?> value="false" name="wp_jdt_searching"> <span><?php _e('No'); ?></span></label>
</fieldset>
</td>
</tr>
</table>
<?php submit_button(); ?>
</form>
</div>
<?php
}
add_action('wp_enqueue_scripts', 'wp_jdt_style_and_script'); // add custom style and script
function wp_jdt_style_and_script()
{
// css
wp_register_style('jdt-style-data-tables', plugins_url('css/dataTables.dataTables.css', __FILE__), array(), '2.3.8');
// js
wp_enqueue_script('jdt-js-datatables', plugins_url('js/dataTables.js', __FILE__), array('jquery'), '2.3.8');
}
add_action('plugins_loaded', 'wp_jdt_load_textdomain');
function wp_jdt_load_textdomain()
{
load_plugin_textdomain('wp-jquery-datatable', false, dirname(plugin_basename(__FILE__)) . '/languages');
}
add_shortcode('wp_jdt', 'wp_jdt_shortcode');
function wp_jdt_shortcode($atts)
{
// Get defaults from options for when not set in shortcode
$defaults = array(
'id' => 'example',
'info' => get_option('wp_jdt_info', 'true'),
'paging' => get_option('wp_jdt_paging', 'true'),
'page_length' => get_option('wp_jdt_page_length', '10'),
'paging_type' => get_option('wp_jdt_paging_type', 'simple'),
'b_length_change' => get_option('wp_jdt_b_length_change', 'true'),
'ordering' => get_option('wp_jdt_ordering', 'true'),
'order_row_number' => get_option('wp_jdt_order_row', '0'),
'order_row_number_sort' => get_option('wp_jdt_order_row_sort', 'desc'),
'searching' => get_option('wp_jdt_searching', 'true'),
);
// Combines user attributes with known attributes and fill in defaults when needed
$atts = shortcode_atts($defaults, $atts, 'wp_jdt');
// Sanitize and fall back to defaults when setting from shortcode is unknown
$table_id = preg_replace('/[^a-zA-Z0-9_\-]/', '', $atts['id']);
$table_info = ($atts['info'] === 'true') ? 'true' : 'false';
$table_paging = ($atts['paging'] === 'true') ? 'true' : 'false';
$table_page_length = absint($atts['page_length']);
$allowed_table_paging_types = array('simple', 'simple_numbers', 'full', 'full_numbers');
$table_paging_type = in_array($atts['paging_type'], $allowed_table_paging_types, true) ? $atts['paging_type'] : 'simple';
$table_b_length_change = ($atts['b_length_change'] === 'true') ? 'true' : 'false';
$table_ordering = ($atts['ordering'] === 'true') ? 'true' : 'false';
$table_order_row_number = absint($atts['order_row_number']);
$table_order_sort = (strtolower($atts['order_row_number_sort']) === 'asc') ? 'asc' : 'desc';
$table_searching = ($atts['searching'] === 'true') ? 'true' : 'false';
$table_length_menu = array(10, 25, 50, 100, -1);
if (!in_array($table_page_length, $table_length_menu)) {
// Add the missing page length option to the array
$table_length_menu[] = (int)$table_page_length;
// Temporarily remove -1 ("All") if present to preserve its position at the end
$has_all = false;
if (($key = array_search(-1, $table_length_menu)) !== false) {
unset($table_length_menu[$key]);
$has_all = true;
}
// Sort the remaining numeric options in ascending order
sort($table_length_menu);
// Append -1 back to the end of the array
if ($has_all) {
$table_length_menu[] = -1;
}
}
// css and js
wp_enqueue_style('jdt-style-data-tables');
wp_enqueue_script('jdt-js-datatables');
// Load JS script via wp_footer anonymous function
add_action('wp_footer', function () use (
$table_id,
$table_info,
$table_paging,
$table_page_length,
$table_length_menu,
$table_paging_type,
$table_b_length_change,
$table_ordering,
$table_order_row_number,
$table_order_sort,
$table_searching
) {
?>
<script type="text/javascript">
jQuery(document).ready(function($) {
$('#<?php echo esc_js($table_id); ?>').DataTable({
'info': <?php echo $table_info; ?>,
'paging': <?php echo $table_paging; ?>,
'pageLength': <?php echo $table_page_length; ?>,
'lengthMenu': <?php echo json_encode($table_length_menu); ?>,
'pagingType': '<?php echo esc_js($table_paging_type); ?>',
'bLengthChange': <?php echo $table_b_length_change; ?>,
'ordering': <?php echo $table_ordering; ?>,
'order': [<?php echo $table_order_row_number; ?>, '<?php echo esc_js($table_order_sort); ?>'],
'searching': <?php echo $table_searching; ?>,
'language': {
'search': '<?php echo esc_js(esc_html__('Searching', 'wp-jquery-datatable')); ?>:'
}
});
});
</script>
<?php
});
// return empty string
return '';
}
register_uninstall_hook(__FILE__, 'wp_jdt_uninstall'); // uninstall plug-in
function wp_jdt_uninstall()
{
$settings = array(
'wp_jdt_info',
'wp_jdt_paging',
'wp_jdt_page_length',
'wp_jdt_paging_type',
'wp_jdt_b_length_change',
'wp_jdt_ordering',
'wp_jdt_order_row',
'wp_jdt_order_row_sort',
'wp_jdt_searching'
);
foreach ($settings as $setting) {
delete_option($setting);
}
}