Skip to content

Instantly share code, notes, and snippets.

@mattmischuk
Created November 20, 2014 20:00
Show Gist options
  • Save mattmischuk/b9884bdcc728bb91b580 to your computer and use it in GitHub Desktop.
Save mattmischuk/b9884bdcc728bb91b580 to your computer and use it in GitHub Desktop.
AntiFramework
<?php
/*
AntiFramework Class ( http://antiframework.com/anti-framework )
Copyright: (c) 2011 AntiFramework.
License: GNU/GPL Version 2 or later. http://www.gnu.org/licenses/gpl.html
*/
class AntiFramework{
public $_default_options_panel_array, $_default_metaboxes_array, $_widgets;
function __construct(){
require_once(AF_METADATA_PATH . "/options.php");
require_once(AF_METADATA_PATH . "/metaboxes.php");
$this->_default_options_panel_array = $antiframework_options;
$this->_default_metaboxes_array = $metaboxes;
if(!is_array(get_option("antiframework_options"))){
add_option("antiframework_options",$this->getOptions());
}
$this->registerFrameworkPostType();
}
public function registerFrameworkPostType(){
$labels = array(
'name' => __( 'Pica Framework Post' )
);
$args = array(
'labels' => $labels,
'public' => true,
'show_ui' => false,
'show_in_menu' => false,
'show_in_nav_menus' => false,
'query_var' => false,
'rewrite' => false,
'capability_type' => 'post',
'has_archive' => false,
'hierarchical' => false,
'supports' => array( 'title', 'editor' )
);
register_post_type('antiframework',$args);
}
public function createPages(){
//add_object_page(get_current_theme(), get_current_theme(), 'manage_options','antiframework', array($this,"printThemeOptionsPage"), $icon);
//add_submenu_page("antiframework", get_current_theme(), "Theme Options", "manage_options", "antiframework", array($this,"printThemeOptionsPage"));
}
public function loadAssets(){
$_current_page = "";
if((isset( $_REQUEST["page"]))){
$_current_page = strtolower(strip_tags(trim( $_REQUEST["page"])));
}
wp_register_script('af-admin-jquery-plugins', AF_CORE_URL . "/js/jquery-plugins.js", false, '1.0.0');
wp_enqueue_script('af-admin-jquery-plugins');
wp_register_style('af-admin-styles', AF_CORE_URL . "/css/admin-styles.css", false, '1.0.0');
wp_enqueue_style('af-admin-styles');
if(($_current_page != "" && substr( $_current_page, 0, 2 ) == "af" )){
wp_enqueue_script('media-upload');
wp_enqueue_style('thickbox');
wp_register_style('af-options-panel-styles', AF_CORE_URL . "/css/options-panel-styles.css", false, '1.0.0');
wp_enqueue_style('af-options-panel-styles');
}
add_action("admin_print_footer_scripts", "add_antiframework_admin_scripts", 99);
wp_register_style('af-metaboxes-styles', AF_CORE_URL . "/css/metaboxes-styles.css", false, '1.0.0');
wp_enqueue_style('af-metaboxes-styles');
}
public function printThemeOptionsPage(){
// we're saving the options
if(isset($_POST["save-options"])){
$this->setOptions($_POST);
}
// we're resetting the options
elseif(isset($_POST["reset-options"])){
$this->resetOptions();
af_partial("options/panel",array(
"panel_array" => $this->getOptionsPanelarray(),
"admin" => true
));
}
// we're deleting a metabox or option file
elseif(isset($_GET["delete-wp-attachment-post"])){
wp_delete_post($_GET["delete-wp-attachment-post"], true);
}
// we're requesting an updated metabox or option file
elseif(isset($_GET["item_name"])){
switch($_GET["item_type"]){
case "option" :
$option = $this->getOption($_GET["item_name"]);
af_partial("options/option_upload",array(
"option" => $option,
"admin" => true
));
break;
case "metabox" :
$metabox = $this->getMetabox(array(
"item_name" => $_GET["item_name"],
"post_grandparent" => $_GET["post_grandparent"]
));
af_partial("metaboxes/metabox_upload",array(
"metabox" => $metabox,
"admin" => true
));
break;
}
}
// we're simply reaching the options panel page
else{
af_partial("options/panel",array(
"panel_array" => $this->getOptionsPanelarray(),
"admin" => true
));
}
}
public function getOption($o_key, $option = ""){
global $wpdb;
// single option request
if($option == ""){
$o_key = strtr($o_key, "-", "_");
$options_panel_array = $this->_default_options_panel_array;
foreach($options_panel_array as $array_tab_key => $array_tab){
foreach($array_tab["tab_sections"] as $array_section_key => $array_section){
foreach($array_section["section_options"] as $array_option_key => $array_option){
if($array_option_key == $o_key){
$option = $array_option;
break;
}
if($option != ""){ break; }
}
if($option != ""){ break; }
}
if($option != ""){ break; }
}
$option['is_single'] = true;
}
$option['id'] = strtr($o_key, "_", "-");
if($option['type'] == "upload"){
$query_args = array(
'post_name' => $option['id'],
'post_type' => 'antiframework',
'post_status' => 'draft',
'comment_status' => 'closed',
'ping_status' => 'closed'
);
$query = 'SELECT ID FROM ' . $wpdb->posts . ' WHERE post_parent = 0';
foreach($query_args as $column_name => $post_value){
$query .= ' AND ' . $column_name . ' = "' . $post_value . '"';
}
$query .= ' LIMIT 1';
$post_parent = $wpdb->get_row($query);
if(count($post_parent)){
$post_parent = $post_parent->ID;
} else {
$_words = explode( '_', $option['id'] );
$_title = join( ' ', $_words );
$_title = ucwords( $_title );
$_post_data = array( 'post_title' => $_title );
$_post_data = array_merge($_post_data, $query_args);
$post_parent = wp_insert_post($_post_data);
}
$option['post_parent'] = $post_parent;
$uploaded_files = get_posts(array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_status' => null,
'orderby' => 'post_date',
'order' => 'DESC',
'post_parent' => $option['post_parent']
));
if(count($uploaded_files)){
$active_file = $uploaded_files[0];
unset($uploaded_files[0]);
foreach($uploaded_files as $uploaded_file){
wp_delete_post($uploaded_file->ID, true);
}
$option['value'] = $active_file->ID;
}
else {
$option['value'] = '';
}
}
else {
$option['value'] = $this->getOptionValue($option['id']);
}
return $option;
}
public function getOptionValue($option_id){
$options_db_values = get_option("antiframework_options");
$option_value = (is_array($options_db_values) && array_key_exists($option_id, $options_db_values)) ? $options_db_values[$option_id] : "";
return $option_value;
}
public function getOptionsPanelarray(){
global $wpdb;
$options_panel_array = $this->_default_options_panel_array;
// loop trough the first level (tabs)
foreach($options_panel_array as $t_key => $tab){
$options_panel_array[$t_key]['id'] = strtr($t_key, "_", "-");
// loop trough second level (sections)
foreach($tab["tab_sections"] as $s_key => $section){
$options_panel_array[$t_key]['tab_sections'][$s_key]['id'] = strtr($s_key, "_", "-");
// loop trough third level (options)
foreach($section["section_options"] as $o_key => $option){
$option = $this->getOption($o_key, $option);
$options_panel_array[$t_key]['tab_sections'][$s_key]['section_options'][$o_key] = $option;
}
}
}
return $options_panel_array;
}
public function getOptions($default = false){
$options_panel_array = $this->_default_options_panel_array;
if(!is_array(get_option("antiframework_options")) || $default == true){
$options = array();
foreach($options_panel_array as $t_key => $tab){
foreach($tab["tab_sections"] as $s_key => $section){
foreach($section["section_options"] as $option){
$options[$option['id']] = $option["value"];
}
}
}
}
else{
$options = get_option("antiframework_options");
}
return $options;
}
public function setOptions($_POST){
unset($_POST["save-options"]);
$_POST = array_map('stripslashes', $_POST);
update_option("antiframework_options",$_POST);
}
public function resetOptions(){
global $wpdb;
$query_args = array(
'post_type' => 'antiframework',
'post_status' => 'draft',
'comment_status' => 'closed',
'ping_status' => 'closed'
);
$query = 'SELECT ID FROM ' . $wpdb->posts;
$query_txt = ' WHERE ';
foreach($query_args as $column_name => $post_value){
$query .= $query_txt . $column_name . ' = "' . $post_value . '"';
$query_txt = ' AND ';
}
$query .= " AND post_name NOT LIKE '%-post-id-%'";
$framework_posts = $wpdb->get_results($query);
if($framework_posts){
foreach($framework_posts as $framework_post){
$subquery_args = array(
'numberposts' => -1,
'post_parent' => $framework_post->ID,
'post_type' => 'attachment'
);
$attachments = get_children($subquery_args);
if($attachments){
foreach($attachments as $attachment){
wp_delete_post($attachment->ID, true);
}
}
}
}
}
public function getSkins(){
$skin_directories = scandir(AF_THEME_SKINS_PATH,0);
$skins_array = array();
foreach($skin_directories as $skin){
if(!stristr($skin, ".")){
$skin_directory = AF_THEME_SKINS_PATH . "/" . $skin;
$skin_name = str_replace(array("-", "_"), array(" ", " "), $skin);
if(preg_match("[ _]", $subject) != false){
$skin = str_replace(array(" ", "_"), array("-","-"), $skin);
rename($skin_directory,str_replace(array(" ", "_"), array("-","-"), $skin_directory));
}
$skins_array[$skin] = $skin_name;
}
}
return $skins_array;
}
public function getMetabox($m_key, $metabox = ""){
global $wpdb, $post;
$post_id = $post->ID;
// single metabox request
if(is_array($m_key) && $metabox == ""){
$post_id = $m_key['post_grandparent'];
$m_key = strtr($m_key["item_name"],"-","_");
$metabox = $this->_default_metaboxes_array[$m_key];
$metabox['is_single'] = true;
}
$metabox['id'] = strtr($m_key,"_","-");
if($metabox['type'] == "upload"){
$post_name = $metabox['id'] . "-post-id-" . $post_id;
$query_args = array(
'post_parent' => $post_id,
'post_name' => $post_name,
'post_type' => 'antiframework',
'post_status' => 'draft',
'comment_status' => 'closed',
'ping_status' => 'closed'
);
$query = 'SELECT ID FROM ' . $wpdb->posts;
$query_txt = ' WHERE ';
foreach($query_args as $column_name => $post_value){
$query .= $query_txt . $column_name . ' = "' . $post_value . '"';
$query_txt = ' AND ';
}
$query .= ' LIMIT 1';
$post_parent = $wpdb->get_row($query);
if(count($post_parent)){
$post_parent = $post_parent->ID;
} else {
$_words = explode( '_', $post_name );
$_title = join( ' ', $_words );
$_title = ucwords( $_title );
$_post_data = array( 'post_title' => $_title );
$_post_data = array_merge($_post_data, $query_args);
$post_parent = wp_insert_post($_post_data);
}
$metabox['post_parent'] = $post_parent;
$uploaded_files = get_posts(array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_status' => null,
'orderby' => 'post_date',
'order' => 'DESC',
'post_parent' => $metabox['post_parent']
));
if(count($uploaded_files)){
$active_file = $uploaded_files[0];
unset($uploaded_files[0]);
foreach($uploaded_files as $uploaded_file){
wp_delete_post($uploaded_file->ID, true);
}
$metabox['value'] = $active_file->ID;
}
else{
$metabox['value'] = "";
}
}
else {
$metabox['value'] = get_post_meta($post_id, $metabox['id'], TRUE);
}
return $metabox;
}
public function getMetaboxesGroupedByPostType(){
global $wpdb, $post;
$post_id = $post->ID;
$metaboxes_array = $this->_default_metaboxes_array;
$post_types = array();
$metaboxes_by_post_type = array();
$ignored_post_types = array("attachment", "revision", "nav_menu_item");
foreach($metaboxes_array as $m_key => $metabox){
$metabox = $this->getMetabox($m_key, $metabox);
// template specific metaboxes check
if(array_key_exists("template", $metabox)){
$template_file = get_post_meta($post_id,'_wp_page_template', TRUE);
// check if the current metabox is meant to be used
// with the current template
if($template_file != $metabox['template']){
continue;
}
}
// post type specific metaboxes check
if(array_key_exists('post_types', $metabox)) {
$post_types = $metabox['post_types'];
if(!is_array($post_types)) $post_types = explode('!', $post_types);
} else {
// post type not specified, pick the ones that make sense by default
$all_post_types = get_post_types();
$post_types = array();
foreach($all_post_types as $post_type){
if(!in_array($post_type, $ignored_post_types)){
$post_types[] = $post_type;
}
}
}
// return the metaboxes grouped by post type
foreach($post_types as $post_type) {
if(is_array($metaboxes_by_post_type[$post_type])) {
// post type group exists, add metabox
$metaboxes_by_post_type[$post_type][] = $metabox;
}
else {
// create new post type group adding the metabox in the array
$metaboxes_by_post_type[$post_type] = array($metabox);
}
}
}
return $metaboxes_by_post_type;
}
public function hookMetaboxes(){
if(function_exists('add_meta_box')){
$metaboxes_by_post_type = $this->getMetaboxesGroupedByPostType();
foreach($metaboxes_by_post_type as $post_type => $metaboxes_group){
$name = ucwords($post_type);
add_meta_box('antiframework-metaboxes', "$name Metaboxes", array($this, "printMetaboxes"), $post_type, "normal", "default", $metaboxes_group);
}
}
}
public function printMetaboxes($post, $metaboxes_data){
$metaboxes = $metaboxes_data["args"];
af_partial("metaboxes/metaboxes_group", array(
"metaboxes" => $metaboxes,
"admin" => true
));
}
public function parseMetaboxes(){
global $wpdb, $post;
// don't parse on autosave
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
return $post_id;
// check permissions
if('page' == $_POST['post_type']){
if (!current_user_can('edit_page', $post_id)){
return $post_id;
}
} else {
if (!current_user_can('edit_post',$post_id)){
return $post_id;
}
}
/*** we're authenticated: parse and save the metaboxes data ***/
// is this is not wordpress saving an auto-draft, let's remove
// the auto-drafts it previously created
$metaboxes = $this->_default_metaboxes_array;
foreach ($metaboxes as $m_key => $metabox) :
$this->parseMetabox($m_key, $metabox);
endforeach;
}
public function parseMetabox($m_key, $metabox){
$metabox = $this->getMetabox($m_key, $metabox);
$post_id = $_POST['post_ID'];
$current_metabox_value = get_post_meta($post_id, $metabox['id'], true);
$posted_metabox_value = $_POST[$metabox['id']];
switch($metabox["type"]){
case "text" :
case "textarea" :
case "select" :
case "checkbox" :
$metabox['value'] = $posted_metabox_value;
break;
case "content" :
$metabox['value'] = wpautop($posted_metabox_value);
break;
/* upload's value is set in the getMetabox method,
the value of $posted_metabox_value is not relevant */
}
if($current_metabox_value == "" && $metabox['value'] != ""){
add_post_meta($post_id, $metabox['id'], $metabox['value'], true);
}
elseif($current_metabox_value != "" && $metabox['value'] != "" && $current_metabox_value != $metabox['value']){
update_post_meta($post_id, $metabox['id'], $metabox['value']);
}
elseif($current_metabox_value != "" && $metabox['value'] == ""){
delete_post_meta($post_id, $metabox['id'], $current_metabox_value);
}
return $metabox;
}
}
function af_add_gravatar($avatar_defaults){
$my_avatar = get_bloginfo("template_url") . '/img/no-avatar.png';
$avatar_defaults[$my_avatar] = "Custom Gravatar";
return $avatar_defaults;
}
function af_register_widgets(){
$dir = TEMPLATEPATH . "/widgets/";
$ext = "php";
$widgets = array();
$opened_dir = opendir($dir);
while($widget = readdir($opened_dir)){
$fext = substr($widget,strlen($ext)*-1);
if(($widget!='.') && ($widget!='..') && ($fext==$ext)){
$widget_extension = strrchr($widget,'.');
if($widget_extension !== false) {
$widget = substr($widget, 0, -strlen($widget_extension));
array_push($widgets, $widget);
}
include($dir . $widget . "." . $ext);
}
}
closedir($opened_dir);
foreach ($widgets as $widget) {
register_widget($widget);
}
}
if(!function_exists('scandir')) {
function scandir($directory,$sorting_order = 0) {
$dh = opendir($directory);
while(false !== ($filename = readdir($dh)) ) {
$files[] = $filename;
}
if($sorting_order == 0) {
sort($files);
} else {
rsort($files);
}
return($files);
}
}
function af_partial($file, $locals = array()){
extract($locals);
$file_path = ($admin == true) ? AF_ADMIN_PARTIALS_PATH . "/" . $file . ".php" : AF_THEME_PARTIALS_PATH . "/" . $file . ".php";
$real_file_path = substr_replace($file_path, "/_", strrpos($file_path, "/"), strlen("/"));
include($real_file_path);
}
function af_option($option_id){
$options_db_values = get_option("antiframework_options");
$option_value = (is_array($options_db_values) && array_key_exists($option_id, $options_db_values)) ? $options_db_values[$option_id] : "";
echo $option_value;
}
function af_get_option($option_id){
$options_db_values = get_option("antiframework_options");
$option_value = (is_array($options_db_values) && array_key_exists($option_id, $options_db_values)) ? $options_db_values[$option_id] : "";
return $option_value;
}
function af_seo_title(){
if (is_home()) {
bloginfo('name'); echo " &mdash; "; bloginfo('description');
}
elseif (is_category()) {
single_cat_title(); echo " &mdash; "; bloginfo('name');
}
elseif (is_single() || is_page()) {
single_post_title(); echo " &mdash; "; bloginfo('name');
}
elseif (is_search()) {
echo "Search Results For: "; echo wp_specialchars($s); echo " &mdash; "; bloginfo('name');
}
elseif (is_tag()) {
echo "Tagged as: "; echo wp_specialchars($s); echo " &mdash; "; bloginfo('name');
}
elseif (is_404()) {
bloginfo('name'); echo " &mdash; Page Not Found!";
}
else {
bloginfo('name'); echo " &mdash; "; bloginfo('description');
}
}
function af_wp_paged_navigation($pages_to_show = 5, $pre_html = '<div class="paged_navigation">', $post_html = '</div>')
{
global $wpdb,$wp_query;
$request=$wp_query->request;
$posts_per_page=intval(get_query_var('posts_per_page'));
$paged=intval(get_query_var('paged'));
$numposts=$wp_query->found_posts;
$max_page=$wp_query->max_num_pages;
if(empty($paged) || $paged==0) {
$paged=1;
}
$pages_to_show_minus_1=$pages_to_show-1;
$half_page_start=floor($pages_to_show_minus_1/2);
$start_page=$paged-$half_page_start;
$pages_left=$max_page-$paged;
if($start_page <= 0) {
$start_page=1;
}
$end_page=$paged;
if(($end_page-$start_page)!=$pages_to_show_minus_1) {
$end_page=$start_page+$pages_to_show_minus_1;
}
if($end_page>$max_page) {
$start_page=$max_page-$pages_to_show_minus_1;
$end_page=$max_page;
}
if($start_page<=0) {
$start_page=1;
}
if($max_page>1) {
echo $pre_html;
echo '<span class="current_page_description">Page '.$paged.' of '.$end_page.'</span>';
if ($paged>=$pages_to_show_minus_1) {
echo '<a href="'.clean_url(get_pagenum_link()).'" class="first" title="first">&laquo; FIRST</a>';
echo "<span class='dots'>...</span>";
}
previous_posts_link("&laquo;",$max_page);
for($i=$start_page;$i<=$end_page;$i++) {
if($i==$paged) {echo '<span class="current_page">'.$i.'</span>';}
else {echo '<a href="'.clean_url(get_pagenum_link($i)).'" class="page_link" title="'.$i.'">'.$i.'</a>';}
}
next_posts_link("&raquo;",$max_page);
if ($pages_left>$half_page_start) {
echo "<span class='dots'>...</span>";
echo '<a href="'.clean_url(get_pagenum_link($max_page)).'" class="last" title="last">LAST &raquo;</a>';
}
echo $post_html;
}
}
function af_breadcrumb($sep="&nbsp;&nbsp;&raquo;&nbsp;&nbsp;",$tag_pre="Posts Tagged as ",$date_pre="Posts Published in ",$author_pre="Author ",$search_pre="Search Results for "){
global $wp_query,$post;
$bread_txt=array();
$bread_txt['home']="Home";
$hometext=$bread_txt['home'].$sep."All Posts";
$homelink='<a href="'.get_bloginfo('url').'">'.$bread_txt['home'].'</a>';
$output="";
if (is_home()) {
$output=$hometext;
} else {
$output.=$homelink.$sep;
if (!is_page()) {
if (is_single()) {
global $post;
$cats = (is_attachment()) ? get_the_category($post->post_parent) : get_the_category();
$cat = $cats[0];
if (is_object($cat)) {
if($cat->parent!=0) {
$output .= get_category_parents($cat->term_id,true,$sep);
} else {
$output .= '<a href="'.get_category_link($cat->term_id).'">'.$cat->name.'</a>'.$sep;
}
if(is_attachment()){
$output .= '<a href="'.get_permalink($post->post_parent).'">'.get_the_title($post->post_parent).'</a>'.$sep."Slideshow";
} else {
$output .= get_the_title();
}
}
}
if (is_category()) {
$category=$wp_query->get_queried_object();
$parent=($category->category_parent) ? $category->category_parent : $cat;
$parent_cat_id=intval($parent);
$parents=get_category_parents($parent_cat_id,TRUE,$sep,FALSE);
if (!is_wp_error($parents)) {
$output.=$parents;
}
$output.=$category->cat_name;
} elseif (is_tag()) {
$output.=$tag_pre.single_cat_title('',false);
} elseif (is_date()) {
$output.=$date_pre.single_month_title(' ',false);
} elseif (is_author()) {
$user=get_userdatabylogin($wp_query->query_vars['author_name']);
$output.=$author_pre.$user->display_name;
} elseif (is_search()) {
$output.=$search_pre.get_search_query();
} else if (is_tax()) {
$taxonomy=get_taxonomy(get_query_var('taxonomy'));
$term=get_query_var('term');
$output.=$taxonomy->label.$term;
}
} else {
$post=$wp_query->get_queried_object();
if (0==$post->post_parent) {
$output=$homelink.$sep.get_the_title();
} else {
if (isset($post->ancestors)) {
if (is_array($post->ancestors))
$ancestors=array_values($post->ancestors);
else
$ancestors=array($post->ancestors);
} else {
$ancestors=array($post->post_parent);
}
$ancestors=array_reverse($ancestors);
$ancestors[]=$post->ID;
$links=array();
foreach ($ancestors as $ancestor) {
$tmp =array();
$tmp['title']=strip_tags( get_the_title( $ancestor ) );
$tmp['url']=get_permalink($ancestor);
$tmp['cur']=false;
if ($ancestor==$post->ID) {
$tmp['cur']=true;
}
$links[]=$tmp;
}
$output=$homelink;
foreach ($links as $link) {
$output.=$sep;
if (!$link['cur']) {
$output.='<a href="'.$link['url'].'">'.$link['title'].'</a>';
} else {
$output .=$link['title'];
}
}
}
}
if(is_404()){
$output=$homelink.$sep." page not found";
}
}
echo $output;
}
function af_current_url(){
$af_current_url = 'http';
if ($_SERVER["HTTPS"] == "on") {$af_current_url .= "s";}
$af_current_url .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$af_current_url .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$af_current_url .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
if (strpos($af_current_url, "?")) $af_current_url = strstr($af_current_url, '?', true);
return $af_current_url;
}
function af_get_posts($posts_per_page = 20, $all_posts){
$starting_index = (isset($_GET['index']) && trim($_GET['index']) != "" ) ? $_GET['index'] : 0;
$ending_index = $starting_index + $posts_per_page;
$requested_posts = array();
for($c = $starting_index; $c < $ending_index; $c++){
if($all_posts[$c]) $requested_posts[] = $all_posts[$c];
}
$posts_data = array(
'posts' => $requested_posts,
'info' => array(
'current_index' => $starting_index,
'total_posts_n' => count($all_posts)
)
);
return $posts_data;
}
function af_get_attachment_posts($posts_per_page = 20 ,$parent_posts, $attachment_type = "image"){
$starting_index = (isset($_GET['index'])) ? $_GET['index'] : 0;
$ending_index = $starting_index + $posts_per_page;
$all_attachment_posts = array();
foreach ($parent_posts as $post) :
setup_postdata($post);
$args = array(
'order' => 'ASC',
'post_type' => 'attachment',
'post_parent' => $post->ID,
'post_mime_type' => $attachment_type,
'post_status' => null,
'numberposts' => -1,
);
$post_attachments = get_posts($args);
$all_attachment_posts = array_merge($all_attachment_posts,$post_attachments);
endforeach;
$requested_posts = array();
if($posts_per_page == -1){
$requested_posts = $all_attachment_posts;
}
else {
for($c = $starting_index; $c < $ending_index; $c++){
if($all_attachment_posts[$c]) $requested_posts[] = $all_attachment_posts[$c];
}
}
$posts_data = array(
'posts' => $requested_posts,
'info' => array(
'current_index' => $starting_index,
'total_posts_n' => count($all_attachment_posts)
)
);
return $posts_data;
}
function af_get_items_pagination($get_params, $items_n, $items_per_page = 20, $visible_pagination_links_n = 5, $base_url = ""){
if($base_url == "") $base_url = af_current_url();
if($items_n > $items_per_page) {
$starting_item_index = ($get_params['index']) ? $get_params['index'] : 0;
$items_pages_n = ceil( $items_n / $items_per_page );
$all_pagination_links = array();
for($cur_nav_link_index = 0 ; $cur_nav_link_index < $items_pages_n; $cur_nav_link_index ++)
{
if( $cur_nav_link_index == ($starting_item_index / $items_per_page) ) {
$cur_nav_link_class = 'selected';
$selected_nav_link_index = $cur_nav_link_index;
$selected_nav_link_n = $cur_nav_link_index+1;
} else {
$cur_nav_link_class = '';
}
$current_page_n = $cur_nav_link_index + 1;
$get_params['index'] = $cur_nav_link_index * $items_per_page;
$get_params_string = http_build_query($get_params, '', '&amp;');
$get_params_string = "?".$get_params_string;
$all_pagination_links[] = array(
'url' => $base_url . $get_params_string,
'text' => $current_page_n,
'class' => $cur_nav_link_class
);
}
$last_nav_link_index = $cur_nav_link_index - 1;
$lr_nav_links_block = ($visible_pagination_links_n - 1) / 2;
if($selected_nav_link_index < $lr_nav_links_block)
{
// we are in the first lr_nav_links_block
$starting_nav_item_index = 0;
$ending_nav_item_index = $visible_pagination_links_n -1;
}
elseif($selected_nav_link_index >= ( count($all_pagination_links) - $lr_nav_links_block) )
{
// we are in the last lr_nav_links_block
$nav_links_left = $items_pages_n - ($selected_nav_link_index + 1);
$linksto_add_to_l_block = $lr_nav_links_block - $nav_links_left;
$starting_nav_item_index = $selected_nav_link_index - ($lr_nav_links_block + $linksto_add_to_l_block);
$ending_nav_item_index = $selected_nav_link_index + $nav_links_left;
} else {
// default situation, in the middle of the navigation with both lr_nav_links_block visible
$starting_nav_item_index = $selected_nav_link_index - $lr_nav_links_block;
$ending_nav_item_index = $selected_nav_link_index + $lr_nav_links_block;
}
$visible_pagination_links = array();
for($c = $starting_nav_item_index; $c <= $ending_nav_item_index; $c++)
{
if($all_pagination_links[$c]) $visible_pagination_links[] = $all_pagination_links[$c];
}
if($selected_nav_link_index !== 0)
{
$get_params['index'] = ($selected_nav_link_index - 1) * $items_per_page;
$get_params_string = http_build_query($get_params, '', '&amp;');
$get_params_string = "?".$get_params_string;
$previous_page_link = array(
'url' => $base_url . $get_params_string,
'text' => 'Previous',
'class' => 'previous-nav-link'
);
array_unshift($visible_pagination_links, $previous_page_link);
}
if($selected_nav_link_index > $lr_nav_links_block)
{
$get_params['index'] = 0;
$get_params_string = http_build_query($get_params, '', '&amp;');
$get_params_string = "?".$get_params_string;
$first_page_link = array(
'url' => $base_url . $get_params_string,
'text' => 'First',
'class' => 'first-nav-link'
);
array_unshift($visible_pagination_links, $first_page_link);
}
if($selected_nav_link_index !== $last_nav_link_index)
{
$get_params['index'] = ($selected_nav_link_index + 1) * $items_per_page;
$get_params_string = http_build_query($get_params, '', '&amp;');
$get_params_string = "?".$get_params_string;
$next_page_link = array(
'url' => $base_url . $get_params_string,
'text' => 'Next',
'class' => 'next-nav-link'
);
array_push($visible_pagination_links, $next_page_link);
}
if($selected_nav_link_index < ( count($all_pagination_links) - $lr_nav_links_block -1) )
{
$get_params['index'] = ($last_nav_link_index) * $items_per_page;
$get_params_string = http_build_query($get_params, '', '&amp;');
$get_params_string = "?".$get_params_string;
$last_page_link = array(
'url' => $base_url . $get_params_string,
'text' => 'Last',
'class' => 'last-nav-link'
);
array_push($visible_pagination_links, $last_page_link);
}
$pagination_data = array(
'links' => $visible_pagination_links,
'info' => array(
'selected_nav_link_n' => $selected_nav_link_n,
'items_pages_n' => $items_pages_n
)
);
return $pagination_data;
} else {
return false;
}
}
function af_notice($text){
/*
global $af_notices;
$af_notices[] = "<div class='error'><p>$text</p></div>";
*/
}
function af_notices(){
/*
global $af_notices;
foreach($af_notices as $notice) :
echo $notice;
endforeach;
*/
}
function add_antiframework_admin_scripts(){
?><script type="text/javascript">/* <![CDATA[ */
<?php
$af_metaboxes = file_get_contents(AF_CORE_URL . "/js/admin-scripts.js", true);
echo $af_metaboxes;
?>
/* ]]> */</script><?php
}
/*
When a post is deleted by a user or automatically by wordpress, we also want
to delete is child 'antiframework' posts used for uploads.
The attachments of this deleted antiframework will then stay
in the media library without parents, so their behavior will match
the wordpress defaults and it will be as if those attachments
were children of the main post deleted in the first place
*/
function delete_children_antiframework_posts($post_id){
$args = array(
'post_parent' => $post_id,
'post_type' => 'antiframework',
'post_status' => 'draft',
'comment_status' => 'closed',
'ping_status' => 'closed'
);
$auto_draft_children = get_posts($args);
if(count($auto_draft_children)){
foreach($auto_draft_children as $child){
wp_delete_post($child->ID);
}
}
}
function af_log($msg){
$filename = AF_CORE_PATH . "/af_log.txt";
// open file
$fd = fopen($filename, "a");
// append date/time to message
$str = "[" . date("Y/m/d h:i:s", mktime()) . "] " . $msg;
// write string
fwrite($fd, $str . "\n");
// close file
fclose($fd);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment