function wpv_scan_view_usage_callback() { wpv_ajax_authenticate( 'work_views_listing', array( 'parameter_source' => 'post', 'type_of_death' => 'data' ) ); $post_id = wpv_getpost( 'id', 0 ); if ( 0 == $post_id ) { $data = array( 'message' => __( 'Wrong data', 'wpv-views' ) ); wp_send_json_error( $data ); } global $wpdb, $sitepress; $values_to_prepare = array(); $trans_join = ''; $trans_where = ''; if ( isset( $sitepress ) && function_exists( 'icl_object_id' ) ) { $current_lang_code = $sitepress->get_current_language(); $trans_join = " JOIN {$wpdb->prefix}icl_translations t "; $trans_where = " AND ID = t.element_id AND t.language_code = %s "; $values_to_prepare[] = $current_lang_code; } $view = get_post( $post_id ); $needle = '[wpv-view name="' . $view->post_title . '"'; $needle = '%' . wpv_esc_like( $needle ) . '%'; $needle_name = '[wpv-view name="' . $view->post_name . '"'; $needle_name = '%' . wpv_esc_like( $needle_name ) . '%'; $values_to_prepare[] = $needle; $values_to_prepare[] = $needle_name; $values_to_prepare[] = $needle; $values_to_prepare[] = $needle_name; $q = "SELECT DISTINCT * FROM {$wpdb->posts} {$trans_join} WHERE post_status = 'publish' {$trans_where} AND post_type NOT IN ('revision') AND ( ID IN ( SELECT DISTINCT ID FROM {$wpdb->posts} WHERE ( post_content LIKE %s OR post_content LIKE %s ) AND post_type NOT IN ('revision') AND post_status = 'publish' ) OR ID IN ( SELECT DISTINCT post_id FROM {$wpdb->postmeta} WHERE ( meta_value LIKE %s OR meta_value LIKE %s ) ) )"; $res = $wpdb->get_results( $wpdb->prepare( $q, $values_to_prepare ), OBJECT ); $items = array(); if ( ! empty( $res ) ) { foreach ( $res as $row ) { $type = get_post_type_object( $row->post_type ); $type = $type->labels->singular_name; if ( $row->post_type == 'view' ) { $edit_link = get_admin_url() . "admin.php?page=views-editor&view_id=" . $row->ID; } else if( WPV_Content_Template_Embedded::POST_TYPE == $row->post_type ) { $edit_link = wpv_ct_editor_url( $row->ID ); } else { $edit_link = get_admin_url() . "post.php?post=" . $row->ID . "&action=edit"; } $items[] = array( 'id' => $row->ID, 'link' => $edit_link, 'title' => "<strong>" . $type . "</strong>: " . $row->post_title ); } } $data = array( 'used_on' => $items ); wp_send_json_success( $data ); }
/** * Add a meta box to public and publicly_queryable post types to set the Content Template to be used for that single item being edited * * @param WP_Post $post the post being edited * * Echoes the meta box. * * @since unknown * * @note 1.7 added a link to edit the Content Template * @note 1.7 removed loop Templates from the dropdown */ function content_template_select_meta_box( $post ) { global $wpdb, $WP_Views, $sitepress; $values_to_prepare = array(); $wpml_join = $wpml_where = ""; if ( isset( $sitepress ) && function_exists( 'icl_object_id' ) ) { $content_templates_translatable = $sitepress->is_translated_post_type( 'view-template' ); if ( $content_templates_translatable ) { $wpml_current_language = $sitepress->get_current_language(); $wpml_join = " JOIN {$wpdb->prefix}icl_translations t "; $wpml_where = " AND p.ID = t.element_id AND t.language_code = %s "; $values_to_prepare[] = $wpml_current_language; } } $exclude_loop_templates = ''; $exclude_loop_templates_ids = wpv_get_loop_content_template_ids(); if ( count( $exclude_loop_templates_ids ) > 0 ) { $exclude_loop_templates_ids_sanitized = array_map( 'esc_attr', $exclude_loop_templates_ids ); $exclude_loop_templates_ids_sanitized = array_map( 'trim', $exclude_loop_templates_ids_sanitized ); // is_numeric + intval does sanitization $exclude_loop_templates_ids_sanitized = array_filter( $exclude_loop_templates_ids_sanitized, 'is_numeric' ); $exclude_loop_templates_ids_sanitized = array_map( 'intval', $exclude_loop_templates_ids_sanitized ); if ( count( $exclude_loop_templates_ids_sanitized ) > 0 ) { $exclude_loop_templates = " AND p.ID NOT IN ('" . implode( "','" , $exclude_loop_templates_ids_sanitized ) . "') "; } } $values_to_prepare[] = 'view-template'; $view_tempates_available = $wpdb->get_results( $wpdb->prepare( "SELECT p.ID, p.post_name, p.post_title FROM {$wpdb->posts} p {$wpml_join} WHERE p.post_status = 'publish' {$wpml_where} AND p.post_type = %s {$exclude_loop_templates} ORDER BY p.post_title", $values_to_prepare ) ); if ( isset( $_GET['post'] ) && intval( $_GET['post'] ) > 0 ) { $template_selected = get_post_meta( (int) $_GET['post'], '_views_template', true ); if ( empty( $template_selected ) ) { $template_selected = 0; } } else { $template_selected = 0; global $pagenow, $post_type; if ( $pagenow == 'post-new.php' ) { if ( isset( $_GET['trid'] ) && isset( $_GET['source_lang'] ) ) { $sp_trid = sanitize_text_field( $_GET['trid'] ); $sp_source_lang = sanitize_text_field( $_GET['source_lang'] ); // we are creating a translated post if ( isset( $sitepress ) && function_exists( 'icl_object_id' ) ) { $translations = $sitepress->get_element_translations( $sp_trid, 'post_' . $post->post_type ); if ( isset( $translations[$sp_source_lang] ) ) { $template_selected = get_post_meta( $translations[$sp_source_lang]->element_id, '_views_template', true ); if ( empty( $template_selected ) ) { $template_selected = 0; } } } } if ( $template_selected == 0 ) { // see if we have specified what template to use for this post type global $WPV_settings; if ( isset( $WPV_settings['views_template_for_' . $post_type] ) ) { $template_selected = $WPV_settings['views_template_for_' . $post_type]; } } } } ?> <select name="views_template[<?php echo esc_attr( $post->ID ); ?>]" id="views_template" class="widefat js-wpv-edit-post-select-ct"> <?php // Add a "None" type to the list. $none = new stdClass(); $none->ID = '0'; $none->post_title = __( 'None', 'wpv-views' ); array_unshift( $view_tempates_available, $none ); if ( $template_selected != 0 ) { // Adjust for WPML support $template_selected = apply_filters( 'translate_object_id', $template_selected, 'view-template', true, null ); } foreach( $view_tempates_available as $template ) { if ( $template->post_title != '' ) { ?> <option value="<?php echo esc_attr( $template->ID ); ?>" <?php selected( $template_selected, $template->ID ); ?>><?php echo $template->post_title; ?></option> <?php } else { ?> <option value="<?php echo esc_attr( $template->ID ); ?>" <?php selected( $template_selected, $template->ID ); ?>><?php echo $template->post_name; ?></option> <?php } } ?> </select> <?php if ( ! $WP_Views->is_embedded() ) { $edit_link = ''; $edit_link_visible = ' hidden'; if ( ! empty( $template_selected ) && $template_selected !== 0 && get_post_status( $template_selected ) == 'publish' ) { $edit_link = wpv_ct_editor_url( $template_selected ); $edit_link_visible = ''; } ?> <div class="js-wpv-edit-post-edit-ct-link-container<?php echo esc_attr( $edit_link_visible ); ?>" style="margin-top:10px;padding-top:10px;border-top:solid 1px #ccc;"> <a href="<?php echo esc_url( $edit_link ); ?>" class="button button-secondary js-wpv-edit-post-edit-ct-link" target="_blank"><?php _e( 'Edit this Content Template', 'wpv-views' ); ?> <i class="icon-chevron-right"></i></a> </div> <script type="text/javascript"> jQuery( function( $ ) { // Warning! We need to take care of Layouts compatibility: if there is a select for Layouts AND has a value, we need to hide the thiz_wpv_ct_link_container var thiz_wpv_layout_select = $( document.getElementById( 'js-layout-template-name' ) ), thiz_wpv_ct_select = $( '.js-wpv-edit-post-select-ct' ), thiz_wpv_ct_link = $( '.js-wpv-edit-post-edit-ct-link' ), thiz_wpv_ct_link_container = $( '.js-wpv-edit-post-edit-ct-link-container' ), thiz_wpv_ct_select_manager = function() { if ( thiz_wpv_ct_select.val() != 0 ) { thiz_wpv_ct_link.attr( 'href', 'admin.php?page=' + <?php printf( '\'%s\'', WPV_CT_EDITOR_PAGE_NAME ); ?> + '&ct_id=' + thiz_wpv_ct_select.val() + '&action=edit' ); thiz_wpv_ct_link_container.fadeIn( 'fast' ); } else { thiz_wpv_ct_link_container.hide(); } }; if ( thiz_wpv_layout_select.length > 0 ) { if ( thiz_wpv_layout_select.val() != '0' ) { thiz_wpv_ct_link_container.hide(); } else { thiz_wpv_ct_select_manager(); } thiz_wpv_layout_select.on( 'change', function() { if ( thiz_wpv_layout_select.val() != '0' ) { thiz_wpv_ct_link_container.hide(); } else { thiz_wpv_ct_select_manager(); } }); } thiz_wpv_ct_select.on( 'change', thiz_wpv_ct_select_manager ); }); </script> <?php } }
function wpv_scan_wpv_if_callback() { if ( ! current_user_can( 'manage_options' ) ) { die( "Untrusted user" ); } if ( ! wp_verify_nonce( $_POST["wpnonce"], 'views_update_help_wpv_if_nonce' ) ) { die( "Security check" ); } global $wpdb, $sitepress, $WP_Views; $values_to_prepare = array(); $trans_join = ''; $trans_where = ''; $trans_meta_where = ''; if ( isset( $sitepress ) && function_exists( 'icl_object_id' ) ) { $current_lang_code = $sitepress->get_current_language(); $trans_join = " JOIN {$wpdb->prefix}icl_translations t "; $trans_where = " AND ID = t.element_id AND t.language_code = %s "; $values_to_prepare[] = $current_lang_code; } $needle = '%[wpv-if%'; $values_to_prepare[] = $needle; $values_to_prepare[] = $needle; $q = "SELECT DISTINCT * FROM {$wpdb->posts} {$trans_join} WHERE post_status='publish' {$trans_where} AND post_type NOT IN ('revision') AND ( ID IN ( SELECT DISTINCT ID FROM {$wpdb->posts} WHERE post_content LIKE %s ) OR ID IN ( SELECT DISTINCT post_id FROM {$wpdb->postmeta} WHERE meta_value LIKE %s ) ) "; $res = $wpdb->get_results( $wpdb->prepare( $q, $values_to_prepare ), OBJECT ); $items = array(); $slug_to_label = array(); $wpa_label = __( 'WordPress Archives', 'wpv-views' ); if ( !empty( $res ) ) { $items = array(); foreach ( $res as $row ) { if ( isset( $slug_to_label[$row->post_type] ) ) { $type = $slug_to_label[$row->post_type]; } else { $post_object = get_post_type_object( $row->post_type ); $type = $post_object->labels->singular_name; $slug_to_label[$row->post_type] = $type; } if ( !isset( $items[$type] ) ) { $items[$type] = array(); } $edit_link_item = ''; if ( $row->post_type == 'view' ) { $settings = $WP_Views->get_view_settings($row->ID); if ($settings['view-query-mode'] == 'normal') { $edit_link_item = get_admin_url() . "admin.php?page=views-editor&view_id=" . $row->ID; } else if ($settings['view-query-mode'] == 'archive') { if (!isset($items[$wpa_label])) { $items[$wpa_label] = array(); } $type = $wpa_label; $edit_link_item = get_admin_url() . "admin.php?page=view-archives-editor&view_id=" . $row->ID; } } else if( WPV_Content_Template_Embedded::POST_TYPE == $row->post_type ) { $edit_link_item = wpv_ct_editor_url( $row->ID ); } else { $edit_link_item = get_admin_url()."post.php?post=".$row->ID."&action=edit"; } if ( !empty( $edit_link_item ) ) { $edit_link = '<a target="_blank" href="'; $edit_link .= $edit_link_item; $edit_link .= '" title="' . esc_attr( __( 'Edit this item', 'wpv-views' ) ) . '">'; $edit_link .= $row->post_title; $edit_link .= '</a>'; $items[$type][] = $edit_link; } } } echo json_encode( $items ); die(); }
/** * Adjust link to Content Template edit page for full Views. * * See icl_post_link for parameter description. * * @param $link * @param $post_type * @param $post_id * @param $link_purpose * @return array * * @since 1.10 */ function wpv_ct_post_link( $link, $post_type, $post_id, $link_purpose ) { global $WP_Views; if( !$WP_Views->is_embedded() && ( WPV_Content_Template_Embedded::POST_TYPE == $post_type ) && ( 'edit' == $link_purpose ) ) { // Full Views, CT edit link is requested if( !is_array( $link ) ) { $link = array(); } // If CT is trashed or non-existent, disable the link. $ct = WPV_Content_Template::get_instance( $post_id ); if( ( null == $ct ) || $ct->is_trashed ) { $link['is_disabled'] = true; } else { $link['is_disabled'] = false; $link['url'] = wpv_ct_editor_url( $post_id, false ); } } return $link; }