module.php
138 lines
| 1 | <?php |
| 2 | |
| 3 | |
| 4 | namespace JFB_Modules\Shortcode; |
| 5 | |
| 6 | use Jet_Form_Builder\Classes\Arguments\Default_Form_Arguments; |
| 7 | use Jet_Form_Builder\Classes\Arguments\Form_Arguments; |
| 8 | use Jet_Form_Builder\Exceptions\Repository_Exception; |
| 9 | use JFB_Components\Module\Base_Module_After_Install_It; |
| 10 | use JFB_Components\Module\Base_Module_It; |
| 11 | use JFB_Components\Repository\Repository_Pattern_Trait; |
| 12 | |
| 13 | // If this file is called directly, abort. |
| 14 | if ( ! defined( 'WPINC' ) ) { |
| 15 | die; |
| 16 | } |
| 17 | |
| 18 | /** |
| 19 | * Class Manager |
| 20 | * |
| 21 | * @package JFB_Modules\Shortcode |
| 22 | */ |
| 23 | final class Module implements Base_Module_It, Base_Module_After_Install_It { |
| 24 | |
| 25 | use Repository_Pattern_Trait; |
| 26 | |
| 27 | public function rep_item_id() { |
| 28 | return 'shortcode'; |
| 29 | } |
| 30 | |
| 31 | public function on_install() { |
| 32 | $this->rep_install(); |
| 33 | } |
| 34 | |
| 35 | public function on_uninstall() { |
| 36 | $this->rep_clear(); |
| 37 | } |
| 38 | |
| 39 | public function condition(): bool { |
| 40 | return true; |
| 41 | } |
| 42 | |
| 43 | public function init_hooks() { |
| 44 | $slug = \JFB_Modules\Post_Type\Module::SLUG; |
| 45 | |
| 46 | // post-type related |
| 47 | add_filter( |
| 48 | "manage_{$slug}_posts_columns", |
| 49 | array( $this, 'filter_columns' ) |
| 50 | ); |
| 51 | add_action( |
| 52 | "manage_{$slug}_posts_custom_column", |
| 53 | array( $this, 'add_admin_column_content' ), |
| 54 | 10, |
| 55 | 2 |
| 56 | ); |
| 57 | } |
| 58 | |
| 59 | public function remove_hooks() { |
| 60 | $slug = \JFB_Modules\Post_Type\Module::SLUG; |
| 61 | |
| 62 | // post-type related |
| 63 | remove_filter( |
| 64 | "manage_{$slug}_posts_columns", |
| 65 | array( $this, 'filter_columns' ) |
| 66 | ); |
| 67 | remove_action( |
| 68 | "manage_{$slug}_posts_custom_column", |
| 69 | array( $this, 'add_admin_column_content' ) |
| 70 | ); |
| 71 | } |
| 72 | |
| 73 | public function rep_instances(): array { |
| 74 | return array( |
| 75 | new Form_Shortcode(), |
| 76 | ); |
| 77 | } |
| 78 | |
| 79 | public function filter_columns( $columns ) { |
| 80 | $after = array( 'date' => $columns['date'] ); |
| 81 | unset( $columns['date'] ); |
| 82 | |
| 83 | $columns['jfb_shortcode'] = __( 'Shortcode', 'jet-form-builder' ); |
| 84 | |
| 85 | return array_merge( $columns, $after ); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * @param $column |
| 90 | * @param $form_id |
| 91 | * |
| 92 | * @return void |
| 93 | * @throws Repository_Exception |
| 94 | */ |
| 95 | public function add_admin_column_content( $column, $form_id ) { |
| 96 | if ( 'jfb_shortcode' !== $column ) { |
| 97 | return; |
| 98 | } |
| 99 | /** @var \JFB_Modules\Post_Type\Module $post_type */ |
| 100 | $post_type = jet_form_builder()->module( 'post-type' ); |
| 101 | $arguments = array_diff( $post_type->get_args( $form_id ), Form_Arguments::arguments() ); |
| 102 | |
| 103 | $arguments = array_merge( |
| 104 | array( 'form_id' => $form_id ), |
| 105 | Default_Form_Arguments::arguments(), |
| 106 | $arguments |
| 107 | ); |
| 108 | |
| 109 | printf( |
| 110 | '<input readonly type="text" onclick="this.select()" value="%s" style="%s"/>', |
| 111 | esc_attr( $this->get_shortcode( 'jet_fb_form', $arguments ) ), |
| 112 | 'width: 100%' |
| 113 | ); |
| 114 | } |
| 115 | |
| 116 | public function get_shortcode( $type, $arguments ): string { |
| 117 | $format = '[%1$s %2$s]'; |
| 118 | |
| 119 | try { |
| 120 | $type = $this->rep_clone_item( $type ); |
| 121 | } catch ( Repository_Exception $exception ) { |
| 122 | return ''; |
| 123 | } |
| 124 | |
| 125 | return sprintf( $format, $type->get_name(), $this->generate_arguments_string( $arguments ) ); |
| 126 | } |
| 127 | |
| 128 | private function generate_arguments_string( $arguments ): string { |
| 129 | $response = array(); |
| 130 | |
| 131 | foreach ( $arguments as $name => $value ) { |
| 132 | $response[] = "$name=\"$value\""; |
| 133 | } |
| 134 | |
| 135 | return implode( ' ', $response ); |
| 136 | } |
| 137 | } |
| 138 |