Как разместить мой плагин после корзины в WordPress - PullRequest
0 голосов
/ 06 апреля 2020

Привет, я исправляю и перепрофилирую старый плагин для своих нужд. Мне удалось получить плагин, как я хочу, но я борюсь с позиционированием того, где кнопка, созданная плагином, расположена на странице одного продукта в Woocommerce. Может кто-нибудь помочь или указать мне в правильном направлении? Я хотел бы отобразить эту кнопку под кнопкой «Добавить в корзину».

    add_action( 'after_setup_theme', array ( 'Request_Update', 'get_instance' ) );

    class Request_Update
    {
        /**
         * Internal variables prefix.
         *
         * @type string
         */
        private $prefix = 'RU';

    /**
     * Name for a field that is hidden per CSS and filled by spammers only.
     *
     * @type string
     */
    private $hidden_field = 'no_fill';

    /**
     * URL of the current page.
     *
     * @see __construct()
     * @type string
     */
    private $current_url = '';

    /**
     * nonce = number used once, unique identifier for request validation.
     *
     * @type string
     */
    private $nonce_name = 'RU_nonce';

    /**
     * On which post types do we want to show the form?
     *
     * @type array
     */
    private $post_types = array ( 'product' );

    /**
     * Creates a new instance. Called on 'after_setup_theme'.
     *
     * @see    __construct()
     * @return void
     */
    public static function get_instance()
    {
        static $instance = NULL;

        NULL === $instance and $instance = new self;

        return $instance;
    }

    /**
     * Set actions, filters and basic variables, load language.
     *
     * @uses apply_filters() 'RU_button' as action name
     *      'RU_auto_add_button' to append the form automatically
     *      'RU_post_types' for supported post types (default: 'post')
     */
    public function __construct()
    {
        // Use
        // do_action( 'RU_button' );
        // in your theme to show the button.
        add_action( $this->prefix . '_button', array ( $this, 'print_button' ) );

        // Use
        // add_filter( 'RU_auto_add_button', '__return_false' );
        // to turn automatic buttons off.
        if ( apply_filters( $this->prefix . '_auto_add_button', TRUE ) )
            add_filter( 'the_content', array ( $this, 'append_button' ), 50 );

        $this->current_url = $_SERVER['REQUEST_URI'];

        // Use
        // add_filter( 'RU_post_types', 'your_callback' );
        // to add more post types.
        $this->post_types  = apply_filters(
            $this->prefix . '_post_types',
            $this->post_types
        );
    }

    /**
     * Handler for the action 'RU_button'. Prints the button.
     *
     * @return void
     */
    public function print_button()
    {
        print $this->button_form();
    }

    /**
     * Handler for content filter.
     *
     * @param  string $content Existing content
     * @return string
     */
    public function append_button( $content )
    {
        if ( is_feed() )
            return $content;

        return $content . $this->button_form();
    }

    /**
     * Returns the button form or a feedback message after submit.
     *
     * @return string
     */
    public function button_form()
    {
        $this->load_translation();

        if ( 'POST' != $_SERVER['REQUEST_METHOD'] )
            return $this->get_form();

        return $this->handle_submit();
    }

    /**
     * Returns the form or an empty string.
     *
     * @uses apply_filters() 'RU_show_form' to suppress form output.
     * @return string
     */
    public function get_form()
    {
        global $post;

        if ( empty ( $post )
            or ! in_array( get_post_type( $post ), $this->post_types )
            // You may disable the form conditionally. For example: restrict it
            // to posts with post-format 'video'.
            or ! apply_filters( $this->prefix . '_show_form', TRUE, $post )
        )
            return '';

        $post_id      = (int) $post->ID;
        $url          = esc_attr( $this->current_url );
        $hidden       = $this->get_hidden_field();
        $nonce        = wp_create_nonce( __FILE__ );
        $button_text  = __( 'Request Update', 'plugin_RU' );

        $form = <<<RUFORM
    <form method='post' action='$url' class='{$this->prefix}_form'>
        <input type='hidden' name='{$this->prefix}[$this->nonce_name]' value='$nonce' />
        <input type='hidden' name='{$this->prefix}[post_id]' value='$post_id' />
        <input type='submit' name='{$this->prefix}[report]' value='$button_text' />
    </form>
    RUFORM;

        return $form;
    }

    /**
     * Hidden text field as spam protection.
     *
     * @return string
     */
    private function get_hidden_field()
    {
        // prevent doubled IDs if you use the_content() on archive pages.
        static $counter = 0;

        $field    = $this->hidden_field . "_$counter";
        $counter += 1;
        $title    = esc_attr__( 'Leave this empty', 'plugin_RU' );

        return "<style scoped>#$field{display:none}</style>
            <input name='{$this->prefix}[$field]' title='$title' />";
    }

    /**
     * Handle form submission.
     *
     * @uses apply_filters() 'RU_recipient' to set the mail recipient.
     *      'RU_from' to set the 'From' header.
     * @return string
     */
    private function handle_submit()
    {
        if ( ! isset ( $_POST[ $this->prefix ] )
            or '' == trim( implode( '', $_POST[ $this->prefix ] ) )
            or ! wp_verify_nonce( $_POST[ $this->prefix ][ $this->nonce_name ], __FILE__ )
            or ! empty ( $_POST[ $this->prefix ][ $this->hidden_field ] )
            or   empty ( $_POST[ $this->prefix ][ 'post_id' ] )
        )
            return $this->get_form();

        $blog_name = get_bloginfo( 'name' );

        // Pro tempore. You may add an option for this in 'wp-admin'.
        $recipient = get_option( 'admin_email' );
        $recipient = apply_filters( $this->prefix . '_recipient', $recipient );

        $subject   = sprintf(
            __( 'UPDATE REQUEST ON %s', 'plugin_RU' ),
            $blog_name
        );
        $message  = sprintf(
            __( "Please update the Plugin\Theme on:\n <%s>", 'plugin_RU' ),
            get_permalink( (int) $_POST[ $this->prefix ][ 'post_id' ] )
        );
        $from     = "From: [UPDATE REQUEST] $blog_name <$recipient>";
        $from     = apply_filters( $this->prefix . '_from', $from );
        $send     = wp_mail( $recipient, $subject, $message, $from );

        $error    = __(
            'Sorry, we could not send the report. May we ask you to use the contact page instead?',
            'plugin_RU'
        );
        $success  = __( "Thank you! We will take a look and update is avaliable, please allow 48 Hours for all update requests.", 'plugin_RU' );
        $feedback = $send ? $success : $error;

        return "<p class='{$this->prefix}_result'>$feedback</p>";
    }

    /**
     * Load translation.
     *
     * @return boolean
     */
    private function load_translation()
    {
        return load_plugin_textdomain(
            'plugin_RU',
            FALSE,
            plugin_basename( __FILE__ ) . '/lang'
        );
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...