Plugin Directory

Changeset 3228575


Ignore:
Timestamp:
01/25/2025 08:10:01 PM (13 months ago)
Author:
tidaweb
Message:
  • Bugs fixes and improvements
  • Added new APIs
  • Added Button to custom post types for taking the screenshot from it
  • Settings new UI design and improvement
Location:
tida-url-screenshot
Files:
25 added
3 deleted
8 edited

Legend:

Unmodified
Added
Removed
  • tida-url-screenshot/trunk/assets/css/tida-screenshot.css

    r2758772 r3228575  
    77
    88/* styles */
    9 .tida_screenshot_form {
     9.tida-screenshot-form {
    1010    margin: 0 auto;
    1111    text-align: center;
    1212}
    1313
    14 .screenshot_msg, .screenshot_result {
     14.screenshot-msg, .screenshot-result {
    1515    margin-top: 20px;
    1616}
    1717
    18 .screenshot_msg {
     18.screenshot-msg {
    1919    text-align: center;
    2020}
    2121
    22 .text_blink {
     22.text-blink {
    2323    animation: blinker 1s linear infinite;
    2424    text-align: center;
  • tida-url-screenshot/trunk/assets/js/tida-screenshot.js

    r2758772 r3228575  
    11jQuery(document).ready(function(){
    2     jQuery(".tida_screenshot_form").on('submit', function(e){
     2    jQuery(".tida-screenshot-form").on('submit', function(e){
    33        e.preventDefault();
    44        var url = jQuery(this).find('input[type="text"]').val();
     
    66            method: 'POST',
    77            url: tida_screenshot_params.ajax_url,
    8             data: { action : 'get_tida_screenshot', url : url },
     8            data: { action : 'get_tida_url_screenshot', url : url },
    99            dataType: 'json',
    1010            beforeSend: function() {
    11                 jQuery('.screenshot_result img').attr('src', '');
    12                 jQuery('.screenshot_msg').html('<span class="text_blink">' + tida_screenshot_params.please_text + '</span>');
     11                jQuery('.screenshot-result img').attr('src', '');
     12                jQuery('.screenshot-msg').html('<span class="text-blink">' + tida_screenshot_params.please_text + '</span>');
    1313            },
    1414            success: function(response) {
    15                 jQuery('.screenshot_result img').attr('src', response.src);
    16                 jQuery('.screenshot_msg').html(response.msg);
     15                jQuery('.screenshot-msg').html(response.msg);
     16                if( response.status )
     17                    jQuery('.screenshot-result img').attr('src', response.src);
     18            },
     19            error: function(error) {
     20                console.log(error);
     21            }
     22        });
     23    });
     24   
     25    jQuery(".tida-post-screenshot").on('click', function(e){
     26        e.preventDefault();
     27        var pid = jQuery(this).data('pid');
     28        console.log(pid);
     29        jQuery.ajax({
     30            method: 'POST',
     31            url: tida_screenshot_params.ajax_url,
     32            data: { action : 'get_tida_post_screenshot', pid : pid },
     33            dataType: 'json',
     34            beforeSend: function() {
     35                jQuery('.screenshot-result img').attr('src', '');
     36                jQuery('.screenshot-msg').html('<span class="text-blink">' + tida_screenshot_params.please_text + '</span>');
     37            },
     38            success: function(response) {
     39                jQuery('.screenshot-msg').html(response.msg);
     40                if( response.status )
     41                    jQuery('.screenshot-result img').attr('src', response.src);
    1742            },
    1843            error: function(error) {
  • tida-url-screenshot/trunk/includes/admin/class-settings.php

    r3214556 r3228575  
    11<?php
    2 
    3 if ( ! defined( 'ABSPATH' ) ) exit;
     2/**
     3 * Settings Class
     4 * The class responsible for managing the settings in WordPress.
     5 *
     6 * Package: Tida URL Screenshot
     7 * Author: Tidaweb
     8 * Last Modified Time: 2025/01/25 01:40:42
     9 */
     10
     11if (!defined('ABSPATH')) {
     12    exit;
     13} // Exit if accessed directly
    414
    515class Tida_URL_Screenshot_Settings {
    6 
    7     /**
    8      * Prefix for plugin settings.
    9      *
    10      * @var     string
    11      * @access  public
    12      * @since   1.0.0
    13      */
    14     public $base = '';
    15 
    16     /**
    17      * Available settings for plugin.
    18      *
    19      * @var     array
    20      * @access  public
    21      * @since   1.0.0
    22      */
    23     public $settings = array();
    24 
    25     public function __construct() {
    26         //$this->file = $file;
    27         //$this->dir = dirname( $this->file );
    28         $this->base = 'tida_screenshot_';
    29 
    30         // Initialise settings
    31         add_action( 'admin_init', array( &$this, 'init' ) );
    32 
    33         // Register plugin settings
    34         add_action( 'admin_init' , array( &$this, 'register_settings' ) );
    35 
    36         // Add settings page to menu
    37         add_action( 'admin_menu' , array( &$this, 'add_menu_item' ) );
    38 
    39         // admin footer
    40         add_action('admin_head', array(&$this, 'admin_header_scripts'));
    41 
    42         // Add settings link to plugins page
    43         add_filter( 'plugin_action_links_' . plugin_basename( TIDA_URL_SCREENSHOT_PLUGIN_DIR . 'tida-url-screenshot-tool.php' ) , array( &$this, 'add_settings_link' ) );
    44     }
    45 
    46     /**
    47      * Initialise settings
    48      * @return void
    49      */
    50     public function init() {
    51         $this->settings = $this->settings_fields();
    52     }
    53 
    54     /**
    55      * Add settings page to admin menu
    56      * @return void
    57      */
    58     public function add_menu_item() {
    59         $page = add_options_page( __( 'Tida URL Screenshot Settings', 'tida-url-screenshot' ) , __( 'Tida URL Screenshot Settings', 'tida-url-screenshot' ) , 'manage_options' , 'tida-url-screenshot-settings' ,  array( &$this, 'settings_page' ) );
    60     }
    61 
    62     /**
    63      * Add settings link to plugin list table
    64      * @param  array $links Existing links
    65      * @return array        Modified links
    66      */
    67     public function add_settings_link( $links ) {
    68         $settings_link[] = '<a href="'.esc_url( add_query_arg( array( 'page' => 'tida-url-screenshot-settings' ) , admin_url( '/options-general.php' ) ) ).'">' . __( 'Settings', 'tida-url-screenshot' ) . '</a>';
    69         $settings_link = array_merge( $settings_link, $links );
    70         return $settings_link;
    71     }
    72 
    73     /**
    74      * Build settings fields
    75      * @return array Fields to be displayed on settings page
    76      */
    77     private function settings_fields() {
    78 
    79         $settings['general'] = array(
    80             'title'       => __( 'General', 'tida-url-screenshot' ),
    81             'description' => __( 'This section is the appearance settings.', 'tida-url-screenshot' ),
    82             'fields'                => array(
    83                 array(
    84                     'id'            => 'input_class',
    85                     'label'         => __( 'Input Class' , 'tida-url-screenshot' ),
    86                     'description'   => __( 'You can enter name of input class for custom style.', 'tida-url-screenshot' ),
    87                     'type'          => 'text',
    88                     'default'       => '',
    89                     'length'        => 20,
    90                     'placeholder'   => ''
    91                 ),
    92                 array(
    93                     'id'            => 'button_class',
    94                     'label'         => __( 'Button Class' , 'tida-url-screenshot' ),
    95                     'description'   => __( 'You can enter name of button class for custom style.', 'tida-url-screenshot' ),
    96                     'type'          => 'text',
    97                     'default'       => '',
    98                     'length'        => 20,
    99                     'placeholder'   => ''
    100                 ),
    101             )
    102         );
    103 
    104         $settings['api'] = array(
    105             'title'       => __( 'API', 'tida-url-screenshot' ),
    106             'description' => __( 'This section is the settings of API service.', 'tida-url-screenshot' ),
    107             'fields'                => array(
    108                 array(
    109                     'id'          => 'api_service',
    110                     'label'       => __( 'API Service', 'tida-url-screenshot' ),
    111                     'description' => __( 'Please select the service for taking the screenshot', 'tida-url-screenshot' ),
    112                     'type'        => 'select',
    113                     'options'     => apply_filters('tida_screenshot_api_list', $list )
    114                 ),
    115                 array(
    116                     'id'            => 'api_token',
    117                     'label'         => __( 'API Token' , 'tida-url-screenshot' ),
    118                     'description'   => '',
    119                     'type'          => 'text',
    120                     'default'       => '',
    121                     'length'        => 70,
    122                     'placeholder'   => ''
    123                 ),
    124             )
    125         );
    126 
    127         $settings = apply_filters( 'tida_url_screenshot_settings_fields', $settings );
    128 
    129         return $settings;
    130     }
    131 
    132     /**
    133      * Register plugin settings
    134      * @return void
    135      */
    136     public function register_settings() {
    137         if ( is_array( $this->settings ) ) {
    138 
    139             // Check posted/selected tab.
    140             $current_section = '';
    141             if ( isset( $_POST['tab'] ) && !empty( $_POST['tab'] ) ) {
    142                 $current_section = sanitize_text_field( wp_unslash( $_POST['tab'] ) );
    143             } else {
    144                 if ( isset( $_GET['tab'] ) && !empty( $_GET['tab'] ) ) {
    145                     $current_section = sanitize_text_field( wp_unslash( $_GET['tab'] ) );
    146                 }
    147             }
    148 
    149             foreach ( $this->settings as $section => $data ) {
    150 
    151                 if ( $current_section && $current_section !== $section ) {
    152                     continue;
    153                 }
    154 
    155                 // Add section to page.
    156                 add_settings_section( $section, $data['title'], array( $this, 'settings_section' ), 'tida_url_screenshot_settings' );
    157 
    158                 foreach ( $data['fields'] as $field ) {
    159 
    160                     // Validation callback for field.
    161                     $validation = '';
    162                     if ( isset( $field['callback'] ) ) {
    163                         $validation = $field['callback'];
    164                     }
    165 
    166                     // Register field.
    167                     $option_name = $this->base . $field['id'];
    168                     register_setting( 'tida_url_screenshot_settings', $option_name, $validation );
    169 
    170                     // Add field to page.
    171                     add_settings_field(
    172                         $field['id'],
    173                         $field['label'],
    174                         array( &$this, 'display_field' ),
    175                         'tida_url_screenshot_settings',
    176                         $section,
    177                         array(
    178                             'field'  => $field,
    179                             'prefix' => $this->base,
    180                         )
    181                     );
    182                 }
    183 
    184                 if ( ! $current_section ) {
    185                     break;
    186                 }
    187             }
    188         }
    189     }
    190 
    191     /**
    192      * Settings section description field.
    193      *
    194      * @param array $section Array of section ids.
    195      * @return void
    196      */
    197     public function settings_section( $section ) {
    198         $html = '<p> ' . $this->settings[ $section['id'] ]['description'] . '</p>' . "\n";
    199         $html = apply_filters('settings_section_output', $html );
    200         echo wp_kses_post( $html );
    201     }
    202 
    203     /**
    204      * Generate HTML for displaying fields.
    205      *
    206      * @param  array   $data Data array.
    207      * @param  object  $post Post object.
    208      * @param  boolean $echo  Whether to echo the field HTML or return it.
    209      * @return string
    210      */
    211     public function display_field( $data = array(), $post = null, $echo = true ) {
    212 
    213         // Get field info.
    214         if ( isset( $data['field'] ) ) {
    215             $field = $data['field'];
    216         } else {
    217             $field = $data;
    218         }
    219 
    220         // Check for prefix on option name.
    221         $option_name = '';
    222         if ( isset( $data['prefix'] ) ) {
    223             $option_name = $data['prefix'];
    224         }
    225 
    226         // Get saved data.
    227         $data = '';
    228         if ( $post ) {
    229 
    230             // Get saved field data.
    231             $option_name .= $field['id'];
    232             $option       = get_post_meta( $post->ID, $field['id'], true );
    233 
    234             // Get data to display in field.
    235             if ( isset( $option ) ) {
    236                 $data = $option;
    237             }
    238         } else {
    239 
    240             // Get saved option.
    241             $option_name .= $field['id'];
    242             $option       = get_option( $option_name );
    243 
    244             // Get data to display in field.
    245             if ( isset( $option ) ) {
    246                 $data = $option;
    247             }
    248         }
    249 
    250         // Show default data if no option saved and default is supplied.
    251         if ( false === $data && isset( $field['default'] ) ) {
    252             $data = $field['default'];
    253         } elseif ( false === $data ) {
    254             $data = '';
    255         }
    256 
    257         $html = '';
    258 
    259         switch ( $field['type'] ) {
    260 
    261             case 'text':
    262             case 'url':
    263                 $html .= '<input id="' . esc_attr( $field['id'] ) . '" type="text" name="' . esc_attr( $option_name ) . '" placeholder="' . esc_attr( $field['placeholder'] ) . '" size="'.esc_attr( $field['length'] ).'" value="' . esc_attr( $data ) . '" />' . "\n";
    264                 break;
    265 
    266             case 'hidden':
    267                 $min = '';
    268                 if ( isset( $field['min'] ) ) {
    269                     $min = ' min="' . esc_attr( $field['min'] ) . '"';
    270                 }
    271 
    272                 $max = '';
    273                 if ( isset( $field['max'] ) ) {
    274                     $max = ' max="' . esc_attr( $field['max'] ) . '"';
    275                 }
    276                 $html .= '<input id="' . esc_attr( $field['id'] ) . '" type="' . esc_attr( $field['type'] ) . '" name="' . esc_attr( $option_name ) . '" placeholder="' . esc_attr( $field['placeholder'] ) . '" value="' . esc_attr( $data ) . '"' . $min . '' . $max . '/>' . "\n";
    277                 break;
    278 
    279             case 'checkbox':
    280                 $checked = '';
    281                 if ( $data && 'on' === $data ) {
    282                     $checked = 'checked="checked"';
    283                 }
    284                 $html .= '<input id="' . esc_attr( $field['id'] ) . '" type="' . esc_attr( $field['type'] ) . '" name="' . esc_attr( $option_name ) . '" ' . $checked . '/>' . "\n";
    285                 break;
    286 
    287             case 'select':
    288                 $html .= '<select name="' . esc_attr( $option_name ) . '" id="' . esc_attr( $field['id'] ) . '">';
    289                 foreach ( $field['options'] as $k => $v ) {
    290                     $selected = false;
    291                     if ( $k === $data ) {
    292                         $selected = true;
    293                     }
    294                     if($k == "mapbox")
    295                         $html .= '<option ' . selected( $selected, true, false ) . ' value="' . esc_attr( $k ) . '"  disabled="disabled">' . $v . '</option>';
    296                     else
    297                         $html .= '<option ' . selected( $selected, true, false ) . ' value="' . esc_attr( $k ) . '">' . $v . '</option>';
    298                 }
    299                 $html .= '</select> ';
    300                 break;
    301 
    302         }
    303 
    304         switch ( $field['type'] ) { // for description
    305 
    306             case 'text':
    307             case 'select':
    308                 $html .= '<p class="description">' . $field['description'] . '</p>';
    309                 break;
    310 
    311             default:
    312                 if ( ! $post ) {
    313                     $html .= '<label for="' . esc_attr( $field['id'] ) . '">' . "\n";
    314                 }
    315 
    316                 $html .= '<span class="description">' . $field['description'] . '</span>' . "\n";
    317 
    318                 if ( ! $post ) {
    319                     $html .= '</label>' . "\n";
    320                 }
    321                 break;
    322         }
    323 
    324         if ( ! $echo ) {
    325             return apply_filters('display_field_output', $html);
    326         }
    327 
    328         // Define allowed HTML tags and attributes for form elements
    329         $allowed_tags = array(
    330             'input' => array(
    331                 'id' => array(),
    332                 'type' => array(),
    333                 'name' => array(),
    334                 'placeholder' => array(),
    335                 'size' => array(),
    336                 'value' => array(),
    337                 'checked' => array(),
    338                 'min' => array(),
    339                 'max' => array(),
    340                 'disabled' => array(),
    341             ),
    342             'select' => array(
    343                 'name' => array(),
    344                 'id' => array(),
    345                 'disabled' => array(),
    346             ),
    347             'option' => array(
    348                 'value' => array(),
    349                 'selected' => array(),
    350             ),
    351             'p' => array(
    352                 'class' => array(),
    353             ),
    354             'span' => array(
    355                 'class' => array(),
    356             ),
    357             'label' => array(
    358                 'for' => array(),
    359             ),
    360         );
    361    
    362         $html = apply_filters('display_field_output', $html);
    363    
    364         // Escape output with wp_kses and custom allowed tags
    365         if ( ! $echo ) {
    366             return wp_kses( $html, $allowed_tags );
    367         }
    368    
    369         echo wp_kses( $html, $allowed_tags );
    370     }
    371 
    372     /**
    373      * Validate form field
    374      *
    375      * @param  string $data Submitted value.
    376      * @param  string $type Type of field to validate.
    377      * @return string       Validated value
    378      */
    379     public function validate_field( $data = '', $type = 'text' ) {
    380 
    381         switch ( $type ) {
    382             case 'text':
    383                 $data = esc_attr( $data );
    384                 break;
    385             case 'url':
    386                 $data = esc_url( $data );
    387                 break;
    388             case 'email':
    389                 $data = is_email( $data );
    390                 break;
    391         }
    392 
    393         return $data;
    394     }
    395 
    396     /**
    397      * Load settings page content
    398      * @return void
    399      */
    400     public function settings_page() {
    401 
    402         // Build page HTML.
    403         $html = '<div class="wrap" id="tida_url_screenshot_settings">' . "\n";
    404             $html .= '<h2>' . __( 'Tida URL Screenshot Settings', 'tida-url-screenshot' ) . '</h2>' . "\n";
    405 
    406             $tab = '';
    407             if ( isset( $_GET['tab'] ) && !empty( $_GET['tab'] ) ) {
    408                 $tab .= sanitize_text_field( wp_unslash( $_GET['tab'] ) );
    409             }
    410 
    411             // Show page tabs.
    412             if ( is_array( $this->settings ) && 1 < count( $this->settings ) ) {
    413 
    414                 $html .= '<h2 class="nav-tab-wrapper">' . "\n";
    415 
    416                 $c = 0;
    417                 foreach ( $this->settings as $section => $data ) {
    418 
    419                     // Set tab class.
    420                     $class = 'nav-tab';
    421                     if ( ! isset( $_GET['tab'] ) ) {
    422                         if ( 0 === $c ) {
    423                             $class .= ' nav-tab-active';
    424                         }
    425                     } else {
    426                         if ( isset( $_GET['tab'] ) && $section == sanitize_text_field( wp_unslash( $_GET['tab'] ) ) ) {
    427                             $class .= ' nav-tab-active';
    428                         }
    429                     }
    430 
    431                     // Set tab link.
    432                     $tab_link = add_query_arg( array( 'tab' => esc_attr( $section ) ) );
    433                     if ( isset( $_GET['settings-updated'] ) ) {
    434                         $tab_link = remove_query_arg( 'settings-updated', $tab_link );
    435                     }
    436 
    437                     // Output tab.
    438                     $html .= '<a href="' . esc_url( $tab_link ) . '" class="' . esc_attr( $class ) . '">' . esc_html( $data['title'] ) . '</a>' . "\n";
    439 
    440                     ++$c;
    441                 }
    442 
    443                 $html .= '</h2>' . "\n";
    444             }
    445 
    446                 $html .= '<form method="post" action="options.php" enctype="multipart/form-data">' . "\n";
    447 
    448                 // Get settings fields.
    449                 ob_start();
    450                 settings_fields( 'tida_url_screenshot_settings' );
    451                 do_settings_sections( 'tida_url_screenshot_settings' );
    452                 $html .= ob_get_clean();
    453 
    454                 $html     .= '<p class="submit">' . "\n";
    455                     $html .= '<input type="hidden" name="tab" value="' . esc_attr( $tab ) . '" />' . "\n";
    456                     $html .= '<input name="Submit" type="submit" class="button-primary" value="' . esc_attr( __( 'Save Settings', 'tida-url-screenshot' ) ) . '" />' . "\n";
    457                 $html .= '</p>' . "\n";
    458             $html .= '</form>' . "\n";
    459         $html .= '</div>' . "\n";
    460 
    461         // Define allowed HTML tags and attributes for form elements
    462         $allowed_tags = array(
    463             'div' => array(
    464                 'class' => array(),
    465                 'id' => array(),
    466             ),
    467             'h2' => array(
    468                 'class' => array()
    469             ),
    470             'a' => array(
    471                 'href' => array(),
    472                 'class' => array(),
    473             ),
    474             'form' => array(
    475                 'method' => array(),
    476                 'action' => array(),
    477                 'enctype' => array(),
    478             ),
    479             'input' => array(
    480                 'class' => array(),
    481                 'id' => array(),
    482                 'type' => array(),
    483                 'name' => array(),
    484                 'placeholder' => array(),
    485                 'size' => array(),
    486                 'value' => array(),
    487                 'checked' => array(),
    488                 'min' => array(),
    489                 'max' => array(),
    490                 'disabled' => array(),
    491             ),
    492             'select' => array(
    493                 'name' => array(),
    494                 'id' => array(),
    495                 'disabled' => array(),
    496             ),
    497             'option' => array(
    498                 'value' => array(),
    499                 'selected' => array(),
    500             ),
    501             'p' => array(
    502                 'class' => array(),
    503             ),
    504             'span' => array(
    505                 'class' => array(),
    506             ),
    507             'label' => array(
    508                 'for' => array(),
    509             ),
    510             'table' => array(
    511                 'class' => array(),
    512                 'id' => array(),
    513             ),
    514             'thead' => array(),
    515             'tbody' => array(),
    516             'tr' => array(),
    517             'th' => array(
    518                 'scope' => array(),
    519             ),
    520             'td' => array(),
    521         );
    522    
    523         $html = apply_filters('tida_url_screenshot_settings_page', $html);
    524    
    525         // Escape output with wp_kses and custom allowed tags
    526         echo wp_kses( $html, $allowed_tags );
    527     }
    528 
    529     public function admin_header_scripts()
    530     {
    531         global $pagenow;
    532 
    533         //Check if current admin page is Option Tree settings
    534         if ( $pagenow == 'options-general.php' && isset( $_GET['page'] ) && !empty( $_GET['page'] ) && $_GET['page'] == 'tida-url-screenshot-settings' ) {
    535         ?>
    536         <script>
    537         jQuery(document).ready(function(){
    538             // event for api token
    539             api_token_field_toggle();
    540             jQuery("select[name=tida_screenshot_api_service]").on('change', function(){ api_token_field_toggle(); });
    541         });
    542 
    543         function api_token_field_toggle()
    544         {
    545             var api_selector = jQuery("select[name=tida_screenshot_api_service]").val();
    546             if(api_selector=="ip-api" || api_selector=="ipwhois")
    547                 jQuery("input[name=tida_screenshot_api_token]").parent().parent().slideUp();
    548             else
    549                 jQuery("input[name=tida_screenshot_api_token]").parent().parent().slideDown();
    550         }
    551         </script>
    552         <?php
    553         }
    554     }
    555 
     16   
     17    /**
     18     * Holds the singleton instance of the Tida_URL_Screenshot_Settings class.
     19     *
     20     * @var Tida_URL_Screenshot_Settings|null
     21     */
     22    protected static $_instance = null;
     23
     24    /**
     25     * Singleton instance of the Tida_URL_Screenshot_Settings class.
     26     * Ensures that only one instance of the class exists at any given time.
     27     *
     28     * @return Tida_URL_Screenshot_Settings|null Returns the singleton instance of Tida_URL_Screenshot_Settings.
     29     */
     30    public static function instance() {
     31        if (is_null(self::$_instance)) {
     32            self::$_instance = new self();
     33        }
     34        return self::$_instance;
     35    }
     36
     37    /**
     38     * Constructor method.
     39     * Adds actions to initialize settings, create the settings page, enqueue scripts, and handle AJAX requests.
     40     *
     41     * @return void
     42     */
     43    public function __construct()
     44    {
     45        add_action('admin_enqueue_scripts', array(&$this, 'admin_enqueue_scripts') );
     46        add_action('admin_menu', array(&$this, 'add_settings_menu') );
     47        add_action('admin_init', array(&$this, 'settings_init') );
     48    }
     49   
     50    /**
     51     * Enqueues styles for the admin settings page.
     52     *
     53     * @return void
     54     */
     55    public function admin_enqueue_scripts()
     56    {
     57        $screen = get_current_screen();
     58        if (isset($screen->id) && $screen->id === 'settings_page_tida-url-screenshot-settings') {
     59            wp_enqueue_style('tida-screenshot', TIDA_URL_SCREENSHOT_PLUGIN_URL . '/assets/css/tida-screenshot-admin.css', array(), TIDA_URL_SCREENSHOT_VERSION );
     60        }
     61    }
     62
     63    /**
     64     * Creates the settings page under the admin menu.
     65     *
     66     * @return void
     67     */
     68    public function add_settings_menu()
     69    {
     70        add_options_page(
     71            __( 'Tida URL Screenshot Settings', 'tida-url-screenshot' ),
     72            __( 'Tida URL Screenshot Settings', 'tida-url-screenshot' ),
     73            'manage_options',
     74            'tida-url-screenshot-settings',
     75            array( &$this, 'settings_callback' )
     76        );
     77    }
     78
     79    /**
     80     * Initializes the settings and adds settings fields.
     81     *
     82     * @return void
     83     */
     84    public function settings_init()
     85    {
     86        register_setting('tida_url_screenshot_settings', 'tida_url_screenshot_settings', array($this, 'sanitize'));
     87
     88        add_settings_section(
     89            'tida_url_screenshot_settings_section',
     90            '',
     91            '',
     92            'tida-url-screenshot-settings'
     93        );
     94
     95        // API Service
     96        add_settings_field(
     97            'api_service',
     98            __('API Service', 'tida-url-screenshot'),
     99            array($this, 'api_service_field_callback'),
     100            'tida-url-screenshot-settings',
     101            'tida_url_screenshot_settings_section'
     102        );
     103
     104        // API Key
     105        add_settings_field(
     106            'api_key',
     107            __('API Key', 'tida-url-screenshot'),
     108            array($this, 'api_key_field_callback'),
     109            'tida-url-screenshot-settings',
     110            'tida_url_screenshot_settings_section'
     111        );
     112
     113        // Input Class
     114        add_settings_field(
     115            'input_class',
     116            __('Input Class', 'tida-url-screenshot'),
     117            array($this, 'input_class_field_callback'),
     118            'tida-url-screenshot-settings',
     119            'tida_url_screenshot_settings_section'
     120        );
     121
     122        // Button Class
     123        add_settings_field(
     124            'button_class',
     125            __('Button Class', 'tida-url-screenshot'),
     126            array($this, 'button_class_field_callback'),
     127            'tida-url-screenshot-settings',
     128            'tida_url_screenshot_settings_section'
     129        );
     130       
     131        // Enable on post types
     132        add_settings_field(
     133            'post_types',
     134            __('Post Types', 'tida-url-screenshot'),
     135            array($this, 'post_types_field_callback'),
     136            'tida-url-screenshot-settings',
     137            'tida_url_screenshot_settings_section'
     138        );
     139       
     140        // Button Position for post types
     141        add_settings_field(
     142            'button_position',
     143            __('Button Position', 'tida-url-screenshot'),
     144            array($this, 'button_position_field_callback'),
     145            'tida-url-screenshot-settings',
     146            'tida_url_screenshot_settings_section'
     147        );
     148    }
     149
     150    /**
     151     * Displays the settings page.
     152     *
     153     * @return void
     154     */
     155    public static function settings_callback()
     156    {
     157        ?>
     158        <div class="wrap">
     159            <div class="settings-header">
     160                <h1><?php echo esc_html( get_admin_page_title() ); ?></h1>
     161            </div>
     162            <div class="settings-body">
     163                <form method="post" action="options.php">
     164                    <?php
     165                    settings_fields('tida_url_screenshot_settings');
     166                    do_settings_sections('tida-url-screenshot-settings');
     167                    submit_button();
     168                    ?>
     169                </form>
     170            </div>
     171        </div>
     172        <?php
     173    }
     174
     175    /**
     176     * Outputs the dropdown field for selecting API services.
     177     *
     178     * @return void
     179     */
     180    public function api_service_field_callback()
     181    {
     182        global $wpdb;
     183        $settings = get_option('tida_url_screenshot_settings');
     184        ?>
     185        <select name="tida_url_screenshot_settings[api_service]">
     186            <option value=""><?php _e('Select the API service...', 'tida-url-screenshot'); ?></option>
     187            <?php
     188            $api_services_list = array();
     189            $api_services_list = apply_filters('tida_screenshot_api_list', $api_services_list );
     190           
     191            foreach ($api_services_list as $key => $item) {
     192                $selected = (isset($settings['api_service']) && $settings['api_service'] === $key) ? 'selected' : '';
     193                echo '<option value="' . esc_attr($key) . '" ' . $selected . '>' . esc_html($item) . '</option>';
     194            }
     195            ?>
     196        </select>
     197        <p class="description"><?php echo esc_attr__('Please select the service for taking the screenshot.', 'tida-url-screenshot'); ?></p>
     198        <?php
     199    }
     200   
     201    /**
     202     * Outputs the input field for the API key.
     203     *
     204     * @return void
     205     */
     206    public function api_key_field_callback()
     207    {
     208        $settings = get_option('tida_url_screenshot_settings');
     209        ?>
     210        <input type="text" name="tida_url_screenshot_settings[api_key]" value="<?php echo esc_attr( $settings['api_key'] ?? ''); ?>" size="70" />
     211        <p class="description"><?php echo esc_attr__('API Key = Access Token = User Key = API Token', 'tida-url-screenshot'); ?></p>
     212        <?php
     213    }
     214
     215    /**
     216     * Outputs the input field for the input class.
     217     *
     218     * @return void
     219     */
     220    public function input_class_field_callback()
     221    {
     222        $settings = get_option('tida_url_screenshot_settings');
     223        ?>
     224        <input type="text" name="tida_url_screenshot_settings[input_class]" value="<?php echo esc_attr( $settings['input_class'] ?? ''); ?>" size="20" />
     225        <p class="description"><?php echo esc_attr__('Enter the name of input field class for custom style.', 'tida-url-screenshot'); ?></p>
     226        <?php
     227    }
     228
     229    /**
     230     * Outputs the input field for the button class.
     231     *
     232     * @return void
     233     */
     234    public function button_class_field_callback()
     235    {
     236        $settings = get_option('tida_url_screenshot_settings');
     237        ?>
     238        <input type="text" name="tida_url_screenshot_settings[button_class]" value="<?php echo esc_attr( $settings['button_class'] ?? ''); ?>" size="20" />
     239        <p class="description"><?php echo esc_attr__('Enter the name of button class for custom style.', 'tida-url-screenshot'); ?></p>
     240        <?php
     241    }
     242   
     243    /**
     244     * Outputs a list of checkboxes for selecting post types to enable screenshot functionality.
     245     *
     246     * @return void
     247     */
     248    public function post_types_field_callback()
     249    {
     250        global $wpdb;
     251        $settings = get_option('tida_url_screenshot_settings');
     252        ?>
     253        <?php foreach (self::get_post_types() as $key => $item): ?>
     254            <p>
     255                <label>
     256                    <input type="checkbox" name="tida_url_screenshot_settings[post_types][]" value="<?php echo esc_attr($key); ?>" <?php if (isset($settings['post_types']) && in_array($key, $settings['post_types'])) echo 'checked'; ?>> <?php echo esc_attr($item); ?>
     257                </label>
     258            </p>
     259        <?php endforeach; ?>
     260        <p class="description"><?php echo esc_attr__('Select one or more post types to enable screenshot functionality on them.', 'tida-url-screenshot'); ?></p>
     261        <?php
     262    }
     263   
     264    /**
     265     * Outputs a dropdown list for selecting the button position.
     266     *
     267     * @return void
     268     */
     269    public function button_position_field_callback()
     270    {
     271        global $wpdb;
     272        $settings = get_option('tida_url_screenshot_settings');
     273        ?>
     274        <select name="tida_url_screenshot_settings[button_position]">
     275            <option value=""><?php _e('Select the button position...', 'tida-url-screenshot'); ?></option>
     276            <?php
     277            $button_positions = array('top' => __('Top of Content', 'tida-url-screenshot'), 'bottom' => __('Bottom of Content', 'tida-url-screenshot'));
     278            foreach ($button_positions as $key => $item) {
     279                $selected = (isset($settings['button_position']) && $settings['button_position'] === $key) ? 'selected' : '';
     280                echo '<option value="' . esc_attr($key) . '" ' . $selected . '>' . esc_html($item) . '</option>';
     281            }
     282            ?>
     283        </select>
     284        <p class="description"><?php echo esc_attr__('Select where to display the screenshot button.', 'tida-url-screenshot'); ?></p>
     285        <?php
     286    }
     287   
     288    /**
     289     * Sanitizes the input from the settings fields.
     290     *
     291     * @param array $input The input from the settings form.
     292     * @return array The sanitized input values, returning an array of sanitized settings fields.
     293     */
     294    public function sanitize($input)
     295    {
     296        $sanitized_input = array();
     297       
     298        if (isset($input['api_service'])) {
     299            $sanitized_input['api_service'] = sanitize_text_field($input['api_service']);
     300        }
     301   
     302        if (isset($input['api_key'])) {
     303            $sanitized_input['api_key'] = sanitize_text_field($input['api_key']);
     304        }
     305   
     306        if (isset($input['input_class'])) {
     307            $sanitized_input['input_class'] = sanitize_text_field($input['input_class']);
     308        }
     309   
     310        if (isset($input['button_class'])) {
     311            $sanitized_input['button_class'] = sanitize_text_field($input['button_class']);
     312        }
     313   
     314        // Handle post types
     315        if (isset($input['post_types']) && is_array($input['post_types'])) {
     316            $valid_post_types = array_keys(self::get_post_types()); // Get valid post type keys
     317            $sanitized_input['post_types'] = array(); // Initialize sanitized input array
     318   
     319            foreach ($input['post_types'] as $post_type) {
     320                // Check if the current post type is valid and sanitize
     321                if (in_array($post_type, $valid_post_types)) {
     322                    $sanitized_input['post_types'][] = sanitize_text_field($post_type);
     323                }
     324            }
     325        }
     326   
     327        if (isset($input['button_position']) && in_array($input['button_position'], array('top', 'bottom'))) {
     328            $sanitized_input['button_position'] = sanitize_text_field($input['button_position']);
     329        }
     330   
     331        return $sanitized_input;
     332    }
     333   
     334    /**
     335     * Get all registered post types.
     336     *
     337     * This method retrieves an array of public post types registered in WordPress,
     338     * returning the post type names and their corresponding labels.
     339     *
     340     * @return array An associative array of post type names as keys and post type labels as values.
     341     */
     342    private static function get_post_types()
     343    {
     344        $post_types = get_post_types( array('public' => true ), 'objects' );
     345        $options    = array();
     346       
     347        foreach ($post_types as $post_type) {
     348            if( $post_type->name == 'attachment' ) continue;
     349            $options[$post_type->name] = $post_type->label;
     350        }
     351       
     352        return $options;
     353    }
    556354}
    557355
    558 new Tida_URL_Screenshot_Settings();
    559 ?>
     356if (class_exists('Tida_URL_Screenshot_Settings')) {
     357    Tida_URL_Screenshot_Settings::instance();
     358}
  • tida-url-screenshot/trunk/includes/class-API.php

    r3214557 r3228575  
    11<?php
     2/**
     3 * Class Tida_Screenshot_API_Core
     4 *
     5 * Package: Tida URL Screenshot
     6 * Author: Tidaweb
     7 */
    28
    3 if ( ! defined( 'ABSPATH' ) ) exit;
     9if (!defined('ABSPATH')) exit;
    410
    511class Tida_Screenshot_API_Core {
    612
    7     /**
    8      * Prefix for plugin settings.
    9     *
    10      * @var    string
    11      * @access public
    12      * @since  1.0.0
    13     */
    14     public static $api_token = '';
     13    /**
     14     * API token for authenticating requests to the API.
     15    *
     16     * @var string
     17     * @access public
     18     * @since 1.0.0
     19    */
     20    public static $api_token = '';
    1521
    16     /**
    17      * Available settings for plugin.
    18      *
    19      * @var     array
    20      * @access  protected
    21      * @since   1.0.0
    22      */
    23     protected static $api_url = '';
     22    /**
     23     * URL for the API.
     24     * This variable is populated when API settings are initialized.
     25     *
     26     * @var string
     27     * @access protected
     28     * @since 1.0.0
     29     */
     30    protected static $api_url = '';
    2431
    25     public function __construct() {
    26        
    27         // initialize
    28         self::$api_token = get_option('tida_screenshot_api_token');
     32    /**
     33     * Constructor method.
     34     * Initializes the API token from the plugin settings and adds available APIs to the list.
     35     *
     36     * @return void
     37     */
     38    public function __construct()
     39    {
     40        // Initialize
     41        self::$api_token = get_option('tida_url_screenshot_settings')['api_key'];
    2942
    30         add_filter('tida_screenshot_api_list', array($this, 'add_api_to_list'));
     43        // Add API to the list
     44        add_filter('tida_screenshot_api_list', array($this, 'add_api_to_list'));
     45    }
    3146
    32     }
     47    /**
     48     * Add API to the list.
     49     *
     50     * @param array $list The current list of APIs.
     51     * @return array The modified list of APIs, including any new additions.
     52     */
     53    public function add_api_to_list($list)
     54    {
     55        // You would typically add your API endpoint details here.
     56        // Example: $list['My New API'] = 'http://api.example.com';
     57        return $list;
     58    }
    3359
    34     /**
    35      * Add API to list - add_filter for tida_screenshot_api_list
    36      * @param list
    37      */
    38     public function add_api_to_list( $list )
    39     {
    40         return $list;
    41     }
     60    /**
     61     * Get the API URL value.
     62     *
     63     * @return string The sanitized API URL.
     64     */
     65    public static function get_api_url()
     66    {
     67        return esc_url(self::$api_url);
     68    }
    4269
    43     /**
    44      * Get url value
    45      */
    46     public static function get_api_url()
    47     {
    48         return esc_url( self::$api_url );
    49     }
     70    /**
     71     * Send data to the specified API endpoint.
     72     *
     73     * @param string $web_api_url The URL of the API endpoint to which the request is sent.
     74     * @param array $body Optional. Additional data to include in the request body. Default is an empty array.
     75     * @return string The base64-encoded image URL, or an empty string if there was an error fetching the image.
     76     */
     77    public static function send_data_to_api($web_api_url, $body = [])
     78    {
     79        $final_image_url = '';
    5080
    51     /**
    52      * Call API
    53      * @param web_api_url
    54      * @param body
    55      * @return final_result
    56      */
    57     public static function send_data_to_api( $web_api_url, $body = [] )
    58     {
    59         $final_image_url = '';
     81        if (!empty($web_api_url)) {
     82            $request = wp_remote_get($web_api_url, array('timeout' => 7000));
    6083
    61         if(!empty($web_api_url))
    62         {
    63             $request = wp_remote_get( $web_api_url, array( 'timeout' => 7000 ) );
    64            
    65             if ( is_wp_error( $request ) )
    66             {
    67                 //error_log( print_r( $request, true ) );
    68             }
    69             else
    70             {
    71                 $response = wp_remote_retrieve_body( $request );
    72                 $imageData = base64_encode($response);
    73            
    74                 // Format the image SRC:  data:{mime};base64,{data};
    75                 $final_image_url = 'data:image/png;base64,'.$imageData;
    76             }
    77         }
    78        
     84            if (is_wp_error($request)) {
     85                // Handle error appropriately
     86            } else {
     87                $response = wp_remote_retrieve_body($request);
     88                $imageData = base64_encode($response);
    7989
    80         return apply_filters('api_result_data_output', $final_image_url );
    81     }
     90                if (!is_wp_error($response)) {
     91                    // Format the image SRC: data:{mime};base64,{data};
     92                    $final_image_url = 'data:image/jpeg;base64,' . $imageData;
     93                }
     94            }
     95        }
    8296
    83    
     97        return apply_filters('api_result_data_output', $final_image_url);
     98    }
    8499}
    85100
     101// Instantiate the core API class for handling requests
    86102new Tida_Screenshot_API_Core();
    87 ?>
  • tida-url-screenshot/trunk/readme.txt

    r3214556 r3228575  
    44Requires at least: 4.0
    55Tested up to: 6.7
    6 Stable tag: 1.0.3
     6Stable tag: 1.1
    77License: GPLv2 or later
    88License URI: http://www.gnu.org/licenses/gpl-2.0.html
    99
    10 URL Screenshot tool for your website
     10URL Screenshot Tool for your website
    1111
    1212== Description ==
    1313
    14 The user takes the photo by entering the site address in the form that it is on its site. This plugin is using 3rd party api service.
     14# Tida URL Screenshot Plugin
    1515
    16 you can use following api services:
    17 - <a href="https://www.abstractapi.com/api/website-screenshot-api">abstractapi.com</a> / <a href="https://www.abstractapi.com/legal/legal">Terms and Policies</a>
    18 - <a href="https://screenshotlayer.com/">abstractapi.com</a> / <a href="https://www.ideracorp.com/Legal/PrivacyPolicy">Terms and Policies</a>
    19 - new API Coming soon
     16The **Tida URL Screenshot Plugin** has been developed by the professional team at **Tida Web**. Tida Web is a leading company in Web Design, offering services such as [Corporate Website Design](https://tidaweb.com/website-design), E-Commerce Store Development, and Custom [WordPress Plugin Programming](https://tidaweb.com/wp-plugin/).
    2017
    21 Features:
    22 - [tida_screenshot] shortcode for showing URL Screenshot form
     18This plugin utilizes a third-party web service to receive a URL from the user and provide a screenshot of the requested page. Additionally, the plugin supports taking screenshots of a custom post type, making it a versatile and practical tool for various user needs.
     19
     20## Supported API Services
     21- [abstractapi.com](https://www.abstractapi.com/api/website-screenshot-api) / [Privacy Policy](https://www.abstractapi.com/legal/legal)
     22- [apiflash.com](https://apiflash.com) / [Privacy Policy](https://apiflash.com/privacy_policy)
     23- [screenshotlayer.com](https://screenshotlayer.com) / [Privacy Policy](https://www.ideracorp.com/Legal/PrivacyPolicy)
     24- [screenshotmachine.com](https://screenshotmachine.com) / [Privacy Policy](https://www.screenshotmachine.com/privacypolicy.php)
     25- [screenshotone.com](https://screenshotone.com) / [Privacy Policy](https://screenshotone.com/privacy-policy/)
     26- **New API Coming Soon**
     27
     28## Features
     29- `[tida_screenshot]` shortcode for showing the URL Screenshot form.
    2330
    2431== Installation ==
     
    4754== Changelog ==
    4855
     56= 1.1 =
     57* Bugs fixes and improvements
     58* Added new APIs
     59* Added Button to custom post types for taking the screenshot from it
     60* Settings new UI design and improvement
     61
    4962= 1.0.3 =
    5063* Bugs fixes and improvements
  • tida-url-screenshot/trunk/tida-url-screenshot.php

    r3214556 r3228575  
    22/*
    33Plugin Name: Tida URL Screenshot
    4 Description: This plugin is adding the tool on your website that take a screenshot from URL.
    5 Version: 1.0.3
     4Description: This plugin is adding the tool on your website that takes a screenshot from a URL.
     5Version: 1.1
    66Author: Tidaweb
    77Author URI: https://tidaweb.com
     
    1515if(!class_exists('Tida_URL_Screenshot'))
    1616{
    17     class Tida_URL_Screenshot
    18     {
    19         /**
    20         * Construct the plugin object
    21         */
    22         public function __construct()
    23         {
    24             define( 'TIDA_URL_SCREENSHOT_VERSION', '1.0.2' );
     17    class Tida_URL_Screenshot
     18    {
     19        /**
     20        * Construct the plugin object
     21        */
     22        public function __construct()
     23        {
     24            define( 'TIDA_URL_SCREENSHOT_VERSION', '1.1' );
    2525            define( 'TIDA_URL_SCREENSHOT_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
    26 
    27             // Initialize Settings
    28             add_action( 'wp_enqueue_scripts',                   array(&$this, 'header_enqueue_scripts'));
    29            
    30             // tida_screenshot shortcode
    31             add_shortcode( 'tida_screenshot',                   array(&$this, 'tida_screenshot_shortcode'));
    32 
    33             // add ajax for form
    34             add_action( 'wp_ajax_get_tida_screenshot',          array(&$this, 'ajax_get_tida_screenshot'));
    35             add_action( 'wp_ajax_nopriv_get_tida_screenshot',   array(&$this, 'ajax_get_tida_screenshot'));
    36 
     26            define( 'TIDA_URL_SCREENSHOT_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
     27           
     28            // Initialize Settings
     29            add_action( 'wp_enqueue_scripts', array(&$this, 'header_enqueue_scripts'));
     30           
     31            // tida_screenshot shortcode & ajax call
     32            add_shortcode( 'tida_screenshot', array(&$this, 'tida_screenshot_shortcode'));
     33            add_action( 'wp_ajax_get_tida_url_screenshot', array(&$this, 'ajax_get_tida_url_screenshot'));
     34            add_action( 'wp_ajax_nopriv_get_tida_url_screenshot', array(&$this, 'ajax_get_tida_url_screenshot'));
     35           
     36            // Add screenshot button to post type & ajax call
     37            add_filter( 'the_content' , array( &$this, 'add_screenshot_button_to_content' ) );
     38            add_action( 'wp_ajax_get_tida_post_screenshot', array(&$this, 'ajax_get_tida_post_screenshot'));
     39            add_action( 'wp_ajax_nopriv_get_tida_post_screenshot',  array(&$this, 'ajax_get_tida_post_screenshot'));
     40           
    3741            // initialize classes
    3842            self::initialize_classes();
    39 
    40             do_action('tida_url_screenshot_plugin_hooks');
    41            
    42         } // END public function __construct()
    43 
    44         /**
    45          * initialize classes of tida url screenshot
     43           
     44            do_action('tida_url_screenshot_plugin_hooks');
     45           
     46        } // END public function __construct()
     47       
     48        /**
     49         * Initialize classes of Tida URL Screenshot.
     50         *
     51         * @return void
    4652         */
    4753        public static function initialize_classes()
    4854        {
    49             // include settings page
     55            // include settings page
    5056            require_once('includes/admin/class-settings.php');
    51 
    52             // include api services
    53             require_once('includes/class-API.php');
    54             require_once('includes/class-abstractapi.php');
    55             require_once('includes/class-screenshotlayer.php');
    56         }
    57 
    58         public function header_enqueue_scripts()
    59         {
    60             $post = get_post( get_the_ID() );
    61             if(isset( $post ) && !empty( $post ) && has_shortcode($post->post_content, 'tida_screenshot'))
    62             {
    63                 wp_enqueue_style('tida-screenshot', plugins_url( '/assets/css/tida-screenshot.css', __FILE__ ), array(), TIDA_URL_SCREENSHOT_VERSION );
    64                 wp_register_script('tida-screenshot', plugins_url( '/assets/js/tida-screenshot.js', __FILE__ ), array('jquery'), TIDA_URL_SCREENSHOT_VERSION, true );
    65 
    66                 wp_localize_script( 'tida-screenshot', 'tida_screenshot_params', [
    67                     'ajax_url' => admin_url('admin-ajax.php'),
    68                     'please_text' => __('Please Wait', 'tida-url-screenshot')
    69                 ]);
    70 
    71                 wp_enqueue_script('tida-screenshot');
    72             }
    73            
    74         }
    75        
    76         /**
    77          * IP Geo Shortcode: show IP Geo Location form and result
    78          */
     57           
     58            // include api services
     59            require_once('includes/class-api.php');
     60            require_once('includes/class-abstract-api.php');
     61            require_once('includes/class-api-flash.php');
     62            require_once('includes/class-screenshot-layer.php');
     63            require_once('includes/class-screenshot-machine.php');
     64            require_once('includes/class-screenshot-one.php');
     65        }
     66       
     67        /**
     68         * Enqueue necessary styles and scripts in the header.
     69         *
     70         * @return void
     71         */
     72        public function header_enqueue_scripts()
     73        {
     74            $post = get_post( get_the_ID() );
     75            if(isset( $post ) && !empty( $post ) && ( has_shortcode($post->post_content, 'tida_screenshot') || is_single() ) )
     76            {
     77                wp_enqueue_style('tida-screenshot', plugins_url( '/assets/css/tida-screenshot.css', __FILE__ ), array(), TIDA_URL_SCREENSHOT_VERSION );
     78                wp_register_script('tida-screenshot', plugins_url( '/assets/js/tida-screenshot.js', __FILE__ ), array('jquery'), TIDA_URL_SCREENSHOT_VERSION, true );
     79           
     80                wp_localize_script( 'tida-screenshot', 'tida_screenshot_params', [
     81                    'ajax_url' => admin_url('admin-ajax.php'),
     82                    'please_text' => __('Please Wait...', 'tida-url-screenshot')
     83                ]);
     84           
     85                wp_enqueue_script('tida-screenshot');
     86            }
     87        }
     88       
     89        /**
     90         * Handle the Tida Screenshot shortcode.
     91         *
     92         * @return string The HTML output of the screenshot form.
     93         */
    7994        public function tida_screenshot_shortcode()
    8095        {
    81             ob_start();
    82             $tida_screenshot_input_class = get_option('tida_screenshot_input_class');
    83             $tida_screenshot_button_class = get_option('tida_screenshot_button_class');
     96            ob_start();
     97            $tida_url_screenshot_settings = get_option('tida_url_screenshot_settings');
    8498            ?>
    85             <form class="tida_screenshot_form" method="post" action="">
    86                 <?php wp_nonce_field('tida_url_screenshot_nonce', 'tida_url_screenshot_nonce'); ?>
    87                 <input type="text" <?php if(!empty($tida_screenshot_input_class)) echo 'class="'.esc_attr( $tida_screenshot_input_class ).'"'; ?> name="url" placeholder="<?php echo esc_attr__('Enter URL...', 'tida-url-screenshot'); ?>" />
    88                 <input type="submit" <?php if(!empty($tida_screenshot_button_class)) echo 'class="'.esc_attr( $tida_screenshot_button_class ).'"'; ?> name="check" value="<?php echo esc_attr__('Take Screenshot', 'tida-url-screenshot'); ?>" />
     99            <form class="tida-screenshot-form" method="post" action="">
     100                <?php wp_nonce_field('tida_url_screenshot_nonce', 'tida_url_screenshot_nonce'); ?>
     101                <input type="text" <?php if(!empty($tida_url_screenshot_settings['input_class'])) echo 'class="'.esc_attr( $tida_url_screenshot_settings['input_class'] ).'"'; ?> name="url" placeholder="<?php echo esc_attr__('Enter URL...', 'tida-url-screenshot'); ?>" />
     102                <input type="submit" <?php if(!empty($tida_url_screenshot_settings['button_class'])) echo 'class="'.esc_attr( $tida_url_screenshot_settings['button_class'] ).'"'; ?> name="check" value="<?php echo esc_attr__('Take Screenshot', 'tida-url-screenshot'); ?>" />
    89103            </form>
    90             <div class="screenshot_msg"></div>
    91             <div class="screenshot_result"><img src="" /></div>
    92             <?php
    93 
     104            <div class="screenshot-msg"></div>
     105            <div class="screenshot-result"><img src="" /></div>
     106            <?php
     107           
    94108            $output = ob_get_contents();
    95109            ob_end_clean();
    96110            return apply_filters('tida_screenshot_shortcode_filter', $output);
    97         }
    98 
    99 
    100        
    101         /**
    102          * Ajax handler for getting the screenshot from api
    103          */
    104         public function ajax_get_tida_screenshot()
    105         {
    106             // get screenshot from api
    107             $result = [];
    108             if( isset( $_POST['url'] ) && !empty( $_POST['url'] ) )
    109             {
    110                 $url = sanitize_url( wp_unslash( $_POST['url'] ) );
    111                 if(!is_null($url))
    112                 {
    113                     $api_service = get_option('tida_screenshot_api_service');
    114                     switch($api_service)
    115                     {
    116                         case "abstractapi":
    117                             $result = [ 'status' => true , 'src' => AbstractAPI::prepare_data_for_api( $url ), 'msg' => '' ];
    118                             break;
    119                         case "screenshotlayer":
    120                             $result = [ 'status' => true , 'src' => ScreenshotLayerAPI::prepare_data_for_api( $url ), 'msg' => '' ];
    121                             break;
    122                         default:
    123                             $result = [ 'status' => false , 'src' => 'NO URL' ];
    124                             break;
    125                     }
    126                 }
    127             }
    128            
    129             echo wp_json_encode( $result );
    130             wp_die();
    131         }
    132        
    133         /**
    134         * Activate the plugin
    135         */
    136         public static function activate()
    137         {
    138             // Do nothing
    139         } // END public static function activate
    140 
    141         /**
    142         * Deactivate the plugin
    143         */
    144         public static function deactivate()
    145         {
    146             // Do nothing
    147         } // END public static function deactivate 
    148    
    149     } // END class Tida_URL_Screenshot
     111        }
     112       
     113        /**
     114         * Add a screenshot button to the post content.
     115         *
     116         * @param string $content The original post content.
     117         * @return string The modified content with the screenshot button.
     118         */
     119        public function add_screenshot_button_to_content($content)
     120        {
     121            $tida_url_screenshot_settings = get_option('tida_url_screenshot_settings');
     122            $enabled_post_types = ( isset( $tida_url_screenshot_settings['post_types'] ) && !empty( $tida_url_screenshot_settings['post_types'] ) ) ? array_map('sanitize_text_field', $tida_url_screenshot_settings['post_types'] ) : array();
     123            $button_position    = ( isset( $tida_url_screenshot_settings['button_position'] ) && !empty( $tida_url_screenshot_settings['button_position'] ) ) ? esc_attr( $tida_url_screenshot_settings['button_position'] ) : 'bottom';
     124       
     125            if ( is_singular( $enabled_post_types ) ) {
     126                $button_html = '<button class="tida-post-screenshot" data-pid="'. get_the_ID() .'">' . __('Take a screenshot from this page', 'tida-url-screenshot') . '</button>';
     127                $button_html .= '<div class="screenshot-msg"></div>';
     128                $button_html .= '<div class="screenshot-result"><img src="" /></div>';
     129       
     130                if ( 'top' === $button_position ) {
     131                    return $button_html . $content;
     132                } else {
     133                    return $content . $button_html;
     134                }
     135            }
     136       
     137            return $content;
     138        }
     139       
     140        /**
     141         * Ajax handler for getting the screenshot from API.
     142         *
     143         * @return void JSON encoded response with screenshot status
     144         */
     145        public function ajax_get_tida_url_screenshot()
     146        {
     147            // get screenshot from api
     148            $result = [];
     149            if( isset( $_POST['url'] ) && !empty( $_POST['url'] ) )
     150            {
     151                $url = sanitize_url( wp_unslash( $_POST['url'] ) );
     152                if(!is_null($url))
     153                {
     154                    $tida_url_screenshot_settings = get_option('tida_url_screenshot_settings');
     155                    if( !empty( $tida_url_screenshot_settings['api_service'] ) )
     156                    {
     157                        switch( $tida_url_screenshot_settings['api_service'] )
     158                        {
     159                            case "abstractapi":
     160                                $screenshot_img_src = AbstractAPI::prepare_data_for_api( $url );
     161                                break;
     162                            case "apiflash":
     163                                $screenshot_img_src = ApiFlashAPI::prepare_data_for_api( $url );
     164                                break;
     165                            case "screenshotlayer":
     166                                $screenshot_img_src = ScreenshotLayerAPI::prepare_data_for_api( $url );
     167                                break;
     168                            case "screenshotmachine":
     169                                $screenshot_img_src = ScreenShotMachineAPI::prepare_data_for_api( $url );
     170                                break;
     171                            case "screenshotone":
     172                                $screenshot_img_src = ScreenshotOneAPI::prepare_data_for_api( $url );
     173                                break;
     174                            default:
     175                                $screenshot_img_src = '';
     176                                break;
     177                        }
     178                    }
     179                }
     180            }
     181           
     182            if( !empty( $screenshot_img_src ) )
     183            {
     184                $result = [ 'status' => true, 'src' => $screenshot_img_src, 'msg' => '' ];
     185            }
     186            else
     187            {
     188                $result = [ 'status' => false, 'msg' => esc_attr__('Error retrieving URL screenshot', 'tida-url-screenshot') ];
     189            }
     190           
     191            echo wp_json_encode( $result );
     192            wp_die();
     193        }
     194       
     195        /**
     196         * Ajax handler for getting the screenshot of a post.
     197         *
     198         * @return void JSON encoded response with screenshot status
     199         */
     200        public function ajax_get_tida_post_screenshot()
     201        {
     202            // get screenshot from api
     203            $result = [];
     204            if( isset( $_POST['pid'] ) && !empty( $_POST['pid'] ) )
     205            {
     206                $post_id = absint( wp_unslash( $_POST['pid'] ) );
     207                if( !empty( $post_id ) && $post_id >  0)
     208                {
     209                    $post_info = get_post( $post_id );
     210                    if( $post_info )
     211                    {
     212                        $post_url = get_permalink( $post_info->ID );
     213                        $tida_url_screenshot_settings = get_option('tida_url_screenshot_settings');
     214                        if( !empty( $tida_url_screenshot_settings['api_service'] ) )
     215                        {
     216                            switch( $tida_url_screenshot_settings['api_service'] )
     217                            {
     218                                case "abstractapi":
     219                                    $screenshot_img_src = AbstractAPI::prepare_data_for_api( $post_url );
     220                                    break;
     221                                case "apiflash":
     222                                    $screenshot_img_src = ApiFlashAPI::prepare_data_for_api( $post_url );
     223                                    break;
     224                                case "screenshotlayer":
     225                                    $screenshot_img_src = ScreenshotLayerAPI::prepare_data_for_api( $post_url );
     226                                    break;
     227                                case "screenshotmachine":
     228                                    $screenshot_img_src = ScreenShotMachineAPI::prepare_data_for_api( $post_url );
     229                                    break;
     230                                case "screenshotone":
     231                                    $screenshot_img_src = ScreenshotOneAPI::prepare_data_for_api( $post_url );
     232                                    break;
     233                                default:
     234                                    $screenshot_img_src = '';
     235                                    break;
     236                            }
     237                        }
     238                    }
     239                }
     240            }
     241           
     242            if( !empty( $screenshot_img_src ) )
     243            {
     244                $result = [ 'status' => true, 'src' => $screenshot_img_src, 'msg' => '' ];
     245            }
     246            else
     247            {
     248                $result = [ 'status' => false, 'msg' => esc_attr__('Error retrieving URL screenshot', 'tida-url-screenshot') ];
     249            }
     250           
     251            echo wp_json_encode( $result );
     252            wp_die();
     253        }
     254       
     255        /**
     256         * Activate the plugin.
     257         *
     258         * @return void
     259         */
     260        public static function activate()
     261        {
     262            // Do nothing
     263        } // END public static function activate
     264       
     265        /**
     266         * Deactivate the plugin.
     267         *
     268         * @return void
     269         */
     270        public static function deactivate()
     271        {
     272            // Do nothing
     273        } // END public static function deactivate
     274    } // END class Tida_URL_Screenshot
    150275} // END if(!class_exists('Tida_URL_Screenshot'))
    151276
    152277if(class_exists('Tida_URL_Screenshot'))
    153278{
    154     // instantiate the plugin class
    155     new Tida_URL_Screenshot();
    156 
     279    // instantiate the plugin class
     280    new Tida_URL_Screenshot();
     281   
    157282    register_activation_hook( __FILE__, array( 'Tida_URL_Screenshot', 'activate' ) );
    158283    register_deactivation_hook( __FILE__, array( 'Tida_URL_Screenshot', 'deactivate' ) );
    159284}
    160 
    161 
    162 
  • tida-url-screenshot/trunk/uninstall.php

    r2758772 r3228575  
    99    exit();
    1010
    11 // delete general options
    12 delete_option('tida_screenshot_input_class');
    13 delete_option('tida_screenshot_button_class');
    14 
    15 // delete api options
    16 delete_option('tida_screenshot_api_service');
    17 delete_option('tida_screenshot_api_token');
    18 
    19 ?>
     11// delete option
     12delete_option('tida_url_screenshot_settings');
Note: See TracChangeset for help on using the changeset viewer.