types
2 years ago
base-form-action.php
2 years ago
form-actions-manager.php
2 years ago
get-form-data.php
2 years ago
import-form-trait.php
2 years ago
base-form-action.php
99 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace Jet_Form_Builder\Form_Actions; |
| 5 | |
| 6 | use Jet_Form_Builder\Classes\Get_Template_Trait; |
| 7 | |
| 8 | // If this file is called directly, abort. |
| 9 | if ( ! defined( 'WPINC' ) ) { |
| 10 | die; |
| 11 | } |
| 12 | |
| 13 | abstract class Base_Form_Action { |
| 14 | |
| 15 | use Get_Template_Trait; |
| 16 | |
| 17 | const NONCE_ACTION = 'jfb_admin_inline'; |
| 18 | |
| 19 | public function __construct() { |
| 20 | add_action( 'admin_action_' . $this->action_id(), array( $this, 'do_action' ) ); |
| 21 | } |
| 22 | |
| 23 | abstract public function get_id(); |
| 24 | |
| 25 | abstract public function get_title(); |
| 26 | |
| 27 | abstract public function do_admin_action(); |
| 28 | |
| 29 | public function do_action() { |
| 30 | if ( ! wp_verify_nonce( sanitize_key( $_REQUEST['_wpnonce'] ?? '' ), $this->get_id() ) ) { |
| 31 | wp_die( 'Your link has expired, please return to the previous page and try again', 'Error' ); |
| 32 | } |
| 33 | |
| 34 | $this->do_admin_action(); |
| 35 | } |
| 36 | |
| 37 | public function action_id() { |
| 38 | return $this->get_id(); |
| 39 | } |
| 40 | |
| 41 | public function display_action_link() { |
| 42 | return true; |
| 43 | } |
| 44 | |
| 45 | public function action_url( $post = null ) { |
| 46 | $admin_url = esc_url_raw( admin_url( 'admin.php' ) ); |
| 47 | |
| 48 | $args = array( |
| 49 | 'action' => $this->action_id(), |
| 50 | '_wpnonce' => wp_create_nonce( $this->get_id() ), |
| 51 | ); |
| 52 | |
| 53 | if ( $post instanceof \WP_Post ) { |
| 54 | $args['post'] = $post->ID; |
| 55 | } |
| 56 | |
| 57 | return add_query_arg( $args, $admin_url ); |
| 58 | } |
| 59 | |
| 60 | public function action_template() { |
| 61 | return '<a href="%1$s" title="%3$s" rel="permalink">%2$s</a>'; |
| 62 | } |
| 63 | |
| 64 | public function hover_title() { |
| 65 | return $this->get_title(); |
| 66 | } |
| 67 | |
| 68 | public function post_type() { |
| 69 | return jet_form_builder()->post_type->slug(); |
| 70 | } |
| 71 | |
| 72 | final public function register_action( $actions, $post ) { |
| 73 | if ( $this->post_type() !== $post->post_type ) { |
| 74 | return $actions; |
| 75 | } |
| 76 | |
| 77 | if ( ! $this->check_user_access( $post->ID ) ) { |
| 78 | return $actions; |
| 79 | } |
| 80 | |
| 81 | $actions[ $this->get_id() ] = sprintf( |
| 82 | $this->action_template(), |
| 83 | $this->action_url( $post ), |
| 84 | $this->get_title(), |
| 85 | $this->hover_title() |
| 86 | ); |
| 87 | |
| 88 | return $actions; |
| 89 | } |
| 90 | |
| 91 | protected function check_user_access( $post_id = null ) { |
| 92 | return ( |
| 93 | current_user_can( 'edit_posts' ) && current_user_can( 'edit_post', $post_id ) |
| 94 | ); |
| 95 | } |
| 96 | |
| 97 | |
| 98 | } |
| 99 |