Skip to content

Instantly share code, notes, and snippets.

@rgadon107

rgadon107/api.php

Last active Jan 5, 2018
Embed
What would you like to do?
reusable-metabox-plugin
<?php
/**
* Public API to interact with the ConfigStore.
*
* @package spiralWebDB\ConfigStore;
*
* @since 1.0.0
*
* @author Robert A. Gadon
*
* @link http://spiralwebdb.com
*
* @license GNU General Public License 2.0+
*/
namespace spiralWebDB\ConfigStore;
/**
* Load (set) the configuration into the store from the
* absolute path to the configuration file.
*
* @since 1.0.0
*
* @param string $path_to_file Absolute path to the config file.
*/
function loadConfigFromFilesystem( $path_to_file ) {
list( $store_key, $config) = _load_config_from_filesystem( $path_to_file );
return _the_store( $store_key, $config )
}
/**
* Get a specific configuration from the store.
*
* @since 1.0.0
*
* @param string $store_key
*
* @return mixed
*/
function getConfig( $store_key ) {
return _the_store( $store_key );
}
<?php
/**
* Meta Box Reusable WordPress Plugin
*
* @package MetaBox
* @author r_gadon
* @license GPL-2.0+
*
* @wordpress-plugin
* Plugin Name: Meta Box Reusable WordPress Plugin
* Description: Reusable meta box plugin to add a custom meta box to our sandbox.
* Version: 1.0.0
* Author: Robert Gadon
* Author URI: https://spiralwebdb.com
* Text Domain: reusable_metabox
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
*/
namespace spiralWebDB\MetaBox;
if ( ! defined( 'ABSPATH' ) ) {
exit( 'Cheatin&#8217; uh?' );
}
/**
* Setup the plugin's constants.
*
* @since 1.0.0
*
* @return void
*/
function init_constants() {
$plugin_url = plugin_dir_url( __FILE__ );
if ( is_ssl() ) {
$plugin_url = str_replace( 'http://', 'https://', $plugin_url );
}
define( 'METABOX_URL', $plugin_url );
define( 'METABOX_DIR', plugin_dir_path( __FILE__ ) );
}
/**
* Launch the plugin
*
* @since 1.0.0
*
* @return void
*/
function launch() {
init_constants();
require __DIR__ . '/src/config-store/module.php';
$key = \spiralWebDB\ConfigStore\loadConfigFromFilesystem( __DIR__ . '/config/portfolio.php' );
d( \spiralWebDB\ConfigStore\getConfig( $key ) );
$key = \spiralWebDB\ConfigStore\loadConfigFromFilesystem( __DIR__ . '/config/subtitle.php' );
ddd( \spiralWebDB\ConfigStore\getConfig( $key ) );
}
launch();
<?php
/**
* Configuration Store Internal Functionality (Private)
*
* @package spiralWebDB\ConfigStore
*
* @since 1.0.0
*
* @author Robert A. Gadon
*
* @link http://spiralwebdb.com
*
* @license GNU General Public License 2.0+
*/
namespace spiralWebDB\ConfigStore;
/**
* Store for runtime configuration parameters.
*
* @since 1.0.0
*
* @param string $store_key
* @param array $config_to_store
*
* @return void
*/
function _the_store( $store_key, $config_to_store = array() ) {
static $config_store = array();
// Store
if ( $config_to_store ) {
$config_store[ $store_key ] = $config_to_store;
return true;
}
return $config_store[$store_key];
// Get
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.