Skip to content

Commit

Permalink
Issue #1 - prototype oik-loader, oik-loader-mu and oik-loader-map rou…
Browse files Browse the repository at this point in the history
…tines
  • Loading branch information
bobbingwide committed Mar 5, 2019
0 parents commit 9e795fb
Show file tree
Hide file tree
Showing 3 changed files with 259 additions and 0 deletions.
86 changes: 86 additions & 0 deletions oik-loader-map.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

if ( PHP_SAPI !== "cli" ) {
die();
}

/**
* Create the oik-loader map file for oik-loader-MU
* in the mu-plugins folder
*
* It can either be a PHP file or a CSV or something
*
*
* Syntax: oikwp oik-loader.php url=blocks.wp-a2z.org
*
*
*/

function oik_loader_csv_file() {
return WPMU_PLUGIN_DIR . '/oik-loader.csv';
}
function oik_loader_map() {
$csv = oik_loader_csv_file();
echo "Updating " . $csv;
echo PHP_EOL;

oik_loader_map_block_CPT();



}

function oik_loader_get_scheme_host() {

$siteurl = site_url( null, "https");
echo $siteurl;
$host = parse_url( $siteurl, PHP_URL_HOST );
$scheme_host = "https://" . $host;
return $scheme_host;
}

function oik_loader_get_hostless_permalink( $post_id, $scheme_host ) {
$permalink = get_permalink( $post_id );
$permalink = str_replace( $scheme_host, "", $permalink );
return $permalink;
}

function oik_loader_query_plugin_name( $post_id ) {
$plugin_name = null;
$plugin_id = get_post_meta( $post_id, "_oik_sc_plugin", true );
if ( $plugin_id ) {
$plugin_name = get_post_meta( $plugin_id, "_oikp_name", true );
}
return $plugin_name;
}

/**
* Map block CPTs
*/

function oik_loader_map_block_CPT() {
oik_require( "includes/bw_posts.php");
$atts = array( "post_type" => "block",
"post_parent" => 0,
"number_posts" => -1
);
$posts = bw_get_posts( $atts );
$scheme_host = oik_loader_get_scheme_host();
$csvs = [];
foreach ( $posts as $post ) {
$line = [];
$line[] = oik_loader_get_hostless_permalink( $post->ID, $scheme_host );
$line[] = $post->ID;
$line[] = oik_loader_query_plugin_name( $post->ID );
$csv_line = implode( ",", $line );
$csvs[] = $csv_line . PHP_EOL;
}
oik_loader_write_csv_file( $csvs );
}

function oik_loader_write_csv_file( $csvs ) {
$csv_file = oik_loader_csv_file();
file_put_contents( $csv_file, $csvs );
}


123 changes: 123 additions & 0 deletions oik-loader-mu.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<?php
/*
Plugin Name: oik-loader-MU
Plugin URI: https://www.oik-plugins.com/oik-plugins/oik-loader-mu
Description: WordPress Must Use plugin to load required plugins
Version: 0.0.0
Author: bobbingwide
Author URI: https://www.oik-plugins.com/author/bobbingwide
License: GPL2
Copyright 2019 Bobbing Wide (email : herb@bobbingwide.com )
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2,
as published by the Free Software Foundation.
You may NOT assume that you can use any other version of the GPL.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
The license for this software can likely be found here:
http://www.gnu.org/licenses/gpl-2.0.html
*/


$uri = $_SERVER[ 'REQUEST_URI'];
$path = parse_url( $uri, PHP_URL_PATH );
//print_r( $page) ;

$oik_loader_csv = dirname( __FILE__ ) . "/oik-loader.csv";
if ( file_exists( $oik_loader_csv) ) {
//echo "File exists";
$lines = file( $oik_loader_csv );
if ( count( $lines )) {
$index = oik_loader_build_index( $lines );
$plugin = oik_loader_mu_query_plugin( $index, $path );
if ( null !== $plugin ) {
oik_loader_load_plugin( $plugin );
add_filter( "option_active_plugins", "oik_loader_option_active_plugins", 10, 2 );
}
}
} else {
// echo "No file exists";
// Do nothing.

}

/**
* Builds the lookup index for access by URI or post ID
*
* Post ID will be required when editing the post or server rendering in the REST API
*
* @param $lines
*
* @return array
*/
function oik_loader_build_index( $lines ) {
$index = [];
foreach ( $lines as $line ) {
$csv = str_getcsv( $line);
if ( count( $csv) === 3 ) {
//echo $csv[0];
$index[ $csv[0]] = $csv[2];
$index[ $csv[1]] = $csv[2];
}
}
//print_r( $index );
return $index;
}

/**
* Returns the plugin name for the current block CPT
* @param $index
* @param $page
*
* @return null
*/
function oik_loader_mu_query_plugin( $index, $page ) {
$plugin = null;
if ( isset( $index[ $page ])) {
$plugin = $index[ $page ];
}
//echo "$" . $plugin;
return $plugin;
}


/**
* Implements 'option_active_plugins' filter
*
* Adds the missing plugin(s) to the list of plugins to load
*
* @param $active_plugins
* @param $option
*
* @return array
*/
function oik_loader_option_active_plugins( $active_plugins, $option ) {
//print_r( $active_plugins );
$load_plugin = oik_loader_load_plugin();
$active_plugins[] = $load_plugin;
return $active_plugins;

}

/**
* Sets / gets the name of the plugin(s) to load
*
* @param null $plugin
*
* @return null
*/
function oik_loader_load_plugin( $plugin=null ) {
static $load_plugin = null;
if ( $plugin !== null ) {
$load_plugin = $plugin;
}
return $load_plugin;
}
50 changes: 50 additions & 0 deletions oik-loader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
/*
Plugin Name: oik-loader
Plugin URI: https://www.oik-plugins.com/oik-plugins/oik-loader
Description: WordPress plugin to dynamically load required plugins for blocks
Version: 0.0.0
Author: bobbingwide
Author URI: https://www.oik-plugins.com/author/bobbingwide
Text Domain: oik-loader
Domain Path: /languages/
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
Copyright 2019 Bobbing Wide (email : herb@bobbingwide.com )
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2,
as published by the Free Software Foundation.
You may NOT assume that you can use any other version of the GPL.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
The license for this software can likely be found here:
http://www.gnu.org/licenses/gpl-2.0.html
*/

function oik_loader_loaded() {

add_action( "run_oik-loader.php", "oik_loader_run_oik_loader");

}


/**
* Generate the oik-loader.map file for oik-loader-MU
*
*/
function oik_loader_run_oik_loader() {
oik_require( "oik-loader-map.php", "oik-loader");
oik_loader_map();

}


oik_loader_loaded();

0 comments on commit 9e795fb

Please sign in to comment.