| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPDataAccess\Dashboard; |
| 4 |
|
| 5 |
use WPDataAccess\Data_Tables\WPDA_Data_Tables; |
| 6 |
use WPDataAccess\Plugin_Table_Models\WPDA_Publisher_Model; |
| 7 |
use WPDataAccess\WPDA; |
| 8 |
class WPDA_Widget_Publication extends WPDA_Widget { |
| 9 |
protected $pub_id; |
| 10 |
|
| 11 |
protected $pub_content; |
| 12 |
|
| 13 |
protected $table_name; |
| 14 |
|
| 15 |
protected $search_header; |
| 16 |
|
| 17 |
public function __construct( $args = array() ) { |
| 18 |
parent::__construct( $args ); |
| 19 |
$this->can_share = true; |
| 20 |
$this->can_refresh = true; |
| 21 |
if ( isset( $args['pub_id'] ) ) { |
| 22 |
$this->pub_id = $args['pub_id']; |
| 23 |
$embedding = isset( $args['embedding'] ) && true === $args['embedding']; |
| 24 |
$wpda_data_tables = new WPDA_Data_Tables(); |
| 25 |
$this->pub_content = $wpda_data_tables->show( |
| 26 |
$this->pub_id, |
| 27 |
'', |
| 28 |
'', |
| 29 |
'', |
| 30 |
'', |
| 31 |
'', |
| 32 |
'', |
| 33 |
'', |
| 34 |
'', |
| 35 |
'', |
| 36 |
'', |
| 37 |
'', |
| 38 |
'', |
| 39 |
true, |
| 40 |
$embedding |
| 41 |
); |
| 42 |
$wpda_publication = WPDA_Publisher_Model::get_publication( $this->pub_id ); |
| 43 |
$this->table_name = $wpda_publication['0']['pub_table_name']; |
| 44 |
try { |
| 45 |
$json = json_decode( $wpda_publication['0']['pub_table_options_advanced'] ); |
| 46 |
if ( isset( $json->wpda_searchbox ) && ('header' === $json->wpda_searchbox || 'both' === $json->wpda_searchbox) ) { |
| 47 |
$this->search_header = true; |
| 48 |
} |
| 49 |
} catch ( \Exception $e ) { |
| 50 |
$this->search_header = false; |
| 51 |
} |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
public function do_shortcode( $widget ) { |
| 56 |
// Not implemented (use Data Tables short code) |
| 57 |
} |
| 58 |
|
| 59 |
public function do_embed( $widget, $target_element ) { |
| 60 |
} |
| 61 |
|
| 62 |
protected function js( $is_backend = true ) { |
| 63 |
?> |
| 64 |
<script type="application/javascript"> |
| 65 |
jQuery(function() { |
| 66 |
jQuery("#wpda-widget-<?php |
| 67 |
echo esc_attr( $this->widget_id ); |
| 68 |
?>").find(".wpda-widget-refresh").on("click", function(e, action = null) { |
| 69 |
if (action==="refresh") { |
| 70 |
jQuery("#<?php |
| 71 |
echo esc_attr( $this->table_name ) . esc_attr( $this->pub_id ); |
| 72 |
?>").DataTable().ajax.json(); |
| 73 |
} else { |
| 74 |
jQuery("#<?php |
| 75 |
echo esc_attr( $this->table_name ) . esc_attr( $this->pub_id ); |
| 76 |
?>").DataTable().draw("page"); |
| 77 |
} |
| 78 |
jQuery("#<?php |
| 79 |
echo esc_attr( $this->table_name ) . esc_attr( $this->pub_id ); |
| 80 |
?>").DataTable().responsive.recalc(); |
| 81 |
<?php |
| 82 |
if ( $this->search_header && $is_backend ) { |
| 83 |
?> |
| 84 |
post_publication_widget("<?php |
| 85 |
echo esc_attr( $this->table_name ); |
| 86 |
?>", "<?php |
| 87 |
echo esc_attr( $this->pub_id ); |
| 88 |
?>"); |
| 89 |
<?php |
| 90 |
} |
| 91 |
?> |
| 92 |
}); |
| 93 |
}); |
| 94 |
</script> |
| 95 |
<?php |
| 96 |
} |
| 97 |
|
| 98 |
protected function container() { |
| 99 |
ob_start(); |
| 100 |
echo parent::container(); |
| 101 |
// phpcs:ignore WordPress.Security.EscapeOutput |
| 102 |
$post_publication = ( $this->search_header ? "post_publication_widget('{$this->table_name}', '{$this->pub_id}');" : '' ); |
| 103 |
$post_content = "\n\t\t\t\t<div id='wpda-panel-pub-id-{$this->pub_id}' style='display: block'>\n\t\t\t\t\t{$this->pub_content}\n\t\t\t\t</div>\n\t\t\t\t<script type='application/javascript'>\n\t\t\t\tjQuery(function() {\n\t\t\t\t\tsetTimeout(waitUntilWidgetIsLoaded_{$this->pub_id}, 1000);\n\t\t\t\t\tfunction waitUntilWidgetIsLoaded_{$this->pub_id}() {\n\t\t\t\t\t\tif (jQuery('#wpda-widget-{$this->widget_id} div.ui-widget-content').length>0) {\n\t\t\t\t\t\t\tjQuery('#wpda-widget-{$this->widget_id} div.ui-widget-content').html('');\n\t\t\t\t\t\t\tjQuery('#wpda-panel-pub-id-{$this->pub_id}').appendTo(jQuery('#wpda-widget-{$this->widget_id} div.ui-widget-content'));\n\t\t\t\t\t\t\tjQuery('#{$this->table_name}{$this->pub_id}').DataTable().responsive.recalc();\n\t\t\t\t\t\t\tjQuery('#{$this->table_name}{$this->pub_id}').DataTable().columns.adjust();\n\t\t\t\t\t\t\t{$post_publication}\n\t\t\t\t\t\t\t// console.log('WP Data Access data table libraries loaded...');\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\tsetTimeout(waitUntilWidgetIsLoaded_{$this->pub_id}, 1000);\n\t\t\t\t\t\t\tconsole.log('Waiting for WP Data Access data table libraries to be loaded...');\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t});\n\t\t\t\t</script>\n\t\t\t"; |
| 104 |
echo $post_content; |
| 105 |
// phpcs:ignore WordPress.Security.EscapeOutput |
| 106 |
return ob_get_clean(); |
| 107 |
} |
| 108 |
|
| 109 |
public static function widget() { |
| 110 |
$panel_name = ( isset( $_REQUEST['wpda_panel_name'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['wpda_panel_name'] ) ) : '' ); |
| 111 |
// input var okay.; |
| 112 |
$panel_pub_id = ( isset( $_REQUEST['wpda_panel_pub_id'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['wpda_panel_pub_id'] ) ) : '' ); |
| 113 |
// input var okay.; |
| 114 |
$panel_column = ( isset( $_REQUEST['wpda_panel_column'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['wpda_panel_column'] ) ) : '1' ); |
| 115 |
// input var okay.; |
| 116 |
$column_position = ( isset( $_REQUEST['wpda_column_position'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['wpda_column_position'] ) ) : 'prepend' ); |
| 117 |
// input var okay.; |
| 118 |
$widget_sequence_nr = ( isset( $_REQUEST['wpda_widget_sequence_nr'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['wpda_widget_sequence_nr'] ) ) : '1' ); |
| 119 |
// input var okay.; |
| 120 |
$wdg = new WPDA_Widget_Publication(array( |
| 121 |
'name' => $panel_name, |
| 122 |
'pub_id' => $panel_pub_id, |
| 123 |
'column' => $panel_column, |
| 124 |
'position' => $column_position, |
| 125 |
'widget_id' => $widget_sequence_nr, |
| 126 |
)); |
| 127 |
WPDA::sent_header( 'text/html; charset=UTF-8' ); |
| 128 |
echo $wdg->container(); |
| 129 |
// phpcs:ignore WordPress.Security.EscapeOutput |
| 130 |
wp_die(); |
| 131 |
} |
| 132 |
|
| 133 |
public static function refresh() { |
| 134 |
echo static::msg( 'ERROR', 'Method not available for this panel type' ); |
| 135 |
// phpcs:ignore WordPress.Security.EscapeOutput |
| 136 |
wp_die(); |
| 137 |
} |
| 138 |
|
| 139 |
} |
| 140 |
|