• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

How do I extend the class XenForo_Upload

AndyB

Well-known member
#1
I'm trying to create an add-on which needs to extend the class XenForo_Upload.

This class is located in the following file:

/library/Xenforo/Upload.php

This file executes when an attachment is uploaded.

The problem is my add-on does not work, I expect the die command to echo "got here".

Am I listening to the correct event "load_class"?

Thank you.
 

AndyB

Well-known member
#3
Listener.php

PHP:
<?php

class Andy_ImageRotate_Listener
{
   public static function Upload($class, array &$extend)
   {
     $extend[] = 'Andy_ImageRotate_Upload';
   }
}

?>
 

AndyB

Well-known member
#5
Upload.php

PHP:
<?php

class Andy_ImageRotate_Upload extends XFCP_Andy_ImageRotate_Upload
{
   protected function _checkImageState()
   {
     die( "got here" );
   }
}

?>
 

Daniel Hood

Well-known member
#6
There are some classes that are only called statically and cannot be modified. It seems as if you are attempting to rotate images, if so you may want to just put a button next to the attachments to rotate and do it upon being clicked instead of automatically. I'm sure eventually more code events will be created. It's been brought up a lot lately by developers.
 

Daniel Hood

Well-known member
#8
Well most commonly we just unfortunately stumble upon them. However, you could browse the core files and see where all the code event fires are I guess. Eventually you'll get a hang of how all classes are loaded and understand basically which ones are initiated and which ones are called statically. A short list of the ones off the top of my head that are static are the ones in the root folder (Application, Input, Link, etc) and all the template helpers. Basically load_class just works on the other load_class_* options, it's just more vague.

for example load_class_datawriter could also be executed through load_class. Evidenced here (/library/XenForo/Application.php):

PHP:
public static function resolveDynamicClass($class, $type = '', $fakeBase = false)
.............
XenForo_CodeEvent::fire('load_class', array($class, &$extend), $class);
        if ($type)
        {
            XenForo_CodeEvent::fire('load_class_' . $type, array($class, &$extend), $class);
        }
Some of these can be customized through init_dependencies and other types, but it's an entirely different process and it's not technically extending.
 
Last edited:

AndyB

Well-known member
#9
A short list of the ones off the top of my head that are static are the ones in the root folder (Application, Input, Link, etc) and all the template helpers.
Upload.php is in the root folder so I suspect it will be called statically. Looks like hacking the Upload.php file directly is the only option for now.

Thank you for taking the time to explain how the load_class is used.
 

Daniel Hood

Well-known member
#10
No problem. You should still possibly try a different method first. Hacking a core file is absolute last resort at all times.

Could you potentially use the controller? Maybe even a DataWriter::_postSave() option? A deferred method? Does it absolutely have to be the XenForo_Upload class?
 

AndyB

Well-known member
#11
No problem. You should still possibly try a different method first. Hacking a core file is absolute last resort at all times.

Could you potentially use the controller? Maybe even a DataWriter::_postSave() option? A deferred method? Does it absolutely have to be the XenForo_Upload class?
I suppose the image could be auto-oriented at some other point, but I haven't looked yet. What I would like to do is create an add-on which performs what this hack does.

PHP:
  /**
    * Checks the state of the upload to determine if it's
    * a valid image.
    */
   protected function _checkImageState()
   {
     
     // start hack   
     
     exec("/usr/bin/mogrify -auto-orient $this->_tempFile");
     
     // end hack