自定义WordPress小部件在PHP 7.2更新后无法正常工作

时间:2018-12-21 13:29:08

标签: wordpress

我们刚刚将WordPress网站更新为PHP7.2,并且以前的开发人员创建的自定义窗口小部件不再显示在管理员中。这真的很奇怪,我不确定是什么引起了问题。我看不到前端的错误。我在这里没有注意什么?在PaperTrail Either中,没有看到任何特定于此小部件的东西。

class ProductCategoryWidget extends WP_Widget {

        protected $_engine;

        /**
         * Sets up the widgets name etc
         */
        public function __construct() {
            // widget actual processes

            $this->_engine = new \League\Plates\Engine( __DIR__ . "/views" );

            parent::__construct('nolan_product_category_widget', 'Product Categories Menu', [
                'description' => ('Widget to Display Menu of Product Categories')
            ]);
        }

        /**
         * Outputs the content of the widget
         *
         * @param array $args
         * @param array $instance
         */
        public function widget( $args, $instance ) {
            // outputs the content of the widget
            echo $args['before_widget'];

            echo $args['before_title'];

            echo $instance['title'];

            echo $args['after_title'];

            $categories = get_terms("nolan_product_category");


            $categories = array_map(function($c){

                $posts_in_category = get_posts([
                    "posts_per_page" => -1,
                    "post_type" => "nolan_product",
                    "tax_query" => [
                        [
                            "taxonomy" => "nolan_product_category",
                            "field" => "term_id",
                            "terms" => $c->term_id
                        ]
                    ]
                ]);


                return ['Testing'];
                // return [$c, $posts_in_category];

            }, $categories);

            // render the menu
            echo $this->_engine->render("product-category-widget/menu", [
                "categories" => $categories
            ]);

            echo $args['after_widget'];
        }

        /**
         * Outputs the options form on admin
         *
         * @param array $instance The widget options
         * @return string|void
         */
        public function form( $instance ) {
            // outputs the options form on admin

            $title = ! empty( $instance['title'] ) ? $instance['title'] : __( 'Product Categories', 'nolan_products' );
            ?>
            <p>
                <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
                <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>">
            </p>
            <?php
        }

        /**
         * Processing widget options on save
         *
         * @param array $new_instance The new options
         * @param array $old_instance The previous options
         * @return array|void
         */
        public function update( $new_instance, $old_instance ) {
            // processes widget options to be saved

            $instance = array();
            $instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';

            return $instance;
        }

    }


    add_action("widgets_init", function(){
        register_widget( ProductCategoryWidget::class );
    });

0 个答案:

没有答案