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