oop - How to structure a plugin into multiple files using classes?

I am writing my first simple plugin from scratch so I'm looking for a bit of advice on structuring the classes with

I am writing my first simple plugin from scratch so I'm looking for a bit of advice on structuring the classes within the plugin. I encapsulated some basic functions in a class in the main plugin file but I guess it will become quite ungainly with a lot of functions in a single file.

The structure of my plugin directory so far looks like this:

inc
  -core.php
  -plugin-settings.php
css
  -style.css
lang
plugin-file.php

One thing that I have struggled with is structuring plugin into multiple files using classes.

The code of main plugin file is:

<?php
/**
* Plugin Name: Plugin name
* Plugin URI: 
* Description: Some description
* Version: 1.0
* Author: Me
* Author URI: 
*/
define( 'SBP_DIR', plugin_dir_path( __FILE__ ) );   // Defining plugin dir path.
/*----------------------------------------------
    Including Files
----------------------------------------------*/
include(SBP_DIR_DIR . 'inc/core.php');              // main plugin functions
include(SBP_DIR_DIR . 'inc/plugin-settings.php');   // the plugin options page HTML
/*----------------------------------------------
    Class Dummy_Class
----------------------------------------------*/
global $sbp_class;
if ( !class_exists("Dummy_Class") ) {
    class Dumy_Class {
/*----------------------------------------------
    Function Construct
----------------------------------------------*/
        function __construct() {
            $this->path = plugin_basename(__FILE__);
            add_action( 'init', array( $this, 'init' ) );
            add_action('admin_enqueue_scripts',  array( $this, 'enqueue' ) );
            add_filter("plugin_action_links_$this->path", array( $this, 'sbp_settings_link' ) );
        }
/*----------------------------------------------
    Function init
----------------------------------------------*/
        function init() {
            load_plugin_textdomain( 'sb-pack', false, basename( dirname( __FILE__ ) ) . '/lang' );  // load plugin textdomain
        }
/*----------------------------------------------
    CSS style of the plugin options page
----------------------------------------------*/
        function enqueue($hook) {
            global $sbp_settings_page;
            if ( $hook != $sbp_settings_page )  // load stylesheet only on plugin option page
                return;
            wp_enqueue_style('styles', plugin_dir_url( __FILE__ ) . 'css/styles.css');
        }
/*----------------------------------------------
    Add settings link on plugins page
----------------------------------------------*/
        function sbp_settings_link($links) {
            $settings_link = '<a href="options-general.php?page=sbp-options">Settings</a>';
            array_unshift($links, $settings_link);
            return $links;
        }

    }   //End class Dummy_Class
    $sb_pack = new Dummy_Class; // instantiate the plugin class
}   //End if (!class_exists("Dummy_Class"))

I want to mention that core.php file contains what the plugin does, ie some several functions.

My questions are:

  1. How should be included the core.php and plugin-settings.php files within the main plugin file?
  2. Those files should have unique classes too? How YOU would structure them and why?

I do not have in-depth knowledge of OOP so I need a little bit of step by step instruction.

Any thoughts would be appreciated.

I am writing my first simple plugin from scratch so I'm looking for a bit of advice on structuring the classes within the plugin. I encapsulated some basic functions in a class in the main plugin file but I guess it will become quite ungainly with a lot of functions in a single file.

The structure of my plugin directory so far looks like this:

inc
  -core.php
  -plugin-settings.php
css
  -style.css
lang
plugin-file.php

One thing that I have struggled with is structuring plugin into multiple files using classes.

The code of main plugin file is:

<?php
/**
* Plugin Name: Plugin name
* Plugin URI: http://someurl
* Description: Some description
* Version: 1.0
* Author: Me
* Author URI: http://someurl
*/
define( 'SBP_DIR', plugin_dir_path( __FILE__ ) );   // Defining plugin dir path.
/*----------------------------------------------
    Including Files
----------------------------------------------*/
include(SBP_DIR_DIR . 'inc/core.php');              // main plugin functions
include(SBP_DIR_DIR . 'inc/plugin-settings.php');   // the plugin options page HTML
/*----------------------------------------------
    Class Dummy_Class
----------------------------------------------*/
global $sbp_class;
if ( !class_exists("Dummy_Class") ) {
    class Dumy_Class {
/*----------------------------------------------
    Function Construct
----------------------------------------------*/
        function __construct() {
            $this->path = plugin_basename(__FILE__);
            add_action( 'init', array( $this, 'init' ) );
            add_action('admin_enqueue_scripts',  array( $this, 'enqueue' ) );
            add_filter("plugin_action_links_$this->path", array( $this, 'sbp_settings_link' ) );
        }
/*----------------------------------------------
    Function init
----------------------------------------------*/
        function init() {
            load_plugin_textdomain( 'sb-pack', false, basename( dirname( __FILE__ ) ) . '/lang' );  // load plugin textdomain
        }
/*----------------------------------------------
    CSS style of the plugin options page
----------------------------------------------*/
        function enqueue($hook) {
            global $sbp_settings_page;
            if ( $hook != $sbp_settings_page )  // load stylesheet only on plugin option page
                return;
            wp_enqueue_style('styles', plugin_dir_url( __FILE__ ) . 'css/styles.css');
        }
/*----------------------------------------------
    Add settings link on plugins page
----------------------------------------------*/
        function sbp_settings_link($links) {
            $settings_link = '<a href="options-general.php?page=sbp-options">Settings</a>';
            array_unshift($links, $settings_link);
            return $links;
        }

    }   //End class Dummy_Class
    $sb_pack = new Dummy_Class; // instantiate the plugin class
}   //End if (!class_exists("Dummy_Class"))

I want to mention that core.php file contains what the plugin does, ie some several functions.

My questions are:

  1. How should be included the core.php and plugin-settings.php files within the main plugin file?
  2. Those files should have unique classes too? How YOU would structure them and why?

I do not have in-depth knowledge of OOP so I need a little bit of step by step instruction.

Any thoughts would be appreciated.

Share Improve this question edited May 10, 2014 at 18:11 Knott asked May 10, 2014 at 17:09 KnottKnott 4742 gold badges7 silver badges27 bronze badges 1
  • Well, it starts with: Which PHP version do you want to support? Do you want to use an autoloader? What helpers do you want to add to your stack? Composer? etc. etc. I'm not really sure how to answer that without a series of blog articles... – kaiser Commented May 10, 2014 at 19:59
Add a comment  | 

1 Answer 1

Reset to default 6

Some basic rules for object organization in a plugin.

  • One class declaration per file, no other code in that file. The creation of a new instance – new Dummy_Class – should not be in the same file as the class declaration.

  • Everything that is sending data to the user (presentation) should get its own class: HTML, CSV, XML output or HTTP headers.

  • Every algorithm, every data source should get its own class, so you can change it later. Lets say, you store some data in an option and want change that later to use a custom database table: then you should be able to do that without changing all files.

  • Registration and creation of objects belongs to a separate class (controller). add_action( 'something', array( $this, 'callback' ) ) is usually a sign of bad design. Use a separate object instead of $this.

  • Avoid global constants, they clutter up the global namespace. Just pass the path of the current plugin file to the main controller.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745072874a4609657.html

相关推荐

  • oop - How to structure a plugin into multiple files using classes?

    I am writing my first simple plugin from scratch so I'm looking for a bit of advice on structuring the classes with

    15小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信