2017 © Pedro Peláez
 

library wordpress-plugin-base

Wordpress Plugin Base

image

technote/wordpress-plugin-base

Wordpress Plugin Base

  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 16 Versions
  • 0 % Grown

The README.md

This repository is no longer maintained.

Moved to
https://github.com/wp-content-framework/core, (*1)

Wordpress plugin base

Wordpress plugin 開発用のライブラリです。
管理画面やAPIなどの追加や設定値の読み書き等を容易にする機能が用意されています。, (*2)

要件

  • PHP 5.6 以上
  • WordPress 3.9.3 以上

手順

プラグインフォルダの作成

wp-content/plugins フォルダに プラグイン用のフォルダを作成, (*3)

プラグインファイルの作成

作成したプラグインフォルダに「プラグイン名.php」(例:example.php) を作成
標準プラグイン情報
を参考にプラグインの情報を入力, (*4)

このライブラリのインストール

composer を使用してインストールします。
作成したプラグインフォルダで以下のコマンドを実行します。, (*5)

composer require technote/wordpress-plugin-base, (*6)

 
複数のプラグインでこのライブラリを使用する場合、最新のものが自動的に使用されます。, (*7)

このライブラリの使用

作成したプラグインファイルにライブラリを使用する記述を追記します。
プラグインファイルはおおよそ以下のようなものになります。, (*8)

 設定
'test' => array(
    
    // primary key 設定
    'id'      => 'test_id',     // optional [default = $table_name . '_id']
    
    // カラム 設定
    'columns' => array(
    
        // 論理名 => 設定
        'name'   => array(
            'name'     => 'name_test',     // optional (物理名)
            'type'     => 'VARCHAR(32)',   // required
            'unsigned' => false,          // optional [default = false]
            'null'     => true,           // optional [default = true]
            'default'  => null,           // optional [default = null]
            'comment'  => '',             // optional
        ),
        'value1' => array(
            'type'    => 'VARCHAR(32)',
            'null'    => false,
            'default' => 'test',
        ),
        'value2' => array(
            'type'    => 'VARCHAR(32)',
            'comment' => 'aaaa',
        ),
        'value3' => array(
            'type'    => 'INT(11)',
            'null'    => false,
            'comment' => 'bbb',
        ),
    ),
    
    // index 設定
    'index'   => array(
        // key index
        'key'    => array(
            'name' => array( 'name' ),
        ),
        
        // unique index
        'unique' => array(
            'value' => array( 'value1', 'value2' ),
        ),
    ),
    
    // 論理削除 or 物理削除
    'delete'  => 'logical', // physical or logical [default = physical]
),
```

設定を更新したら configs/config.php の db_version も更新します。  
自動でテーブルの追加・更新が行われます。  
データの取得・挿入・更新・削除は以下のように行います。
```
// 取得
$this->app->db->select( 'test', array(
    'id'         => array( 'in', array( 1, 2, 3 ) ),
    'value1'     => array( 'like', 'tes%' ),
    'created_at' => array( ' null,
    'value3'     => 3,
) );

// 挿入
$this->app->db->insert( 'test', array(
    'name'   => 'aaa',
    'value1' => 'bbb',
    'value3' => 100,
) );

// 更新
$this->app->db->update( 'test', array(
    'value2' => 'ccc',
), array(
    'id' => 4,
) );

// 削除
$this->app->db->delete( 'test', array(
    'id' => 4,
) );
```
select 以外は 内部でWordPress標準の関数を使用しているため、  
条件の指定の仕方は 'key' => 'value' (key = value) のみ可能です。  
select の条件指定はライブラリ側で構築しており、  
key = value  
```
key' => 'value'
```
key in ( val1, val2, val3 )
```
'key' => array( 'in', array( val1, val2, val3 ) )  
```
key like '%value%'
```
'key' => array( 'like', '%value%' )
```
などが指定可能です。

- configs/setting.php

設定例:
```
// priority => 詳細
'10' => array(

    // 設定グループ => 詳細
    'Performance' => array(
    
        // priority => 詳細
        '10' => array(
        
            // 設定名 => 詳細
            'minify_js'  => array(
                // 説明
                'label'   => 'Whether to minify js which generated by this plugin',
                // タイプ (bool or int or float or string)
                'type'    => 'bool', // [default = string]
                // デフォルト値
                'default' => true,
            ),
            'minify_css' => array(
                'label'   => 'Whether to minify css which generated by this plugin',
                'type'    => 'bool',
                'default' => true,
            ),
        ),
    ),
),
```

設定ページで設定可能になります。  
プログラムで使用するには以下のようにします。
```
$this->apply_filters( 'minify_js' ) // true or false

if ( $this->apply_filters( 'minify_js' ) ) {
    // ...
}
```

- configs/filter.php  
今後ドキュメント追加予定

- configs/slug.php  
今後ドキュメント追加予定

- configs/capability.php  
今後ドキュメント追加予定

## 画面の追加

- src/classes/controllers/admin に PHP ファイル (例:test.php) を追加
```
app->input->post( 'aaa' );
        // ... 
    }

    // GET, POST 共通で行う動作
    protected function common_action() {
        // wp_enqueue_script('media-upload');
    }

    // view に渡す変数設定
    public function get_view_args() {
        return array(
            'test' => 'aaaa',
        );
    }
}
```

POST の時に行う動作は事前にnonce checkが行われます。

- src/views/admin に PHP ファイル (例:test.php) を追加
```


form( 'open', $args ); ?>

h( $test ); ?>
form( 'input/submit', $args, array(
    'name'  => 'update',
    'value' => 'Update',
    'class' => 'button-primary'
) ); ?>

form( 'close', $args ); ?>
  • $instance, (*9)

    • h:esc_html
    • dump:var_dump
    • id
    • form
    • url
    • img
  • ヘルプの追加, (*10)

    • src/classes/controllers/admin に追加した上記 PHP ファイル に以下を追記
protected function get_help_contents() {
    return array(
        array(
            'title' => 'Test',
            'view'  => 'test',
        )
    );
}

- - src/views/admin/help に PHP ファイル (例:test.php) を追加, (*11)



test

API の追加

今後ドキュメント追加予定, (*12)

filter の追加

今後ドキュメント追加予定, (*13)

cron の追加

今後ドキュメント追加予定, (*14)

テストの追加

  • PHPUnitの追加
    composer require --dev phpunit/phpunit, (*15)

  • src/classes/tests に PHP ファイル (例:sample.php) を追加, (*16)

<?php

namespace Example\Classes\Tests;

if ( ! defined( 'TECHNOTE_PLUGIN' ) ) {
    exit;
}

/**
 * Class Sample
 * @package Example\Classes\Tests
 */
class Sample extends \Technote\Classes\Tests\Base {

    public function test_sample1() {
        $this->assertEquals( 2, 1 + 1 );
    }

    public function test_sample2() {
        $this->assertEquals( 1, 1 + 1 );
    }

}
  • 管理画面から実行

test1 test2, (*17)

Author

technote-space, (*18)

The Versions

27/07 2018
12/07 2018
22/06 2018
22/06 2018

dev-feature/test

dev-feature/test https://github.com/technote-space

Wordpress Plugin Base

  Sources   Download

GPL-2.0+

The Requires

 

The Development Requires

plugin wordpress

06/06 2018

1.0.4

1.0.4.0 https://github.com/technote-space

Wordpress Plugin Base

  Sources   Download

GPL-2.0+

The Requires

 

The Development Requires

plugin wordpress

06/06 2018

1.0.3

1.0.3.0 https://github.com/technote-space

Wordpress Plugin Base

  Sources   Download

GPL-2.0+

The Requires

 

The Development Requires

plugin wordpress

04/06 2018

1.0.2

1.0.2.0 https://github.com/technote-space

Wordpress Plugin Base

  Sources   Download

GPL-2.0+

The Requires

 

plugin wordpress

04/06 2018

1.0.1

1.0.1.0 https://github.com/technote-space

Wordpress Plugin Base

  Sources   Download

GPL-2.0+

The Requires

 

plugin wordpress

04/06 2018

1.0.0

1.0.0.0 https://github.com/technote-space

Wordpress Plugin Base

  Sources   Download

GPL-2.0+

The Requires

 

plugin wordpress