Plugin Directory

Changeset 3009284

Timestamp:
12/13/2023 09:49:44 AM (8 months ago)
Author:
shubhamsedani
Message:
  • Have added PHPCS, Validation, nonce, escaped the variables, user permission.
  • Declared GPL-compatible license.
  • Updated the version.
Location:
add-dummy-post/trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • add-dummy-post/trunk/add-dummy-post.php

    r3002229 r3009284  
    44Plugin URI:     https://wordpress.org/plugins/add-dummy-post/
    55Description:    Add Dummy Post in WordPress
    6 Version:        1.0.1
     6Version:        1.0.
    77Author:         shubhamsedani
     8
     9
     10
    811Text Domain:    adp
     12
     13
     14
     15
    916*/
    1017
    1118// Exit if accessed directly
    12 if ( !defined( 'ABSPATH' ) ) exit;
     19if () exit;
    1320
    14 if( !defined( 'ADP_DIR' ) ) {
    15     define('ADP_DIR', dirname( __FILE__ ) ); // plugin dir
     21if) {
     22    define('ADP_DIR', dirname(); // plugin dir
    1623}
    17 if( !defined( 'ADP_URL' ) ) {
    18     define('ADP_URL', plugin_dir_url( __FILE__ ) ); // plugin url
     24if) {
     25    define('ADP_URL', plugin_dir_url(); // plugin url
    1926}
    20 //get more knowledge on this
    21 if( !defined('ADP_BASENAME') ){
     27if (!defined('ADP_BASENAME')) {
    2228    define('ADP_BASENAME', 'ADP');  // plugin base name
    2329}
    24 if( !defined( 'ADP_ADMIN_DIR' ) ) {
    25     define('ADP_ADMIN_DIR', ADP_DIR . '/backend' ); // plugin admin dir
     30if) {
     31    define('ADP_ADMIN_DIR', ADP_DIR . '/backend'); // plugin admin dir
    2632}
    27 if( !defined( 'ADP_ADMIN_URL' ) ) {
    28     define('ADP_ADMIN_URL', ADP_DIR . 'backend' ); // plugin admin url
     33if) {
     34    define('ADP_ADMIN_URL', ADP_DIR . 'backend'); // plugin admin url
    2935}
    30 if( !defined( 'ADP_FRONT_DIR' ) ) {
    31     define('ADP_FRONT_DIR', ADP_DIR . '/frontend' ); // plugin frontend dir
     36if) {
     37    define('ADP_
    3238}
    33 if( !defined( 'ADP_FRONT_URL' ) ) {
    34     define('ADP_FRONT_URL', ADP_DIR . 'frontend' ); // plugin frontend url
    35 }
    36 if( !defined( 'ADP_SETTINGS_TABLE' ) ) {
    37     define( 'ADP_SETTINGS_TABLE', 'ADP_settings' ); // define the table name - to store seleted options details
    38 }
     39
    3940//include custom function file for backend
    4041include ADP_ADMIN_DIR . '/includes/adp-back-end-custom-functions.php';
    4142
    42 //include custom function file for frontend
    43 include ADP_FRONT_DIR . '/includes/adp-front-end-custom-functions.php';
    44 
    4543//get more knowledge on this
    46 function adp_load_scripts() {
     44function adp_load_scripts()
     45{
    4746    wp_enqueue_style('adp_custom_css', ADP_URL . 'backend/assets/public-style.css');
    4847    wp_enqueue_script('adp_custom_js', ADP_URL . 'backend/assets/public-script.js');
    4948}
    50 add_action( 'admin_init','adp_load_scripts');
     49add_action('adp_load_scripts');
    5150
    5251/**
     
    5554 * Register plugin activation hook.
    5655 */
    57 register_activation_hook( __FILE__, 'adp_install' );
     56register_activation_hook();
    5857
    5958/**
     
    6261 * Register plugin deactivation hook.
    6362 */
    64 register_deactivation_hook( __FILE__, 'adp_deactivate' );
     63register_deactivation_hook();
    6564
    6665/**
     
    6968 * Register plugin deactivation hook.
    7069 */
    71 register_uninstall_hook ( __FILE__, 'adp_uninstall' );
     70register_uninstall_hook);
    7271
    7372/**
     
    7776 * stest default values for the plugin options.
    7877 */
    79 function adp_install() {
     78function adp_install()
     79{
    8080    //create custom table for plugin
    81 
    82     //IMP Call of Function
    83     //Need to call when custom post type is being used in plugin
     81    //Need to call this without fail when custom post type is being used in plugin
    8482    flush_rewrite_rules();
    8583}
     
    9088 * Delete plugin options.
    9189 */
    92 function adp_deactivate() {
     90function adp_deactivate()
     91{
     92    // Delete time process
     93}
    9394
    94 }
    9595/**
    9696 * Plugin Setup (On Uninstall)
     
    9898 * Delete plugin options.
    9999 */
    100 function adp_uninstall() {
     100function adp_uninstall()
     101{
    101102    //drop custom table for plugin
    102103}
    103 
    104 ?>
  • add-dummy-post/trunk/backend/includes/adp-back-end-custom-functions.php

    r2758914 r3009284  
    11<?php
    22// Exit if accessed directly
    3 if ( !defined( 'ABSPATH' ) ) exit;
     3if () exit;
    44
    55// import page settings
    6 function adp_dummy_settings(){
     6function adp_dummy_settings()
     7{
    78    include ADP_ADMIN_DIR . '/methods/adp-import-settings.php';
    89}
     
    1617 * Add Custom admin menu
    1718 */
    18 function adp_add_custom_admin_menu(){
    19     add_management_page(  esc_html__( 'Dummy Post Settings', 'adp' ), esc_html__( 'Add Dummy Post', 'adp' ), 'manage_options', 'adp-post', 'adp_dummy_settings');
     19function adp_add_custom_admin_menu()
     20{
     21    add_management_page(esc_html__('Dummy Post Settings', 'adp'), esc_html__('Add Dummy Post', 'adp'), 'manage_options', 'adp-post', 'adp_dummy_settings');
    2022}
    21 
    22 //Action to call the admin menu function
    23 add_action( 'admin_menu', 'adp_add_custom_admin_menu' );
     23add_action('admin_menu', 'adp_add_custom_admin_menu');
    2424
    2525
    2626//function to check the session
    27 function adp_register_my_session(){
    28     if( ! session_id() ) {
     27function adp_register_my_session()
     28{
     29    if (!session_id()) {
    2930        session_start();
    3031    }
     
    3233
    3334add_action('init', 'adp_register_my_session');
    34 ?>
  • add-dummy-post/trunk/backend/methods/adp-import-functions.php

    r2758914 r3009284  
    11<?php
    22// Exit if accessed directly
    3 if ( !defined( 'ABSPATH' ) ) exit;
     3if () exit;
    44
    5 // Check if user form is submitted and start the import process
    6 if( isset($_POST['adp_submit'] ) && !empty( $_POST['blog_number'] ) ){
     5// check if user is login, if yes then only add new posts
     6function adp_handle_form_submission()
     7{
     8    // Check if user form is submitted and start the import process
     9    if (isset($_POST['adp_submit']) && !empty($_POST['adp_submit']) && !empty($_POST['blog_number']) && isset($_POST['blog_number'])) {
     10        // Verify nonce
     11        if (isset($_POST['adp_nonce']) && wp_verify_nonce($_POST['adp_nonce'], 'adp_nonce_action')) {
     12            // Check the user permission
     13            if (current_user_can('manage_options')) {
     14                if (is_user_logged_in() == true) {
     15                    try {
     16                        $number_of_post = intval($_POST['blog_number']);
     17                        str_replace(" ", "", $number_of_post);
    718
    8     // check if user is login, if yes then only add new posts
    9     function adp_login_function(){
    10         if ( is_user_logged_in() == true ) {
     19                        // generate dummy content and title
     20                        abstract class AdpLorem
     21                        {
     22                            public static function title($nparagraphs)
     23                            {
     24                                $paragraphs = [];
     25                                for ($p = 0; $p < $nparagraphs; ++$p) {
     26                                    $nsentences = 1;
     27                                    $sentences = [];
     28                                    for ($s = 0; $s < $nsentences; ++$s) {
     29                                        $frags = [];
     30                                        $commaChance = .33;
     31                                        while (true) {
     32                                            $nwords = 5;
     33                                            $words = self::randomValues(self::$lorem, $nwords);
     34                                            $frags[] = implode(' ', $words);
     35                                            if (self::randomFloat() >= $commaChance) {
     36                                                break;
     37                                            }
     38                                            $commaChance /= 2;
     39                                        }
     40                                        $sentences[] = ucfirst(implode(', ', $frags)) . '.';
     41                                    }
     42                                    $paragraphs[] = implode(' ', $sentences);
     43                                }
     44                                return implode("\n\n", $paragraphs);
     45                            }
    1146
    12             try {
    13                 $number_of_post = intval($_POST['blog_number']);
    14                 str_replace(" ", "", $number_of_post);
    15        
    16                 // generate dummy content and title
    17                 abstract class adp_lorem {
    18                     public static function title($nparagraphs) {
    19                         $paragraphs = [];
    20                         for($p = 0; $p < $nparagraphs; ++$p) {
    21                             $nsentences = 1;
    22                             $sentences = [];
    23                             for($s = 0; $s < $nsentences; ++$s) {
    24                                 $frags = [];
    25                                 $commaChance = .33;
    26                                 while(true) {
    27                                     $nwords = 5;
    28                                     $words = self::random_values(self::$lorem, $nwords);
    29                                     $frags[] = implode(' ', $words);
    30                                     if(self::random_float() >= $commaChance) {
    31                                         break;
     47           
     48               
     49                ;
     50       
     51               
     52               
     53                    s) {
     54                        s = [];
     55                       
     56                           
     57                            ;
     58                           
     59                                ;
     60                               
     61                               
     62                                   
     63                                    ;
     64                                   
     65
     66                                        ;
    3267                                    }
    33                                     $commaChance /= 2;
     68                                    $;
    3469                                }
    35            
    36                                 $sentences[] = ucfirst(implode(', ', $frags)) . '.';
     70                                return implode("</br></br>", $paragraphs);
    3771                            }
    38                             $paragraphs[] = implode(' ', $sentences);
     72
     73                            private static function randomFloat()
     74                            {
     75                                return random_int(0, PHP_INT_MAX - 1) / PHP_INT_MAX;
     76                            }
     77
     78                            private static function randomValues($arr, $count)
     79                            {
     80                                $keys = array_rand($arr, $count);
     81                                if ($count == 1) {
     82                                    $keys = [$keys];
     83                                }
     84                                return array_intersect_key($arr, array_fill_keys($keys, null));
     85                            }
     86
     87                            private static $lorem = ['lorem', 'ipsum', 'dolor', 'sit', 'amet', 'consectetur', 'adipiscing', 'elit', 'praesent', 'interdum', 'dictum', 'mi', 'non', 'egestas', 'nulla', 'in', 'lacus', 'sed', 'sapien', 'placerat', 'malesuada', 'at', 'erat', 'etiam', 'id', 'velit', 'finibus', 'viverra', 'maecenas', 'mattis', 'volutpat', 'justo', 'vitae', 'vestibulum', 'metus', 'lobortis', 'mauris', 'luctus', 'leo', 'feugiat', 'nibh', 'tincidunt', 'a', 'integer', 'facilisis', 'lacinia', 'ligula', 'ac', 'suspendisse', 'eleifend', 'nunc', 'nec', 'pulvinar', 'quisque', 'ut', 'semper', 'auctor', 'tortor', 'mollis', 'est', 'tempor', 'scelerisque', 'venenatis', 'quis', 'ultrices', 'tellus', 'nisi', 'phasellus', 'aliquam', 'molestie', 'purus', 'convallis', 'cursus', 'ex', 'massa', 'fusce', 'felis', 'fringilla', 'faucibus', 'varius', 'ante', 'primis', 'orci', 'et', 'posuere', 'cubilia', 'curae', 'proin', 'ultricies', 'hendrerit', 'ornare', 'augue', 'pharetra', 'dapibus', 'nullam', 'sollicitudin', 'euismod', 'eget', 'pretium', 'vulputate', 'urna', 'arcu', 'porttitor', 'quam', 'condimentum', 'consequat', 'tempus', 'hac', 'habitasse', 'platea', 'dictumst', 'sagittis', 'gravida', 'eu', 'commodo', 'dui', 'lectus', 'vivamus', 'libero', 'vel', 'maximus', 'pellentesque', 'efficitur', 'class', 'aptent', 'taciti', 'sociosqu', 'ad', 'litora', 'torquent', 'per', 'conubia', 'nostra', 'inceptos', 'himenaeos', 'fermentum', 'turpis', 'donec', 'magna', 'porta', 'enim', 'curabitur', 'odio', 'rhoncus', 'blandit', 'potenti', 'sodales', 'accumsan', 'congue', 'neque', 'duis', 'bibendum', 'laoreet', 'elementum', 'suscipit', 'diam', 'vehicula', 'eros', 'nam', 'imperdiet', 'sem', 'ullamcorper', 'dignissim', 'risus', 'aliquet', 'habitant', 'morbi', 'tristique', 'senectus', 'netus', 'fames', 'nisl', 'iaculis', 'cras', 'aenean'];
    3988                        }
    40                         return implode("\n\n", $paragraphs);
     89
     90                        // add new dummy post to the wordpress posts
     91                        if (is_numeric($number_of_post) && $number_of_post < 51) {
     92                            $blog_data = array();
     93
     94                            for ($i = 0; $i < $number_of_post; $i++) {
     95                                $title = AdpLorem::title(1);
     96                                $para = AdpLorem::para(5);
     97
     98                                if (!empty($title)) {
     99                                    $blog_args['post_title'] = $title;
     100                                }
     101
     102                                if (!empty($para)) {
     103                                    $blog_args['post_content'] = $para;
     104                                    $blog_args['post_status'] = 'publish';
     105                                    $blog_args['post_author'] = '1';
     106                                }
     107
     108                                try {
     109                                    $post_id = wp_insert_post($blog_args);
     110                                } catch (\Throwable $th) {
     111                                    echo esc_html__('There is a problem while inserting the posts', 'adp');
     112                                }
     113                            }
     114                        }
     115                    } catch (\Throwable $th) {
     116                        echo esc_html__('There is a problem in the input of data', 'adp');
    41117                    }
    42            
    43                     public static function para($nparagraphs) {
    44                         $paragraphs = [];
    45                         for($p = 0; $p < $nparagraphs; ++$p) {
    46                             $nsentences = random_int(3, 8);
    47                             $sentences = [];
    48                             for($s = 0; $s < $nsentences; ++$s) {
    49                                 $frags = [];
    50                                 $commaChance = .33;
    51                                 while(true) {
    52                                     $nwords = random_int(3, 15);
    53                                     $words = self::random_values(self::$lorem, $nwords);
    54                                     $frags[] = implode(' ', $words);
    55                                     if(self::random_float() >= $commaChance) {
    56                                         break;
    57                                     }
    58                                     $commaChance /= 2;
    59                                 }
    60            
    61                                 $sentences[] = ucfirst(implode(', ', $frags)) . '.';
    62                             }
    63                             $paragraphs[] = implode(' ', $sentences);
    64                         }
    65                         return implode("</br></br>", $paragraphs);
    66                     }
    67            
    68                     private static function random_float() {
    69                         return random_int(0, PHP_INT_MAX - 1) / PHP_INT_MAX;
    70                     }
    71            
    72                     private static function random_values($arr, $count) {
    73                         $keys = array_rand($arr, $count);
    74                         if($count == 1) {
    75                             $keys = [$keys];
    76                         }
    77                         return array_intersect_key($arr, array_fill_keys($keys, null));
    78                     }
    79            
    80                     private static $lorem = ['lorem', 'ipsum', 'dolor', 'sit', 'amet', 'consectetur', 'adipiscing', 'elit', 'praesent', 'interdum', 'dictum', 'mi', 'non', 'egestas', 'nulla', 'in', 'lacus', 'sed', 'sapien', 'placerat', 'malesuada', 'at', 'erat', 'etiam', 'id', 'velit', 'finibus', 'viverra', 'maecenas', 'mattis', 'volutpat', 'justo', 'vitae', 'vestibulum', 'metus', 'lobortis', 'mauris', 'luctus', 'leo', 'feugiat', 'nibh', 'tincidunt', 'a', 'integer', 'facilisis', 'lacinia', 'ligula', 'ac', 'suspendisse', 'eleifend', 'nunc', 'nec', 'pulvinar', 'quisque', 'ut', 'semper', 'auctor', 'tortor', 'mollis', 'est', 'tempor', 'scelerisque', 'venenatis', 'quis', 'ultrices', 'tellus', 'nisi', 'phasellus', 'aliquam', 'molestie', 'purus', 'convallis', 'cursus', 'ex', 'massa', 'fusce', 'felis', 'fringilla', 'faucibus', 'varius', 'ante', 'primis', 'orci', 'et', 'posuere', 'cubilia', 'curae', 'proin', 'ultricies', 'hendrerit', 'ornare', 'augue', 'pharetra', 'dapibus', 'nullam', 'sollicitudin', 'euismod', 'eget', 'pretium', 'vulputate', 'urna', 'arcu', 'porttitor', 'quam', 'condimentum', 'consequat', 'tempus', 'hac', 'habitasse', 'platea', 'dictumst', 'sagittis', 'gravida', 'eu', 'commodo', 'dui', 'lectus', 'vivamus', 'libero', 'vel', 'maximus', 'pellentesque', 'efficitur', 'class', 'aptent', 'taciti', 'sociosqu', 'ad', 'litora', 'torquent', 'per', 'conubia', 'nostra', 'inceptos', 'himenaeos', 'fermentum', 'turpis', 'donec', 'magna', 'porta', 'enim', 'curabitur', 'odio', 'rhoncus', 'blandit', 'potenti', 'sodales', 'accumsan', 'congue', 'neque', 'duis', 'bibendum', 'laoreet', 'elementum', 'suscipit', 'diam', 'vehicula', 'eros', 'nam', 'imperdiet', 'sem', 'ullamcorper', 'dignissim', 'risus', 'aliquet', 'habitant', 'morbi', 'tristique', 'senectus', 'netus', 'fames', 'nisl', 'iaculis', 'cras', 'aenean'];
    81118                }
    82        
    83                 // add new dummy post to the wordpress posts
    84                 if(is_numeric($number_of_post) && $number_of_post < 51){
    85                     $blog_data = array();
    86        
    87                     for($i=0; $i<$number_of_post; $i++){
    88                         $title = adp_lorem::title(1);
    89                         $para = adp_lorem::para(5);
    90        
    91                         if( !empty( $title ) ){
    92                             $blog_args['post_title'] = $title;
    93                         }
    94            
    95                         if( !empty( $para ) ){
    96                             $blog_args['post_content'] = $para;
    97                             $blog_args['post_status'] = 'publish';
    98                             $blog_args['post_author'] = '1';
    99                         }
    100            
    101                         try {
    102                             $post_id = wp_insert_post( $blog_args );
    103                         } catch (\Throwable $th) {
    104                             _e( 'There is a problem while inserting the posts', 'adp' );
    105                         }
    106                        
    107                     }
    108                 }else{
    109                     // $_SESSION['bigger_number'] = "The number is bigger then 50!";
    110                 }
    111        
    112             } catch (\Throwable $th) {
    113                 _e( 'There is a problem in the input of data', 'adp' );
    114119            }
    115 
    116120        }
    117121    }
    118 add_action('init', 'adp_login_function');
    119 
     122    // Redirect after form submission (change URL to your desired location)
     123    wp_redirect(admin_url('tools.php?page=adp-post'));
     124    exit;
    120125}
    121126
    122 
    123 
    124 
    125 ?>
     127// Hook to handle form submissions
     128add_action('admin_post_adp_handle_form_submission', 'adp_handle_form_submission');
     129add_action('admin_post_nopriv_adp_handle_form_submission', 'adp_handle_form_submission');
  • add-dummy-post/trunk/backend/methods/adp-import-settings.php

    r2758914 r3009284  
    1 <?php 
     1<?php
    22// Exit if accessed directly
    3 if ( !defined( 'ABSPATH' ) ) exit;
     3if () exit;
    44
    55?>
     
    77<div class="adp-display-page">
    88    <div class="adp_customheader">
    9         <h2><?php _e( 'Add Dummy Blog Posts', 'adp' ) ?></h2>
     9        <h2><?php ) ?></h2>
    1010    </div>
    1111
    12     <?php
    13     // if ( array_key_exists( 'bigger_number', $_SESSION ) ) { ?>
    14         <!-- <div class="adp_wrapper">
    15             <div class='adp_userInputForm'>
    16                 <div class="notice notice-error is-dismissible wot_error_spacing">
    17                     <p>
    18                         <?php// echo esc_attr($_SESSION['bigger_number']); ?>
    19                     </p>
    20                 </div>
    21             </div>
    22         </div> -->
    23         <?php// unset($_SESSION['bigger_number']);
    24     // }
    25     ?>
     12    <form method='post' action ='<?php echo esc_url(admin_url('admin-post.php')); ?>'>
     13        <!-- Adding Nonce Field -->
     14        <?php wp_nonce_field('adp_nonce_action', 'adp_nonce'); ?>
    2615
    27     <form method='post' action =''>
    2816        <div class="adp_wrapper">
    2917            <div class='adp_userInputForm'>
    3018                <div class="adp_get_number_of_post">
    31                     <label for="blog_number" class="blog_number_label"><?php _e( 'Enter number of post you want to add:', 'adp' ); ?></label>
    32                     <input type="number" class="blog_number" name="blog_number"  max="50">
     19                    <label for="blog_number" class="blog_number_label">
     20                        <?php echo esc_html__('Enter number of post you want to add:', 'adp'); ?>
     21                    </label>
     22                    <input type="number" class="blog_number" name="blog_number" min="0" max="50">
     23                    <input type="hidden" name="action" value="adp_handle_form_submission">
    3324                </div>
    3425                <div>
  • add-dummy-post/trunk/readme.txt

    r3002478 r3009284  
    44License: GPLv2 or later
    55License URI: http://www.gnu.org/licenses/gpl-2.0.html
    6 Requires at least: 3.3
     6Requires at least:
    77Tested up to: 6.4.1
    8 Stable tag: 1.0.1
    9 Requires PHP: 7.0
     8Stable tag: 1.0.
     9Requires PHP:
    1010
    1111== Description ==
Note: See TracChangeset for help on using the changeset viewer.