“Class Not Found” in HTML Form Target PHP File that contains the class

Question

How do I make the HTML form’s action target file ‘aware’ of classes that WordPress has already loaded?

File A -> Creates a WordPress shortcode that inserts a form that has ‘File B’ as the target.
File B goes to process the submitted data, but throws “Class Not Found” whenever I try to instantiate an object of the class that contains the function responsible for processing the data.

Code:

File A Psudeocode (works)

add_action('plugin_loaded',array('wordpress_plugin_bootstrap','load'),5);
class wordpress_plugin_bootstrap{

   public static function load() {
     //check for required class
     require_once('FileB.php'); //contains the class 'PluginClass'
     //register the class 
   }

}

function class_instance_getter(){
    return PluginClass::get_instance():
}

function create_html_form($atts){
//creates and returns the form with action attribute set to 'FileB.php'
}
add_shortcode('ju_lee_do_the_thing','create_html_form');

File B Pseudocode

REQUIREDCLASS::include_addon_framework();

class PluginClass extends OtherClass {

        //init stuff

        private static $_instance = null;

        public static function get_instance(){
            if ( self::$_instance == null){
                 self::$_instance = new PluginClass();
            }
            return self::$_instance;
        }

        public function init(){
            parent::init();
            add_action('wp_loaded',array($this,'handle_form')); //me, trying desperately to get this thing to hold it's fire until everything is loaded
        }

        public static function handle_form($postArray){
            //do stuff with the data using the REQUIREDCLASS
        }
    
} // End of class code

$obj = new PluginClass; // Blows up here
$obj->handle_form($_POST);

Error

Fatal error: Uncaught Error: Class ‘PluginClass’ not found in /../../FileB.php on line [‘blows up here’]

It dawned on me that when execution ‘drops in’ to the HTML form action target file (whether that’s in FileB or elsewhere) it doesn’t ‘know’ what WordPress has (or hasn’t) loaded; I can observe everything loading and registering correctly by debugging FileA on a page refresh.

So the questions become these:

Do I need to do something to make WP ‘aware’ of my action target file?

Should I actually be re-loading everything I need upon code entry of
my target action file? Even if that means reloading an entire WP
plugin (this seems wrong to me)?

While were on the subject…a HTML form action target is kind of
acting like a callback handler (once the user hits submit, dial this
number sort of thing), but it isn’t technically – so what is it? This
will greatly inform my Google searching.
Also, I’ve tried moving all of the form_handler code to another file (uh…’FileC’ ) which sort of works in that I can actually get into the handler code which works great until I need to reference the REQUIREDCLASS…then it just blows up there instead.

I think I’m about 20 hours in, give or take, in trying to figure this out. Feel free to light up the comments with ‘Stuff about PHP and/or WP I really ought to research’.

Also – a HUGE thank you to anyone that can offer guidance on this. I feel like a cat in a laser pointer store.

0
, , sacredfaith 2 years 2020-08-12T08:10:19-05:00 0 Answers 58 views 0

Leave an answer

Browse
Browse