WPLib::do_the_methods PHP Method

do_the_methods() static public method

The "the_" method can call virtual methods and/or delegate to a view or a model. The view and model can both be the same object if needed.
static public do_the_methods ( string | object $view, string | object $model, string $method_name, array $args ) : mixed
$view string | object
$model string | object
$method_name string
$args array
return mixed
    static function do_the_methods($view, $model, $method_name, $args)
    {
        $value = null;
        $suffix = $has_html_suffix = false;
        if (preg_match('#^the_(.+)_template$#', $method_name, $match)) {
            /*
             * Put the $template name at the beginning of the $args array
             */
            array_unshift($args, str_replace('_', '-', $match[1]));
            /**
             * Now call 'the_template' with $template as first element in $args
             */
            $value = call_user_func_array(array($view, 'the_template'), $args);
            if (preg_match('#^<\\{WPLib:(.+)\\}>#', $value, $match)) {
                /**
                 * Check to see if their is a content type indicator
                 */
                switch ($match[1]) {
                    case 'JSON':
                        $suffix = '_json';
                        break;
                    case 'HTML':
                    default:
                        $suffix = '_html';
                        /*
                         * Indicate that this content need not be run through wp_kses_post()
                         * since it was loaded by a template which can be reviewed for security.
                         */
                        $has_html_suffix = true;
                        break;
                }
            }
        } else {
            if (method_exists($view, $method_name) && is_callable($callable = array($view, $method_name))) {
                /**
                 * Call the view method directly.
                 */
                $value = call_user_func_array($callable, $args);
            } else {
                if (preg_match('#^the_(.+?)(_attr|_url|_html|_link)?$#', $method_name, $match)) {
                    $method_name = $match[1];
                    $suffix = 3 === count($match) ? $match[2] : false;
                    $has_html_suffix = self::has_html_suffix($suffix);
                    if ($callable = self::get_callable($view, "get_{$method_name}{$suffix}")) {
                        /*
                         * Call the $view method: 'get_whatever_suffix()'
                         */
                        $value = call_user_func_array($callable, $args);
                    } else {
                        if ($callable = self::get_callable($view, "{$method_name}{$suffix}")) {
                            /*
                             * Call the $view method: 'whatever_suffix()'
                             */
                            $value = call_user_func_array($callable, $args);
                        } else {
                            if ($callable = self::get_callable($model, "get_{$method_name}{$suffix}")) {
                                $has_html_suffix = self::has_html_suffix($suffix);
                                /*
                                 * Call the $model method: 'get_whatever_suffix()'
                                 */
                                $value = call_user_func_array($callable, $args);
                            } else {
                                if ($callable = self::get_callable($model, "get_{$method_name}")) {
                                    $has_html_suffix = self::has_html_suffix($suffix);
                                    /*
                                     * Call the $model method: 'get_whatever()'
                                     */
                                    $value = call_user_func_array($callable, $args);
                                } else {
                                    if (!$has_html_suffix && ($callable = self::get_callable($model, "{$method_name}{$suffix}"))) {
                                        /*
                                         * Call the $model method: 'whatever_suffix()'
                                         */
                                        $value = call_user_func_array($callable, $args);
                                    } else {
                                        if ($callable = self::get_callable($model, $method_name)) {
                                            $has_html_suffix = false;
                                            /*
                                             * Call the $model method: "{$method_name}" (as passed)
                                             */
                                            $value = call_user_func_array($callable, $args);
                                        } else {
                                            $has_html_suffix = false;
                                            /*
                                             * Not found, throw an error.
                                             * $match[0] should have original $method_name
                                             */
                                            $class_name = is_object($view) ? get_class($view) : $view;
                                            $message = sprintf(__('Method %s not found for class %s.', 'wplib'), $match[0], $class_name);
                                            self::trigger_error($message, E_USER_ERROR);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        /**
         * Auto-escape output
         */
        switch ($suffix) {
            case '_attr':
                echo $value = esc_attr($value);
                break;
            case '_url':
                echo $value = esc_url($value);
                break;
            case '_html':
            case '_link':
                echo $has_html_suffix ? $value : wp_kses_post($value);
                break;
            default:
                echo $value = esc_html($value);
        }
    }

Usage Example

Ejemplo n.º 1
0
 /**
  * Magic method for calling inaccessible methods
  * Examples:
  *  $this->date             Return original ISO 8601 date format from model
  *  $this->get_date()       Return custom formatted date
  *  $this->get_date_html()  Return custom formatted date HTML
  *  $this->the_date()       Output custom formatted date
  *  $this->the_date_html()  Output custom formatted date HTML
  *
  * @param string $method_name
  * @param array  $args
  *
  * @return mixed|null
  */
 function __call($method_name, $args = array())
 {
     $value = null;
     if (false !== strpos($method_name, 'the_')) {
         $value = WPLib::do_the_methods($this, $this->model(), $method_name, $args);
     } else {
         $value = call_user_func_array(array($this->model(), $method_name), $args);
     }
     return $value;
 }
All Usage Examples Of WPLib::do_the_methods