Ik ben bezig om een plug-in van Wordpress op te schonen door middel van namespaces toe te voegen.
Dit in verschillende mapjes (Ik heb hier voor de verduidelijking een mappenstructuur onder gezet :)).
Het probleem is dat de namespace / Klasse nu niet geladen wordt.
<?php
Fatal error: Uncaught Error: Class 'controller\register\new_register_post_type' not found in C:\xampp\htdocs\dev\authserver\webroot\wp-content\plugins\WPAUTH2\WPAUTH2.php:20 Stack trace: #0 C:\xampp\htdocs\dev\authserver\webroot\wp-settings.php(305): include_once() #1 C:\xampp\htdocs\dev\authserver\webroot\wp-config.php(89): require_once('C:\\xampp\\htdocs...') #2 C:\xampp\htdocs\dev\authserver\webroot\wp-load.php(37): require_once('C:\\xampp\\htdocs...') #3 C:\xampp\htdocs\dev\authserver\webroot\wp-admin\admin.php(31): require_once('C:\\xampp\\htdocs...') #4 C:\xampp\htdocs\dev\authserver\webroot\wp-admin\index.php(10): require_once('C:\\xampp\\htdocs...') #5 {main} thrown in C:\xampp\htdocs\dev\authserver\webroot\wp-content\plugins\WPAUTH2\WPAUTH2.php on line 20
?>
In de WPAUTH.php wordt de plug-in aangemaakt en main.php geladen.
<?php
/*
Plugin Name: WPAUTH2
Plugin URI: https://xxx
Description: AUTH SERVER
Version: 1.2
Author: Daan Seegers
Author URI: https://xxx
License: GPLv2 or later
*/
require_once 'Classes/main.php';
ini_set('display_startup_errors', 1);
ini_set('display_errors', 1);
error_reporting(-1);
use controller\register\new_register_post_type;
$class = new new_register_post_type();
?>
In de main.php wordt een autloader geladen, die de klasse weer op zou moeten laden.
<?php
function __autoload($class)
{
$parts = explode('\\', $class);
require end($parts) . '.php';
}
?>
Nu probeer ik de klasse new_register_post_type op te halen.
Deze zit in de namespace controller\register.
In de new_register_post_type.php staat de code om een post te registreren, alleen hiervoor krijg ik de melding dat deze niet geladen kan worden
<?php
/**
* Register taxonomies
*/
/**
* Add meta box
*
* @param post $post The post object
* @link https://codex.wordpress.org/Plugin_API/Action_Reference/add_meta_boxes
*/
namespace controller\register;
class new_register_post_type
{
private $name;
private $slug;
public function __construct($name, $slug)
{
$this->name = $name;
$this->slug = $slug;
add_action('init', [$this, 'API_setup']);
}
public function API_setup($name)
{
$labels = array(
'name' => __($this->name, $this->name . '_example_plugin'),
'singular_name' => __($this->name, $this->name . '_example_plugin'),
'add_new_item' => __('Add New ' . $this->name, $this->name . '_example_plugin'),
'edit_item' => __('Edit ' . $this->name, $this->name . '_example_plugin'),
'new_item' => __('New ' . $this->name, $this->name . '_example_plugin'),
'not_found' => __('No ' . $this->name . ' found', $this->name . '_example_plugin'),
'all_items' => __('All ' . $this->name, $this->name . '_example_plugin'),
);
$args = array(
'labels' => $labels,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'has_archive' => true,
'map_meta_cap' => true,
'menu_icon' => 'dashicons-performance',
'supports' => array('title'),
);
register_post_type($this->slug, $args);
}
}
?>