└── re-skinning.content.splitter.php /re-skinning.content.splitter.php: -------------------------------------------------------------------------------- 1 | post_types = apply_filters( 'autopaging_post_types', get_option( $this->option_name_post_types, $this->post_types_default ) ); 36 | $this->num_pages = absint( apply_filters( 'autopaging_num_pages_default', get_option( $this->option_name_num_pages, $this->num_pages_default ) ) ); 37 | if ( 0 == $this->num_pages ) 38 | $this->num_pages = $this->num_pages_default; 39 | $this->num_words = absint( apply_filters( 'autopaging_num_words_default', get_option( $this->option_name_num_words, $this->num_words_default ) ) ); 40 | if ( 0 == $this->num_words ) 41 | $this->num_words = $this->num_words_default; 42 | } 43 | public function uninstall() { 44 | delete_option( 'autopaging_post_types' ); 45 | delete_option( 'autopaging_paging_type' ); 46 | delete_option( 'autopaging_num_pages' ); 47 | delete_option( 'autopaging_num_words' ); 48 | } 49 | public function filter_plugin_action_links( $actions, $file ) { 50 | if ( false !== strpos( $file, basename( __FILE__ ) ) ) 51 | $actions[ 'settings' ] = 'Settings'; 52 | 53 | return $actions; 54 | } 55 | public function action_admin_init() { 56 | register_setting( 'reading', $this->option_name_post_types, array( $this, 'sanitize_supported_post_types' ) ); 57 | register_setting( 'reading', $this->option_name_paging_type, array( $this, 'sanitize_paging_type' ) ); 58 | register_setting( 'reading', $this->option_name_num_pages, array( $this, 'sanitize_num_pages' ) ); 59 | register_setting( 'reading', $this->option_name_num_words, array( $this, 'sanitize_num_words' ) ); 60 | add_settings_section( 'autopaging', __( 'Automatically Paginate Posts', 'autopaging' ), '__return_false', 'reading' ); 61 | add_settings_field( 'autopaging-post-types', __( 'Supported post types:', 'autopaging' ), array( $this, 'settings_field_post_types' ), 'reading', 'autopaging' ); 62 | add_settings_field( 'autopaging-paging-type', __( 'Split post by:', 'autopaging' ), array( $this, 'settings_field_paging_type' ), 'reading', 'autopaging' ); 63 | } 64 | public function settings_field_post_types() { 65 | $post_types = get_post_types( array( 66 | 'public' => true 67 | ), 'objects' ); 68 | unset( $post_types[ 'attachment' ] ); 69 | $current_types = get_option( $this->option_name_post_types, $this->post_types_default ); 70 | foreach ( $post_types as $post_type => $atts ) : 71 | ?> 72 | />
73 | true 81 | ) ); 82 | unset( $post_types[ 'attachment' ] ); 83 | foreach ( $post_types_checked as $post_type ) { 84 | if ( array_key_exists( $post_type, $post_types ) ) 85 | $post_types_sanitized[] = $post_type; 86 | } 87 | } 88 | return $post_types_sanitized; 89 | } 90 | public function settings_field_paging_type() { 91 | $paging_type = get_option( $this->option_name_paging_type, $this->paging_type_default ); 92 | if ( ! in_array( $paging_type, $this->paging_types_allowed ) ) { 93 | $paging_type = $this->paging_type_default; 94 | } 95 | $labels = array( 96 | 'pages' => __( 'Total number of pages: ', 'autopaging' ), 97 | 'words' => __( 'Approximate words per page: ', 'autopaging' ), 98 | ); 99 | foreach ( $this->paging_types_allowed as $type ) : 100 | $type_escaped = esc_attr( $type ); 101 | $func = 'settings_field_num_' . $type; 102 | ?> 103 |

/>

106 | paging_types_allowed ) ? $type : $this->paging_type_default; 110 | } 111 | public function settings_field_num_pages() { 112 | $num_pages = get_option( $this->option_name_num_pages, $this->num_pages_default ); 113 | $max_pages = apply_filters( 'autopaging_max_num_pages', 10 ); 114 | ?> 115 | 120 | option_name_num_words ) ) 127 | ?> 128 | 129 |

130 | post_types as $post_type ) { 141 | add_meta_box( 'autopaging', __( 'Post Autopaging', 'autopaging' ), array( $this, 'meta_box_autopaging' ), $post_type, 'side' ); 142 | } 143 | } 144 | public function meta_box_autopaging( $post ) { 145 | ?> 146 |

147 | ID, $this->meta_key_disable_autopaging, true ) ); ?> /> 148 |

149 |

150 |

<!--nextpage-->' ); ?>

151 | meta_key_disable_autopaging, $this->meta_key_disable_autopaging . '_wpnonce' ); 153 | } 154 | public function action_save_post( $post_id ) { 155 | if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) 156 | return; 157 | if ( isset( $_POST[ $this->meta_key_disable_autopaging . '_wpnonce' ] ) && wp_verify_nonce( $_POST[ $this->meta_key_disable_autopaging . '_wpnonce' ], $this->meta_key_disable_autopaging ) ) { 158 | $disable = isset( $_POST[ $this->meta_key_disable_autopaging ] ) ? true : false; 159 | if ( $disable ) 160 | update_post_meta( $post_id, $this->meta_key_disable_autopaging, true ); 161 | else 162 | delete_post_meta( $post_id, $this->meta_key_disable_autopaging ); 163 | } 164 | } 165 | public function filter_the_posts( $posts ) { 166 | if ( ! is_admin() ) { 167 | foreach( $posts as $the_post ) { 168 | if ( in_array( $the_post->post_type, $this->post_types ) && ! preg_match( '##i', $the_post->post_content ) && ! (bool) get_post_meta( $the_post->ID, $this->meta_key_disable_autopaging, true ) ) { 169 | $num_pages = absint( apply_filters( 'autopaging_num_pages', absint( $this->num_pages ), $the_post ) ); 170 | $num_words = absint( apply_filters( 'autopaging_num_words', absint( $this->num_words ), $the_post ) ); 171 | if ( $num_pages < 2 && empty( $num_words ) ) 172 | continue; 173 | $content = $the_post->post_content; 174 | $content = preg_replace( '#

(.+?)

#i', "$1\r\n\r\n", $content ); 175 | $content = preg_replace( '##i', "\r\n", $content ); 176 | $count = preg_match_all( '#\r\n\r\n#', $content, $matches ); 177 | if ( is_int( $count ) && 0 < $count ) { 178 | $content = explode( "\r\n\r\n", $content ); 179 | switch ( get_option( $this->option_name_paging_type, $this->paging_type_default ) ) { 180 | case 'words' : 181 | $word_counter = 0; 182 | foreach ( $content as $index => $paragraph ) { 183 | $paragraph_words = count( preg_split( '/\s+/', strip_tags( $paragraph ) ) ); 184 | $word_counter += $paragraph_words; 185 | if ( $word_counter >= $num_words ) { 186 | $content[ $index ] .= ''; 187 | $word_counter = 0; 188 | } else { 189 | continue; 190 | } 191 | } 192 | unset( $word_counter ); 193 | unset( $index ); 194 | unset( $paragraph ); 195 | unset( $paragraph_words ); 196 | break; 197 | case 'pages' : 198 | default : 199 | $count = count( $content ); 200 | $insert_every = $count / $num_pages; 201 | $insert_every_rounded = round( $insert_every ); 202 | if ( $num_pages > $count ) { 203 | $insert_every_rounded = 1; 204 | } 205 | $i = $count - 1 == $num_pages ? 2 : 1; 206 | foreach ( $content as $key => $value ) { 207 | if ( $key + 1 == $count ) { 208 | break; 209 | } 210 | 211 | if ( ( $key + 1 ) == ( $i * $insert_every_rounded ) ) { 212 | $content[ $key ] = $content[ $key ] . ''; 213 | $i++; 214 | } 215 | } 216 | unset( $count ); 217 | unset( $insert_every ); 218 | unset( $insert_every_rounded ); 219 | unset( $key ); 220 | unset( $value ); 221 | break; 222 | } 223 | $content = implode( "\r\n\r\n", $content ); 224 | $the_post->post_content = $content; 225 | } 226 | unset( $num_pages ); 227 | unset( $num_words ); 228 | unset( $content ); 229 | unset( $count ); 230 | } 231 | } 232 | } 233 | 234 | return $posts; 235 | } 236 | } 237 | new Automatically_Paginate_Posts; 238 | 239 | --------------------------------------------------------------------------------