Plugin Directory

Changeset 3362814


Ignore:
Timestamp:
09/16/2025 11:01:54 PM (5 months ago)
Author:
scriptsworks
Message:

Actualización a versión 1.1.0: compatibilidad WP 6.8.2, mejoras de seguridad y soporte PHP 8+

Location:
mwr-hit-counter/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • mwr-hit-counter/trunk/README.txt

    r2115135 r3362814  
    1 === MWR HitCounter ===
     1=== MWR Hit Counter ===
     2Contributors: scriptsworks
     3Author: Daniel Martín Ochoa
    24Author URI: https://miwebrentable.com
    3 Author: Daniel Martín Ochoa
    4 Contributors: scriptsworks
     5Donate link: https://www.paypal.me/miwebrentable
    56Tags: hit counter, page counter, page visit, wordpress page view, page view count, post view count, easy counter, counter, single counter
    6 Donate link: https://www.paypal.me/miwebrentable
    7 Requires at least: 4.4.14
    8 Tested up to: 5.2.2
    9 Requires PHP: 7.0
    10 Stable tag: 1.0.0
     7Requires at least: 5.0
     8Tested up to: 6.8.2
     9Requires PHP: 7.4
     10Stable tag: 1.1.0
    1111License: GPLv2 or later
    12 License URI: http://www.gnu.org/licenses/gpl-2.0.html
     12License URI: https://www.gnu.org/licenses/gpl-2.0.html
    1313
    1414== Description ==
    1515
    16 - This plugin allows you to add a visitor 'text-plain' counter on your website easily.
    17 - No IP tracking control, this plugin use cookie tracking.
     16MWR Hit Counter is a simple and lightweight text-based counter for your website. 
     17It uses a single database table and a cookie to count unique visitors over 30 days. 
     18No IP tracking, no bloated features — just a clean counter you can display anywhere.
    1819
    1920== Installation ==
     21
    20221. Upload the plugin files to the `/wp-content/plugins/mwr-hit-counter` directory, or install the plugin through the WordPress plugins screen directly.
    21232. Activate the plugin through the 'Plugins' screen in WordPress.
    22 3. Open your theme and look for the eg. : "footer.php" where will to insert the counter.
    23 Where "start=500" will begin counting, this values can be from 0 to n positives values.
    24 4. Or use the "shortcode" [mwrcounter start=500] into entries or pages.
     243. Add the shortcode where you want the counter to appear.
    2525
    26 = Template code is =
     26= Examples =
    2727
    28 <code><?php echo do_shortcode( '[mwrcounter start=500]' ); ?></code>
     28Template code (in PHP):
     29<code>
     30<?php echo do_shortcode( '[mwrcounter start=500]' ); ?>
     31</code>
    2932
    30 = Post code is =
    31 
    32 <code>[mwrcounter start=500]</code>
     33Post/Page code:
     34<code>
     35[mwrcounter start=500]
     36</code>
    3337
    3438== Frequently Asked Questions ==
    35 = In which WordPress version this Plugin is compatible? =
    3639
    37 It is compatible from 4.4.14 to 5.2.2 WordPress version.
     40= Which WordPress versions are supported? =
     41Tested with WordPress 6.8.2 and should work with any version from 5.0+.
    3842
    39 = When I refresh o revisit my web the counter doesn't nothing. =
     43= Why does the counter not increase on page refresh? =
     44This plugin uses a cookie (valid for 30 days) to prevent counting the same user repeatedly.
    4045
    41 It's normal because this counter do use the cookie tracking for doesn't recount the visit.
    42 
    43 = Cookie expiration time? =
    44 
    45 30 days left.
     46= Can I change the starting value? =
     47Yes, use the `start` attribute in the shortcode: `[mwrcounter start=100]`.
    4648
    4749== Screenshots ==
    48 1. The screenshot corresponds to shortcode insert screenshot-1.png
     50
     511. Shortcode usage example (screenshot-1.png)
    4952
    5053== Changelog ==
     54
     55= 1.1.0 =
     56* Updated for compatibility with WordPress 6.8.2
     57* Improved database table creation using `dbDelta()` with primary key
     58* Added safety checks for PHP 8+
     59* Secure cookie parameters for better compatibility
     60* Code cleanup and better escaping for database operations
     61
    5162= 0.1 =
    52 * Initial release.
     63* Initial release
    5364
    5465== Upgrade Notice ==
    55 = 0.1 =
    56 Added plugin support
    5766
     67= 1.1.0 =
     68This update improves security, fixes compatibility with the latest WordPress and PHP versions, and ensures reliable counting.
     69
  • mwr-hit-counter/trunk/index.php

    r2113762 r3362814  
    22/**
    33 * Plugin Name: MWR Hit Counter
    4  * Description: Simple hit counter for your website.
    5  * Version: 1.0.0
     4 * Description: Simple hit counter for your website with shortcode [mwrcounter start="0"].
     5 * Version: 1.1.0
    66 * Author: Daniel M. Ochoa
    77 * Author URI: https://miwebrentable.com
     8 * Requires at least: 5.0
     9 * Tested up to: 6.8.2
     10 * Requires PHP: 7.4
    811 */
    9  
    1012
    1113/**
    12  * If this file is called directly, abort.
     14 * Exit if accessed directly.
    1315 */
    14  
    15 if ( ! defined( 'WPINC' ) ) {
    16     die;
     16if ( ! defined( 'ABSPATH' ) ) {
     17    exit;
    1718}
    1819
    19 require_once ( ABSPATH . 'wp-admin/includes/upgrade.php' );
    20 
    21 function mwr_counter( $content ) {
    22 
    23                 global $wpdb;
    24                 $p = shortcode_atts( array (
    25                                         'start' => '0'
    26                                             ), $content );
    27    
    28                 $tableName = $wpdb->prefix . "mwr_counter";
    29                 $myrows = $wpdb->get_results ( "SELECT ID, counter FROM $tableName WHERE ID='0'" );
    30                
    31                 if ( !isset ( $_COOKIE['mwrcounter'] ) )
    32                         {
    33                                 $count = $myrows[0]->counter + 1;
    34                                 $wpdb->update ( $tableName, array (
    35                                                 'counter' => $count
    36                                 ), array (
    37                                                 'ID' => 0
    38                                 ) );
    39                                
    40                         }
    41                 else
    42                         {
    43                                 $count = $myrows[0]->counter;
    44                         }
    45                        
    46                 return $content = $count+$p['start'];
    47         }
    48 /**
    49  * start counter shortcode.
    50  */
    51  
    52 add_shortcode( 'mwrcounter', 'mwr_counter' );
    53 
    54 function setting_mwr_counter_cookie() {
    55                 setcookie( 'mwrcounter', '1', time() + ( 86400 * 30 ), "/" );
    56         }
     20require_once ABSPATH . 'wp-admin/includes/upgrade.php';
    5721
    5822/**
    59  * set cookie first time to 30 days.
     23 * Shortcode to display hit counter.
     24 *
     25 * Usage: [mwrcounter start="100"]
    6026 */
    61  
    62 add_action( 'init', 'setting_mwr_counter_cookie' );
     27function mwr_counter_shortcode( $atts ) {
     28    global $wpdb;
    6329
    64 function delete_mwr_counter_table() {
     30    $atts = shortcode_atts( array(
     31        'start' => 0,
     32    ), $atts, 'mwrcounter' );
    6533
    66                 global $wpdb;
    67                 $tableName = $wpdb->prefix . "mwr_counter";
    68                 $sql = "DROP TABLE IF EXISTS $tableName";
    69                 $wpdb->query($sql);
     34    $table_name = $wpdb->prefix . 'mwr_counter';
     35    $myrows = $wpdb->get_results( "SELECT counter FROM $table_name WHERE ID = 0" );
    7036
    71         }
     37    // Si la tabla está vacía, inicializamos manualmente
     38    $count = isset( $myrows[0]->counter ) ? intval( $myrows[0]->counter ) : 0;
    7239
    73 function create_mwr_counter_table() {
     40    if ( ! isset( $_COOKIE['mwrcounter'] ) ) {
     41        $count++;
     42        $wpdb->update(
     43            $table_name,
     44            array( 'counter' => $count ),
     45            array( 'ID' => 0 ),
     46            array( '%d' ),
     47            array( '%d' )
     48        );
     49    }
    7450
    75                 global $wpdb;
    76                 $tableName = $wpdb->prefix . "mwr_counter";
    77                
    78                 $created = dbDelta ( "CREATE TABLE IF NOT EXISTS $tableName (
    79                             `ID` bigint(20) NOT NULL DEFAULT '0',
    80                             `counter` int(9) NOT NULL DEFAULT '0')" );
    81                
    82                 $wpdb->insert ( $tableName, array(
    83                                 'ID' => '0',
    84                                 'counter' => '0'
    85                 ));
    86                
    87         }
    88        
     51    return $count + intval( $atts['start'] );
     52}
     53add_shortcode( 'mwrcounter', 'mwr_counter_shortcode' );
     54
    8955/**
    90  * activate and deactivate hook.
     56 * Set a cookie to avoid recounting within 30 days.
    9157 */
    92  
    93 register_activation_hook( __FILE__, 'create_mwr_counter_table' );
    94 register_deactivation_hook( __FILE__, 'delete_mwr_counter_table' );
     58function mwr_set_counter_cookie() {
     59    if ( ! isset( $_COOKIE['mwrcounter'] ) ) {
     60        setcookie( 'mwrcounter', '1', time() + ( 86400 * 30 ), COOKIEPATH, COOKIE_DOMAIN, is_ssl(), true );
     61    }
     62}
     63add_action( 'init', 'mwr_set_counter_cookie' );
    9564
     65/**
     66 * Create database table on plugin activation.
     67 */
     68function mwr_create_counter_table() {
     69    global $wpdb;
     70    $table_name = $wpdb->prefix . 'mwr_counter';
    9671
     72    $charset_collate = $wpdb->get_charset_collate();
     73
     74    $sql = "CREATE TABLE $table_name (
     75        ID bigint(20) NOT NULL DEFAULT 0,
     76        counter int(11) NOT NULL DEFAULT 0,
     77        PRIMARY KEY  (ID)
     78    ) $charset_collate;";
     79
     80    dbDelta( $sql );
     81
     82    // Insert row if not exists
     83    $exists = $wpdb->get_var( "SELECT COUNT(*) FROM $table_name WHERE ID = 0" );
     84    if ( ! $exists ) {
     85        $wpdb->insert( $table_name, array(
     86            'ID'      => 0,
     87            'counter' => 0
     88        ), array( '%d', '%d' ) );
     89    }
     90}
     91register_activation_hook( __FILE__, 'mwr_create_counter_table' );
     92
     93/**
     94 * Drop table on plugin deactivation (optional).
     95 */
     96function mwr_delete_counter_table() {
     97    global $wpdb;
     98    $table_name = $wpdb->prefix . 'mwr_counter';
     99    $wpdb->query( "DROP TABLE IF EXISTS $table_name" );
     100}
     101register_deactivation_hook( __FILE__, 'mwr_delete_counter_table' );
Note: See TracChangeset for help on using the changeset viewer.