web/wp-content/plugins/page2cat/page2cat_class.php
changeset 194 32102edaa81b
equal deleted inserted replaced
193:2f6f6f7551ca 194:32102edaa81b
       
     1 <?php
       
     2 
       
     3 class SWER_aptools_shortcodes{
       
     4 
       
     5     function showsingle( $atts ){
       
     6     	extract( shortcode_atts( array(
       
     7     		'postid' => '',
       
     8     		'pageid' => '',
       
     9     		'showheader' => 'true',
       
    10     		'header' => '2',
       
    11     		'headerclass' => 'aptools-single-header',
       
    12     		'wrapper' => 'false',
       
    13     		'wrapperclass' => 'aptools-wrapper'
       
    14     	), $atts ) );
       
    15 
       
    16         $hopen = '<h'.$header.' class='.$headerclass.'>';
       
    17         $hclose = '</h'.$header.'>';
       
    18 
       
    19         if( $postid === '' && $pageid !== '' ):
       
    20             $args = array( 'page_id' => $pageid, 'posts_per_page' => 1 );
       
    21         elseif( $pageid === '' && $postid !== '' ):
       
    22             $args = array( 'p' => $postid, 'posts_per_page' => 1 );
       
    23         endif;
       
    24 
       
    25         $page = new WP_Query( $args );
       
    26         if( $page->have_posts() ):
       
    27             if( $wrapper !== 'false'){
       
    28                 echo '<div class="'.$wrapperclass.'">';
       
    29             }
       
    30             while( $page->have_posts() ):
       
    31                 $page->the_post();
       
    32                 if( $showheader === 'true' ) echo $hopen . get_the_title() . $hclose;
       
    33                 echo get_the_content();
       
    34             endwhile;
       
    35             if( $wrapper !== 'false'){
       
    36                 echo '</div>';
       
    37             }
       
    38         endif;
       
    39         wp_reset_postdata();                
       
    40     }
       
    41 
       
    42 
       
    43     function showlist( $atts ){
       
    44     	extract( shortcode_atts( array(
       
    45     		'catid' => '',
       
    46     		'lenght' => '10',
       
    47     		'listclass' => 'aptools-list',
       
    48     		'header' => '2',
       
    49     		'headerclass' => 'aptools-list-header',
       
    50     		'excerpt' => 'false',
       
    51     		'image' => 'false',
       
    52     		'wrapper' => 'false',
       
    53     		'wrapperclass' => 'aptools-list-wrapper'
       
    54     	), $atts ) );
       
    55 
       
    56         $hopen = '<h'.$header.' class='.$headerclass.'>';
       
    57         $hclose = '</h'.$header.'>';
       
    58 
       
    59         if( $catid !== '' ):
       
    60             $args = array( 'category__in' => array($catid), 'posts_per_page' => $lenght );
       
    61         endif;
       
    62 
       
    63         $page = new WP_Query( $args );
       
    64         if( $page->have_posts() ):
       
    65             if( $wrapper !== 'false'){
       
    66                 echo '<div class="'.$wrapperclass.'">';
       
    67             }
       
    68             echo '<ul class="'.$listclass.'">';
       
    69             while( $page->have_posts() ):
       
    70                 $page->the_post();
       
    71                 echo '<li>';
       
    72                 echo '<a href="'.get_permalink().'">'.get_the_title().'</a>'; 
       
    73                 if( $image !== 'false' && has_post_thumbnail() ){ the_post_thumbnail( $image ); }
       
    74                 if( $excerpt === 'true' ) echo ' <span>'.get_the_excerpt().'</span>';
       
    75                 echo '</li>';
       
    76             endwhile;
       
    77             echo '</ul>';
       
    78             if( $wrapper !== 'false'){
       
    79                 echo '</div>';
       
    80             }
       
    81         endif;
       
    82         wp_reset_postdata();                
       
    83     }
       
    84 
       
    85     
       
    86     function showauto(){
       
    87         global $cat;
       
    88         $query_args = array(
       
    89             'post_type' => 'page',
       
    90             'meta_key' => 'aptools_archive_link',
       
    91             'meta_value' => $cat,
       
    92             'posts_per_page' => 1
       
    93         );
       
    94 
       
    95         $pages = new WP_Query( $query_args );
       
    96 
       
    97         if( $pages->have_posts() ):
       
    98             while( $pages->have_posts() ):
       
    99                 $pages->the_post();
       
   100                 echo '<h2>'.get_the_title().'</h2>';
       
   101                 echo '<div class="aptools-category-content">'.get_the_content().'</div>';
       
   102             endwhile;
       
   103         endif;
       
   104         wp_reset_postdata();                
       
   105 
       
   106     }
       
   107 
       
   108 }
       
   109 
       
   110 add_shortcode( 'showsingle', array( 'SWER_aptools_shortcodes', 'showsingle' ) );
       
   111 add_shortcode( 'showlist', array( 'SWER_aptools_shortcodes', 'showlist' ) );
       
   112 add_shortcode( 'showauto', array( 'SWER_aptools_shortcodes', 'showauto' ) );
       
   113 
       
   114 class SWER_aptools_admin{
       
   115 
       
   116     function __construct(){
       
   117         add_meta_box( 'aptools_archive_link', 'Category Pages & Posts', array( &$this, 'aptools_custom_metabox'), 'page', 'side', 'core' );        
       
   118     }
       
   119 
       
   120     function manage_pages_columns( $post_columns ){
       
   121         $post_columns['aptools'] = 'Category';
       
   122         return $post_columns;
       
   123     }
       
   124     
       
   125     function manage_pages_custom_column( $column, $post_id ){
       
   126         $selected = (int) get_post_meta( $post_id, 'aptools_archive_link', true );        
       
   127         
       
   128     	$category = &get_category( $selected );
       
   129     	if ( is_wp_error( $category ) ) return false;
       
   130 
       
   131         if( $category ):
       
   132         echo '<a 
       
   133             href="'.admin_url( 'edit-tags.php?action=edit&taxonomy=category&tag_ID='.$selected.'&post_type=post' ).'">'
       
   134             .$category->name
       
   135             .'</a>';
       
   136         endif;
       
   137     }
       
   138     
       
   139     function aptools_custom_metabox( $post ){
       
   140         $selected = get_post_meta( $post->ID, 'aptools_archive_link', true );
       
   141         #print_r($selected);
       
   142         wp_nonce_field( plugin_basename( __FILE__ ), 'aptools-nonce' );
       
   143         
       
   144         $args = array(
       
   145             'selected'          => $selected,
       
   146             'show_count'        => 0,
       
   147             'hide_empty'        => 1,
       
   148             'hierarchical'      => 1,
       
   149             'show_option_none' => '(None)',
       
   150             'name'              => 'aptools-metabox',
       
   151             'id'                => 'aptools-metabox',
       
   152             'taxonomy'          => 'category'
       
   153         );
       
   154         wp_dropdown_categories( $args );
       
   155         
       
   156         echo '<p>Link this page to a category, and use [showauto] shortcode in your category template to embed that page.</p>';
       
   157     }
       
   158     
       
   159     
       
   160     // update logic, same for manage_posts_custom_columns
       
   161     function save_post( $post_id ){
       
   162         if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) 
       
   163         return;
       
   164 
       
   165         if ( !isset($_POST[ 'aptools-nonce' ]) || !wp_verify_nonce( $_POST[ 'aptools-nonce' ], plugin_basename( __FILE__ ) ) )
       
   166         return;
       
   167         
       
   168         if ( 'page' == $_POST['post_type'] ){
       
   169            if ( !current_user_can( 'edit_page', $post_id ) )
       
   170                return;
       
   171          }else{
       
   172            if ( !current_user_can( 'edit_post', $post_id ) )
       
   173                return;
       
   174          }
       
   175           
       
   176         if( $_POST ):
       
   177         update_post_meta( $post_id, 'aptools_archive_link', $_POST['aptools-metabox'] );
       
   178         endif;
       
   179     }
       
   180     
       
   181     
       
   182     function category_add_form_fields( $tag ){
       
   183         $args = array(
       
   184             'selected'         => 0,
       
   185             'echo'             => 0,
       
   186             'name'             => 'aptools_page_id',
       
   187             'show_option_none' => '(None)'
       
   188         );
       
   189             
       
   190         echo '
       
   191             <div class="form-field">
       
   192             	<label for="tag-description">'._x('Category Pages & Posts', 'Category Pages & Posts').'</label>
       
   193             	'.wp_dropdown_pages( $args ).'
       
   194             	<p>'._('Link this category to a page, and use [showauto] shortcode in your category template to embed that page.').'</p>
       
   195             </div>
       
   196         ';
       
   197     }
       
   198     
       
   199     function category_edit_form_fields( $tag ){
       
   200 
       
   201         $query_args = array(
       
   202             'post_type' => 'page',
       
   203             'meta_key' => 'aptools_archive_link',
       
   204             'meta_value' => $tag->term_id,
       
   205             'posts_per_page' => 1
       
   206         );
       
   207         
       
   208         $selected = 0;
       
   209         $pages = new WP_Query( $query_args );
       
   210         if( $pages->have_posts() ):
       
   211             while( $pages->have_posts() ):
       
   212                 $pages->the_post();       
       
   213                 #echo 'This category is linked with <a href="'.admin_url('post.php?post='.get_the_ID().'&action=edit').'">'.get_the_title().'</a>';
       
   214                 $selected = get_the_ID();
       
   215 
       
   216             endwhile;
       
   217         endif;
       
   218         
       
   219         $pages_args = array(
       
   220             'selected'         => $selected,
       
   221             'echo'             => 0,
       
   222             'name'             => 'aptools_page_id',
       
   223             'show_option_none' => '(None)'
       
   224         );
       
   225         echo '
       
   226         
       
   227         <input type="hidden" name="aptools_pre_page_id" value="'.$selected  .'" />
       
   228     	<tr class="form-field">
       
   229 			<th scope="row" valign="top"><label for="aptools_page_id">Category Pages & Posts</label></th>
       
   230 			<td>'.wp_dropdown_pages( $pages_args ).'<br />
       
   231 			<span class="description">Link this category to a page, and use [showauto] shortcode in your category template to embed that page.</span>
       
   232 			</td>
       
   233 		</tr>            	
       
   234         ';
       
   235 
       
   236         wp_reset_postdata();
       
   237     }
       
   238     
       
   239     function admin_action_editedtag(){
       
   240         if( $_POST['aptools_pre_page_id'] !== $_POST['aptools_page_id'] ):            
       
   241             update_post_meta( $_POST['aptools_pre_page_id'], 'aptools_archive_link', '' );
       
   242             update_post_meta( $_POST['aptools_page_id'], 'aptools_archive_link', $_POST['tag_ID'] );
       
   243         endif;
       
   244     }
       
   245     
       
   246     
       
   247     function add_post_tag_columns($columns){
       
   248         $columns['atptools'] = 'Page';
       
   249         return $columns;
       
   250     }
       
   251     
       
   252     function add_post_tag_column_content($content, $column_name, $id){
       
   253         $query_args = array(
       
   254             'post_type' => 'page',
       
   255             'meta_key' => 'aptools_archive_link',
       
   256             'meta_value' => $id,
       
   257             'posts_per_page' => 1
       
   258         );
       
   259 
       
   260         $pages = new WP_Query( $query_args );
       
   261         if( $pages->have_posts() ):
       
   262             while( $pages->have_posts() ):
       
   263                 $pages->the_post();       
       
   264                 $content .= '<a href="'.admin_url('post.php?post='.get_the_ID().'&action=edit').'">'.get_the_title().'</a>';
       
   265             endwhile;
       
   266         endif;
       
   267 
       
   268         return $content;
       
   269     }    
       
   270 }
       
   271 
       
   272 function call_SWER_aptools_admin(){
       
   273     return new SWER_aptools_admin();
       
   274 }
       
   275 
       
   276 add_action( 'add_meta_boxes', 'call_SWER_aptools_admin' );
       
   277 
       
   278 add_action( 'admin_action_editedtag' ,          array( 'SWER_aptools_admin', 'admin_action_editedtag' ) );
       
   279 add_action( 'category_add_form_fields',         array( 'SWER_aptools_admin', 'category_add_form_fields' ) );
       
   280 add_action( 'category_edit_form_fields',        array( 'SWER_aptools_admin', 'category_edit_form_fields' ) );
       
   281 add_filter( 'manage_edit-category_columns',     array( 'SWER_aptools_admin', 'add_post_tag_columns' ) );
       
   282 add_filter( 'manage_category_custom_column',    array( 'SWER_aptools_admin', 'add_post_tag_column_content' ), 10, 3 );
       
   283 add_filter( 'manage_pages_columns',             array( 'SWER_aptools_admin', 'manage_pages_columns' ) );
       
   284 add_action( 'manage_pages_custom_column',       array( 'SWER_aptools_admin', 'manage_pages_custom_column' ), 10, 2);
       
   285 add_action( 'save_post',                        array( 'SWER_aptools_admin', 'save_post' ) );
       
   286 
       
   287 
       
   288 ?>