Plugin Directory

Changeset 3327925


Ignore:
Timestamp:
07/15/2025 06:06:38 AM (8 months ago)
Author:
ikaring
Message:

Refactor code

Location:
get-filesize-shortcode/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • get-filesize-shortcode/trunk/get-filesize-shortcode.php

    r3012748 r3327925  
    44 * Plugin URI:  http://ika-ring.net
    55 * Description: Simple shortcode to get filesize of a file( eg. PDF, JPG, PNG ... ) with a human readable format, using the largest unit the bytes will fit into. Now added Get filesize block.
    6  * Version:     1.2.0
     6 * Version:     1.3.0
    77 * Author:      Kan Ikawa
    88 * Author URI:  https://ika-ring.net
     
    1212 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
    1313 */
    14 
     14 
     15// セキュリティチェック
    1516if ( ! defined( 'ABSPATH' ) ) {
    16   exit; // Exit if accessed directly.
     17    exit; // Exit if accessed directly.
    1718}
    1819
    19 // Usage: [filesize]http://wordpress.com/path/to/filename.pdf[/filesize]
     20/**
     21 * プラグインのメインクラス
     22 */
     23class Get_Filesize_Shortcode {
     24   
     25    /**
     26     * プラグインの初期化
     27     */
     28    public static function init() {
     29        // 言語ファイルの読み込み
     30        add_action( 'plugins_loaded', array( __CLASS__, 'load_textdomain' ) );
     31       
     32        // ショートコードの登録
     33        add_shortcode( 'filesize', array( __CLASS__, 'filesize_shortcode' ) );
     34       
     35        // ブロックの初期化
     36        add_action( 'init', array( __CLASS__, 'register_block' ) );
     37    }
     38   
     39    /**
     40     * 言語ファイルの読み込み
     41     */
     42    public static function load_textdomain() {
     43        load_plugin_textdomain(
     44            'get-filesize-shortcode',
     45            false,
     46            basename( dirname( __FILE__ ) ) . '/languages'
     47        );
     48    }
     49   
     50    /**
     51     * ファイルサイズ取得のショートコード
     52     *
     53     * @param array $atts ショートコードの属性
     54     * @param string $content ショートコードの内容
     55     * @return string ファイルサイズまたはエラーメッセージ
     56     */
     57    public static function filesize_shortcode( $atts, $content = null ) {
     58        // 属性のデフォルト値とサニタイズ
     59        $atts = shortcode_atts( array(
     60            'url' => '',
     61        ), $atts, 'filesize' );
     62       
     63        // URLの取得(contentが優先)
     64        $url = ! empty( $content ) ? trim( $content ) : $atts['url'];
     65       
     66        // URLが空の場合はエラー
     67        if ( empty( $url ) ) {
     68            return __( 'No file URL specified!', 'get-filesize-shortcode' );
     69        }
     70       
     71        // URLのサニタイズ
     72        $url = esc_url_raw( strip_tags( $url ) );
     73       
     74        // ファイルサイズの取得
     75        return self::get_file_size( $url );
     76    }
     77   
     78    /**
     79     * ファイルサイズを取得する
     80     *
     81     * @param string $url ファイルのURL
     82     * @return string ファイルサイズまたはエラーメッセージ
     83     */
     84    private static function get_file_size( $url ) {
     85        // サイトURLからローカルパスに変換
     86        $site_url = trailingslashit( site_url() );
     87        $path = str_replace( $site_url, ABSPATH, $url );
     88       
     89        // パスのサニタイズ(ディレクトリトラバーサル攻撃対策)
     90        $path = realpath( $path );
     91       
     92        // パスの検証
     93        if ( ! $path || ! self::is_allowed_path( $path ) ) {
     94            return __( 'File path is not allowed!', 'get-filesize-shortcode' );
     95        }
     96       
     97        // ファイルの存在確認
     98        if ( ! is_file( $path ) ) {
     99            return __( 'File not found!', 'get-filesize-shortcode' );
     100        }
     101       
     102        // ファイルサイズの取得
     103        $filesize = filesize( $path );
     104        if ( $filesize === false ) {
     105            return __( 'Unable to get file size!', 'get-filesize-shortcode' );
     106        }
     107       
     108        // 人間が読みやすい形式に変換
     109        return size_format( $filesize );
     110    }
     111   
     112    /**
     113     * パスが許可されているかチェック
     114     *
     115     * @param string $path ファイルパス
     116     * @return bool
     117     */
     118    private static function is_allowed_path( $path ) {
     119        // ABSPATHの範囲内かチェック
     120        $abspath = realpath( ABSPATH );
     121        if ( strpos( $path, $abspath ) !== 0 ) {
     122            return false;
     123        }
     124       
     125        // 許可されたディレクトリのリスト
     126        // $allowed_dirs = array(
     127        //     'wp-content/uploads',
     128        //     'wp-content/themes',
     129        //     'wp-content/plugins',
     130        // );
     131       
     132        // 許可されたディレクトリ内かチェック
     133        // foreach ( $allowed_dirs as $dir ) {
     134        //     $allowed_path = realpath( ABSPATH . $dir );
     135        //     if ( $allowed_path && strpos( $path, $allowed_path ) === 0 ) {
     136        //         return true;
     137        //     }
     138        // }
     139       
     140        //return false;
     141        return true;
     142    }
     143   
     144    /**
     145     * ブロックの登録
     146     */
     147    public static function register_block() {
     148        if ( ! function_exists( 'register_block_type' ) ) {
     149            return;
     150        }
     151       
     152        $block_path = __DIR__ . '/build';
     153        if ( is_dir( $block_path ) ) {
     154            register_block_type( $block_path );
     155        }
     156    }
     157}
    20158
    21 function ika_get_filesize( $atts, $content = null ) {
    22   // i18n
    23   load_plugin_textdomain(
    24     'get-filesize-shortcode',
    25     false,
    26     basename( dirname( __FILE__ ) ) .'/languages'
    27   );
    28  
    29   extract( shortcode_atts( array(
    30     'url' => '',
    31   ), $atts ) );
    32  
    33   //Replace url to directory path
    34   if ( empty( $content ) ) {
    35     $path = str_replace( site_url('/'), ABSPATH, strip_tags( $url ) );
    36   } else {
    37     $path = str_replace( site_url('/'), ABSPATH, strip_tags( $content ) );
    38   }
    39  
    40   if ( is_file( $path ) ){
    41     $filesize = size_format( filesize( $path ) );
    42   } else {
    43     $filesize = __( 'File not found!', 'get_filesize_shortcode');
    44   }
    45   return $filesize;
    46 
    47 }
    48 add_shortcode('filesize', 'ika_get_filesize');
    49 
     159// プラグインの初期化
     160Get_Filesize_Shortcode::init();
    50161
    51162/**
    52  * Registers the block using the metadata loaded from the `block.json` file.
    53  * Behind the scenes, it registers also all assets so they can be enqueued
    54  * through the block editor in the corresponding context.
    55  *
    56  * @see https://developer.wordpress.org/reference/functions/register_block_type/
     163 * プラグインの無効化時の処理
    57164 */
    58 if ( ! function_exists( 'get_filesize_block_get_filesize_block_block_init' ) ) {
    59   function get_filesize_block_get_filesize_block_block_init() {
    60     register_block_type( __DIR__ . '/build' );
    61   }
    62   add_action( 'init', 'get_filesize_block_get_filesize_block_block_init' );
    63 }
     165register_deactivation_hook( __FILE__, function() {
     166    // 必要に応じてクリーンアップ処理を追加
     167});
  • get-filesize-shortcode/trunk/readme.txt

    r3012845 r3327925  
    33Tags: shortcode, filesize, pdf
    44Requires at least: 5.8
    5 Tested up to: 6.4.2
     5Tested up to: 6.8.1
    66Stable tag: trunk
    77Requires PHP: 5.6
Note: See TracChangeset for help on using the changeset viewer.