| 1 |
<?php |
| 2 |
|
| 3 |
// Exit if accessed directly |
| 4 |
if ( ! defined( 'ABSPATH' ) ) { |
| 5 |
exit; |
| 6 |
} |
| 7 |
|
| 8 |
class WINP_SnippetsViewTable extends Wbcr_FactoryViewtables410_Viewtable { |
| 9 |
|
| 10 |
public function configure() { |
| 11 |
/** |
| 12 |
* Columns |
| 13 |
*/ |
| 14 |
$this->columns->clear(); |
| 15 |
//$this->columns->add('status', __('Status', 'insert-php')); |
| 16 |
$this->columns->add( 'title', __( 'Snippet title', 'insert-php' ) ); |
| 17 |
$this->columns->add( 'description', __( 'Description', 'insert-php' ) ); |
| 18 |
$this->columns->add( 'actions', __( 'Status', 'insert-php' ) ); |
| 19 |
$this->columns->add( 'where_use', __( 'Where use?', 'insert-php' ) ); |
| 20 |
$this->columns->add( 'taxonomy-' . WINP_SNIPPETS_TAXONOMY, __( 'Tags', 'insert-php' ) ); |
| 21 |
$this->columns->add( 'snippet_type', '' ); |
| 22 |
|
| 23 |
/** |
| 24 |
* Scripts & styles |
| 25 |
*/ |
| 26 |
$this->styles->add( WINP_PLUGIN_URL . '/admin/assets/css/list-table.css' ); |
| 27 |
$this->runActions(); |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Column 'Title' |
| 32 |
* |
| 33 |
* @param $post |
| 34 |
*/ |
| 35 |
public function columnTitle( $post ) { |
| 36 |
echo $post->post_title; |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Column 'Type' |
| 41 |
* |
| 42 |
* @param $post |
| 43 |
*/ |
| 44 |
public function columnSnippet_type( $post ) { |
| 45 |
$type = WINP_Helper::getMetaOption( $post->ID, 'snippet_type', WINP_SNIPPET_TYPE_PHP ); |
| 46 |
$class = 'wbcr-inp-type-' . esc_attr( $type ); |
| 47 |
$type = $type == 'universal' ? 'uni' : $type; |
| 48 |
|
| 49 |
echo '<div class="wbcr-inp-snippet-type-label ' . $class . '">' . esc_html( $type ) . '</div>'; |
| 50 |
} |
| 51 |
|
| 52 |
public function columnDescription( $post ) { |
| 53 |
echo esc_html( WINP_Helper::getMetaOption( $post->ID, 'snippet_description' ) ); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Column 'Where_use' |
| 58 |
* |
| 59 |
* @param $post |
| 60 |
*/ |
| 61 |
public function columnWhere_use( $post ) { |
| 62 |
$snippet_scope = WINP_Helper::getMetaOption( $post->ID, 'snippet_scope' ); |
| 63 |
|
| 64 |
if ( $snippet_scope == 'evrywhere' ) { |
| 65 |
echo __( 'Run everywhere', 'insert-php' ); |
| 66 |
} else if ( $snippet_scope == 'auto' ) { |
| 67 |
$items = [ |
| 68 |
WINP_SNIPPET_AUTO_HEADER => __( 'Header', 'insert-php' ), |
| 69 |
WINP_SNIPPET_AUTO_FOOTER => __( 'Footer', 'insert-php' ), |
| 70 |
WINP_SNIPPET_AUTO_BEFORE_POST => __( 'Insert Before Post', 'insert-php' ), |
| 71 |
WINP_SNIPPET_AUTO_BEFORE_CONTENT => __( 'Insert Before Content', 'insert-php' ), |
| 72 |
WINP_SNIPPET_AUTO_BEFORE_PARAGRAPH => __( 'Insert Before Paragraph', 'insert-php' ), |
| 73 |
WINP_SNIPPET_AUTO_AFTER_PARAGRAPH => __( 'Insert After Paragraph', 'insert-php' ), |
| 74 |
WINP_SNIPPET_AUTO_AFTER_CONTENT => __( 'Insert After Content', 'insert-php' ), |
| 75 |
WINP_SNIPPET_AUTO_AFTER_POST => __( 'Insert After Post', 'insert-php' ), |
| 76 |
WINP_SNIPPET_AUTO_BEFORE_EXCERPT => __( 'Insert Before Excerpt', 'insert-php' ), |
| 77 |
WINP_SNIPPET_AUTO_AFTER_EXCERPT => __( 'Insert After Excerpt', 'insert-php' ), |
| 78 |
WINP_SNIPPET_AUTO_BETWEEN_POSTS => __( 'Between Posts', 'insert-php' ), |
| 79 |
WINP_SNIPPET_AUTO_BEFORE_POSTS => __( 'Before post', 'insert-php' ), |
| 80 |
WINP_SNIPPET_AUTO_AFTER_POSTS => __( 'After post', 'insert-php' ), |
| 81 |
]; |
| 82 |
|
| 83 |
$snippet_location = WINP_Helper::getMetaOption( $post->ID, 'snippet_location', '' ); |
| 84 |
|
| 85 |
switch ( $snippet_location ) { |
| 86 |
case WINP_SNIPPET_AUTO_HEADER: |
| 87 |
case WINP_SNIPPET_AUTO_FOOTER: |
| 88 |
$text = __( 'Everywhere', 'insert-php' ) . '[' . $items[ $snippet_location ] . ']'; |
| 89 |
break; |
| 90 |
|
| 91 |
case WINP_SNIPPET_AUTO_BEFORE_POST: |
| 92 |
case WINP_SNIPPET_AUTO_BEFORE_CONTENT: |
| 93 |
case WINP_SNIPPET_AUTO_BEFORE_PARAGRAPH: |
| 94 |
case WINP_SNIPPET_AUTO_AFTER_PARAGRAPH: |
| 95 |
case WINP_SNIPPET_AUTO_AFTER_CONTENT: |
| 96 |
case WINP_SNIPPET_AUTO_AFTER_POST: |
| 97 |
$text = __( 'Posts, Pages, Custom post types', 'insert-php' ) . '[' . $items[ $snippet_location ] . ']'; |
| 98 |
break; |
| 99 |
|
| 100 |
case WINP_SNIPPET_AUTO_BEFORE_EXCERPT: |
| 101 |
case WINP_SNIPPET_AUTO_AFTER_EXCERPT: |
| 102 |
case WINP_SNIPPET_AUTO_BETWEEN_POSTS: |
| 103 |
case WINP_SNIPPET_AUTO_BEFORE_POSTS: |
| 104 |
case WINP_SNIPPET_AUTO_AFTER_POSTS: |
| 105 |
$text = __( 'Categories, Archives, Tags, Taxonomies', 'insert-php' ) . '[' . $items[ $snippet_location ] . ']'; |
| 106 |
break; |
| 107 |
|
| 108 |
default: |
| 109 |
$text = __( 'Everywhere', 'insert-php' ); |
| 110 |
} |
| 111 |
|
| 112 |
echo __( 'Automatic insertion', 'insert-php' ) . ': ' . $text; |
| 113 |
} else { |
| 114 |
$snippet_type = WINP_Helper::get_snippet_type( $post->ID ); |
| 115 |
$snippet_type = ( $snippet_type == WINP_SNIPPET_TYPE_UNIVERSAL ? '' : $snippet_type . '_' ); |
| 116 |
|
| 117 |
echo esc_html( apply_filters( 'wbcr/inp/viewtable/where_use', '[wbcr_' . $snippet_type . 'snippet id="' . $post->ID . '"]', $post->ID ) ); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Column 'Actions' |
| 123 |
* |
| 124 |
* @param $post |
| 125 |
*/ |
| 126 |
public function columnActions( $post ) { |
| 127 |
$is_activate = (int) WINP_Helper::getMetaOption( $post->ID, 'snippet_activate', 0 ); |
| 128 |
$icon = 'dashicons-controls-play'; |
| 129 |
|
| 130 |
if ( $is_activate ) { |
| 131 |
$icon = 'dashicons-controls-pause'; |
| 132 |
} |
| 133 |
|
| 134 |
echo '<a class="wbcr-inp-enable-snippet-button button" href="' . wp_nonce_url( admin_url( 'edit.php?post_type=' . WINP_SNIPPETS_POST_TYPE . '&post=' . $post->ID . '&action=wbcr_inp_activate_snippet' ), 'wbcr_inp_snippert_' . $post->ID . '_action_nonce' ) . '"><span class="dashicons ' . esc_attr( $icon ) . '"></span></a>'; |
| 135 |
} |
| 136 |
|
| 137 |
/* |
| 138 |
* Activate/Deactivate snippet |
| 139 |
*/ |
| 140 |
protected function runActions() { |
| 141 |
if ( WINP_Plugin::app()->request->get( 'post_type', '', true ) == WINP_SNIPPETS_POST_TYPE ) { |
| 142 |
$post = WINP_Plugin::app()->request->get( 'post', 0 ); |
| 143 |
$action = WINP_Plugin::app()->request->get( 'action', '', 'sanitize_key' ); |
| 144 |
|
| 145 |
if ( ! empty( $action ) && ! empty( $post ) && 'wbcr_inp_activate_snippet' == $action ) { |
| 146 |
$post_id = (int) $post; |
| 147 |
$wpnonce = WINP_Plugin::app()->request->get( '_wpnonce', '' ); |
| 148 |
|
| 149 |
if ( ( ! empty( $wpnonce ) && ! wp_verify_nonce( $wpnonce, 'wbcr_inp_snippert_' . $post_id . '_action_nonce' ) ) || ! WINP_Plugin::app()->currentUserCan() ) { |
| 150 |
wp_die( 'Permission error. You can not edit this page.' ); |
| 151 |
} |
| 152 |
|
| 153 |
$is_activate = (int) WINP_Helper::getMetaOption( $post_id, 'snippet_activate', 0 ); |
| 154 |
$snippet_scope = WINP_Helper::getMetaOption( $post_id, 'snippet_scope' ); |
| 155 |
$snippet_type = WINP_Helper::get_snippet_type( $post_id ); |
| 156 |
|
| 157 |
/** |
| 158 |
* Prevent activation of the snippet if it contains an error. This will not allow the user to break his site. |
| 159 |
* |
| 160 |
* @since 2.0.5 |
| 161 |
*/ |
| 162 |
if ( ( 'evrywhere' == $snippet_scope || 'auto' == $snippet_scope ) && $snippet_type != WINP_SNIPPET_TYPE_TEXT && $snippet_type != WINP_SNIPPET_TYPE_CSS && $snippet_type != WINP_SNIPPET_TYPE_JS && ! $is_activate ) { |
| 163 |
if ( WINP_Plugin::app()->getExecuteObject()->getSnippetError( $post_id ) ) { |
| 164 |
wp_safe_redirect( add_query_arg( [ |
| 165 |
'action' => 'edit', |
| 166 |
'post' => $post_id, |
| 167 |
'wbcr_inp_save_snippet_result' => 'code-error', |
| 168 |
], admin_url( 'post.php' ) ) ); |
| 169 |
exit; |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
$status = ! $is_activate; |
| 174 |
|
| 175 |
update_post_meta( $post_id, $this->plugin->getPrefix() . 'snippet_activate', $status ); |
| 176 |
|
| 177 |
$redirect_url = add_query_arg( [ |
| 178 |
'post_type' => WINP_SNIPPETS_POST_TYPE, |
| 179 |
'wbcr_inp_snippet_updated' => 1, |
| 180 |
], admin_url( 'edit.php' ) ); |
| 181 |
|
| 182 |
wp_safe_redirect( $redirect_url ); |
| 183 |
exit; |
| 184 |
} |
| 185 |
} |
| 186 |
} |
| 187 |
} |
| 188 |
|