From 3070abc8ddc10f40288d05e958af29718ea75c5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Igor=20Beni=C4=87?= Date: Fri, 5 Jul 2019 02:07:25 +0200 Subject: [PATCH 1/6] Refactoring Main plugin file, separating admin. Adding SS metabox to inform about Simple Sponsorships. Adding upgrade functionality to update post_type to Sponsors instead of Sponsor --- admin/class-wp-sponsors-admin.php | 104 +++++++- admin/partials/meta-boxes/sponsor-info.php | 28 +++ admin/partials/meta-boxes/ss-info.php | 12 + includes/class-wp-sponsors-activator.php | 36 --- includes/class-wp-sponsors-deactivator.php | 36 --- includes/class-wp-sponsors-i18n.php | 2 +- includes/class-wp-sponsors-installer.php | 233 ++++++++++++++++++ includes/class-wp-sponsors-shortcode.php | 142 ----------- includes/class-wp-sponsors-shortcodes.php | 152 ++++++++++++ includes/class-wp-sponsors-upgrade.php | 95 -------- includes/class-wp-sponsors-widget.php | 2 +- includes/class-wp-sponsors.php | 266 +++------------------ includes/functions-updates.php | 46 ++++ public/class-wp-sponsors-public.php | 2 +- wp-sponsors.php | 31 +-- 15 files changed, 619 insertions(+), 568 deletions(-) create mode 100644 admin/partials/meta-boxes/sponsor-info.php create mode 100644 admin/partials/meta-boxes/ss-info.php delete mode 100755 includes/class-wp-sponsors-activator.php delete mode 100755 includes/class-wp-sponsors-deactivator.php create mode 100644 includes/class-wp-sponsors-installer.php delete mode 100644 includes/class-wp-sponsors-shortcode.php create mode 100644 includes/class-wp-sponsors-shortcodes.php delete mode 100644 includes/class-wp-sponsors-upgrade.php create mode 100644 includes/functions-updates.php diff --git a/admin/class-wp-sponsors-admin.php b/admin/class-wp-sponsors-admin.php index cd68410..8a1c799 100755 --- a/admin/class-wp-sponsors-admin.php +++ b/admin/class-wp-sponsors-admin.php @@ -20,7 +20,7 @@ * @subpackage Wp_Sponsors/admin * @author Jan Henckens */ -class Wp_Sponsors_Admin { +class WP_Sponsors_Admin { /** * The ID of this plugin. @@ -104,7 +104,107 @@ public function enqueue_scripts() { } - /** + /** + * Save Metaboxes. + * + * @param $post_id + */ + public function save_meta_boxes( $post_id ) { + // verify this came from the our screen and with proper authorization, + // because save_post can be triggered at other times + + // Checks save status + $is_autosave = wp_is_post_autosave( $post_id ); + $is_revision = wp_is_post_revision( $post_id ); + $is_valid_nonce = ( isset( $_POST['wp_sponsors_nonce'] ) && wp_verify_nonce( $_POST['wp_sponsors_nonce'], basename( __FILE__ ) ) ) ? 'true' : 'false'; + // Exits script depending on save status + if ( $is_autosave || $is_revision || ! $is_valid_nonce ) { + return; + } + // Checks for input and sanitizes/saves if needed + if ( isset( $_POST['wp_sponsors_url'] ) ) { + update_post_meta( $post_id, 'wp_sponsors_url', sanitize_text_field( $_POST['wp_sponsors_url'] ) ); + } + if ( isset( $_POST['wp_sponsors_desc'] ) ) { + update_post_meta( $post_id, 'wp_sponsors_desc', $_POST['wp_sponsors_desc'] ); + } + $link_behaviour = isset($_POST['wp_sponsor_link_behaviour']) ? '1' : '0'; + update_post_meta( $post_id, 'wp_sponsor_link_behaviour', $link_behaviour ); + } + + /** + * Sponsors metaboxes + */ + public function add_meta_boxes() { + add_meta_box( 'wp-sponsor-info', __( 'Sponsor', 'wp_sponsors' ), array( $this, 'sponsors_info_metabox' ), 'sponsors', 'normal', 'high' ); + remove_meta_box( 'postimagediv', 'sponsors', 'side' ); //replace post_type from your post type name + add_meta_box( 'postimagediv', __( 'Sponsor logo', 'wp-sponsors' ), 'post_thumbnail_meta_box', 'sponsors', 'side', 'high' ); + if ( ! class_exists('\Simple_Sponsorships\Plugin' ) ) { + add_meta_box( 'ss-metabox-info', __( 'Simple Sponsorships', 'wp-sponsors' ), array( $this, 'ss_info_metabox' ), 'sponsors', 'side', 'low' ); + } + } + + public function sponsors_info_metabox( $post ) { + include_once 'partials/meta-boxes/sponsor-info.php'; + } + + public function ss_info_metabox() { + include_once 'partials/meta-boxes/ss-info.php'; + } + + + /** + * Adds a new column to the Sponsors overview list in the dashboard + * + * @param array $defaults + */ + public function add_new_sponsors_column( $defaults ) { + $defaults['wp_sponsors_logo'] = __( 'Sponsor logo', 'wp-sponsors' ); + $defaults['menu_order'] = __( 'Order', 'wp-sponsors' ); + + return $defaults; + } + + /** + * Adds the sponsors image (if available) to the Sponsors overview list in the dashboard + * + * @param string $column_name + * @param integer $post_id + */ + public function sponsors_custom_columns( $column_name, $post_id ) { + global $post; + + switch ( $column_name ) { + case 'wp_sponsors_logo': + $shame = new Wp_Sponsors_Shame(); + if ( $column_name == 'wp_sponsors_logo' ) { + $image = $shame->getImage( $post_id ); + echo $image; + } + break; + case 'menu_order': + $order = $post->menu_order; + echo $order; + break; + default: + break; + } + } + + /** + * Order Column + * + * @param $columns + * + * @return mixed + */ + public function sponsor_order_column( $columns ) { + $columns['menu_order'] = 'menu_order'; + + return $columns; + } + + /** * The function that checks for updates and runs the appropriate upgrade when needed * * @since 2.0.0 diff --git a/admin/partials/meta-boxes/sponsor-info.php b/admin/partials/meta-boxes/sponsor-info.php new file mode 100644 index 0000000..58c096d --- /dev/null +++ b/admin/partials/meta-boxes/sponsor-info.php @@ -0,0 +1,28 @@ +'; +// Get the url data if its already been entered +$meta_value = get_post_meta( get_the_ID(), 'wp_sponsors_url', true ); +// Checks and displays the retrieved value +echo '

'; +echo ''; +// Display code/markup goes here. Don't forget to include nonces! +// Noncename needed to verify where the data originated +// Get the url data if its already been entered +$meta_value = get_post_meta( get_the_ID(), 'wp_sponsors_desc', true ); +$meta_value = apply_filters( 'the_content', $meta_value ); +$meta_value = str_replace( ']]>', ']]>', $meta_value ); +// Checks and displays the retrieved value +$editor_settings = array( 'wpautop' => true, + 'media_buttons' => false, + 'textarea_rows' => '8', + 'textarea_name' => 'wp_sponsors_desc' +); +echo '

'; +echo wp_editor( $meta_value, 'wp_sponsors_desc', $editor_settings ); +$meta_value = get_post_meta( get_the_ID(), 'wp_sponsor_link_behaviour', true ); +echo '

'; +$meta_value = $meta_value == "" ? "1" : $meta_value; +echo ''; diff --git a/admin/partials/meta-boxes/ss-info.php b/admin/partials/meta-boxes/ss-info.php new file mode 100644 index 0000000..3a5b138 --- /dev/null +++ b/admin/partials/meta-boxes/ss-info.php @@ -0,0 +1,12 @@ +' . __( 'A complete solution for managing Sponsors.', 'wp-sponsors' ) . '

'; +echo '

' . __( 'With Simple Sponsorships you can:', 'wp-sponsors' ) . '

'; +echo ''; +echo '

' . __( 'Compatible with Sponsors', 'wp-sponsors' ) . '

'; +echo '' . __( 'Check the free version', 'wp-sponsors' ) . ''; \ No newline at end of file diff --git a/includes/class-wp-sponsors-activator.php b/includes/class-wp-sponsors-activator.php deleted file mode 100755 index eebed8f..0000000 --- a/includes/class-wp-sponsors-activator.php +++ /dev/null @@ -1,36 +0,0 @@ - - */ -class Wp_Sponsors_Activator { - - /** - * Short Description. (use period) - * - * Long Description. - * - * @since 1.0.0 - */ - public static function activate() { - - } - -} diff --git a/includes/class-wp-sponsors-deactivator.php b/includes/class-wp-sponsors-deactivator.php deleted file mode 100755 index afbd46f..0000000 --- a/includes/class-wp-sponsors-deactivator.php +++ /dev/null @@ -1,36 +0,0 @@ - - */ -class Wp_Sponsors_Deactivator { - - /** - * Short Description. (use period) - * - * Long Description. - * - * @since 1.0.0 - */ - public static function deactivate() { - - } - -} diff --git a/includes/class-wp-sponsors-i18n.php b/includes/class-wp-sponsors-i18n.php index 7c4adeb..b83f3ca 100755 --- a/includes/class-wp-sponsors-i18n.php +++ b/includes/class-wp-sponsors-i18n.php @@ -24,7 +24,7 @@ * @subpackage Wp_Sponsors/includes * @author Jan Henckens */ -class Wp_Sponsors_i18n { +class WP_Sponsors_i18n { /** * The domain specified for this plugin. diff --git a/includes/class-wp-sponsors-installer.php b/includes/class-wp-sponsors-installer.php new file mode 100644 index 0000000..c1176e3 --- /dev/null +++ b/includes/class-wp-sponsors-installer.php @@ -0,0 +1,233 @@ + array( + 'wp_sponsors_update_200' + ), + '3.0.0' => array( + 'wp_sponsors_update_post_type_300' + ), + ); + + + /** + * Register content + */ + public function register() { + $this->create_sponsor_taxonomies(); + $this->sponsors_register(); + } + + /** + * Creates a categories taxonomy for the sponsors post type + */ + private function create_sponsor_taxonomies() { + // Labels for the sponsor categories + $labels = array( + 'name' => _x( 'Categories', 'taxonomy general name' ), + 'singular_name' => _x( 'Category', 'taxonomy singular name' ), + 'search_items' => __( 'Search categories' ), + 'all_items' => __( 'All categories' ), + 'parent_item' => __( 'Parent category' ), + 'parent_item_colon' => __( 'Parent category:' ), + 'edit_item' => __( 'Edit category' ), + 'update_item' => __( 'Update category' ), + 'add_new_item' => __( 'Add New category' ), + 'new_item_name' => __( 'New category' ), + 'menu_name' => __( 'Categories' ), + ); + // Arguments for the sponsor categories (public = false means it don't have a url) + $args = array( + 'hierarchical' => true, + 'public' => false, + 'rewrite' => false, + 'labels' => $labels, + 'show_ui' => true, + 'show_admin_column' => true, + 'query_var' => true + ); + // Register the sponsors taxonomy + register_taxonomy( 'sponsor_categories', array( 'sponsor' ), $args ); + } + + /** + * Registers the Sponsors custom post type + */ + private function sponsors_register() { + $labels = array( + 'name' => _x( 'Sponsors', 'post type general name', 'wp-sponsors' ), + 'singular_name' => _x( 'Sponsor', 'post type singular name', 'wp-sponsors' ), + 'menu_name' => _x( 'Sponsors', 'admin menu', 'wp-sponsors' ), + 'name_admin_bar' => _x( 'Sponsor', 'add new on admin bar', 'wp-sponsors' ), + 'add_new' => _x( 'Add New', 'book', 'wp-sponsors' ), + 'add_new_item' => __( 'Add New Sponsor', 'wp-sponsors' ), + 'new_item' => __( 'New Sponsor', 'wp-sponsors' ), + 'edit_item' => __( 'Edit Sponsor', 'wp-sponsors' ), + 'view_item' => __( 'View Sponsor', 'wp-sponsors' ), + 'all_items' => __( 'All Sponsors', 'wp-sponsors' ), + 'search_items' => __( 'Search Sponsors', 'wp-sponsors' ), + 'parent_item_colon' => __( 'Parent Sponsors:', 'wp-sponsors' ), + 'not_found' => __( 'No sponsor found.', 'wp-sponsors' ), + 'not_found_in_trash' => __( 'No sponsor found in Trash.', 'wp-sponsors' ), + 'featured_image' => __( 'Sponsor Logo', 'wp-sponsors' ), + 'set_featured_image' => __( 'Set Sponsor Logo', 'wp-sponsors' ), + 'remove_featured_image' => __( 'Remove Sponsor Logo', 'wp-sponsors' ), + 'use_featured_image' => __( 'Use Sponsor Logo', 'wp-sponsors' ), + ); + + $args = array( + //'public' => true, + 'labels' => $labels, + 'public' => false, + 'exclude_from_search' => true, + 'publicly_queryable' => false, + 'show_ui' => true, + 'show_in_menu' => true, + 'show_in_admin_bar' => false, + 'menu_position' => 5, + 'menu_icon' => 'dashicons-format-image', + //'query_var' => true, + 'rewrite' => false, + 'capability_type' => 'post', + 'has_archive' => false, + 'hierarchical' => false, + 'can_export' => true, + 'query_var' => false, + 'supports' => array( 'title', 'page-attributes' ), + 'taxonomies' => array( 'sponsor_categories' ), + ); + + register_post_type( 'sponsors', $args ); + add_post_type_support( 'sponsors', 'thumbnail' ); + + } + + /** + * Check Version + */ + public function check_version() { + if ( ! defined( 'IFRAME_REQUEST' ) && version_compare( get_option( 'sponsors_db_version' ),WP_SPONSORS_VERSION, '<' ) ) { + self::install(); + do_action( 'wp_sponsors_updated' ); + } + } + + /** + * Install WC. + */ + public static function install() { + if ( ! is_blog_installed() ) { + return; + } + + // Check if we are not already running this routine. + if ( 'yes' === get_transient( 'wp_sponsors_installing' ) ) { + return; + } + + // If we made it till here nothing is running yet, lets set the transient now. + set_transient( 'wp_sponsors_installing', 'yes', MINUTE_IN_SECONDS * 10 ); + + self::maybe_update_db_version(); + + delete_transient( 'wp_sponsors_installing' ); + + do_action( 'wp_sponsors_installed' ); + } + + /** + * Run an update callback when triggered by ActionScheduler. + * + * @since 3.6.0 + * @param string $callback Callback name. + */ + public static function run_update_callback( $callback ) { + include_once dirname( __FILE__ ) . '/functions-updates.php'; + + if ( is_callable( $callback ) ) { + $result = (bool) call_user_func( $callback ); + } + } + + /** + * Is a DB update needed? + * + * @since 3.0.0 + * @return boolean + */ + public static function needs_db_update() { + $current_db_version = get_option( 'sponsors_db_version', null ); + $updates = self::get_db_update_callbacks(); + + return ! is_null( $current_db_version ) && version_compare( $current_db_version, max( array_keys( $updates ) ), '<' ); + } + + /** + * See if we need to show or run database updates during install. + * + * @since 3.2.0 + */ + private static function maybe_update_db_version() { + if ( self::needs_db_update() ) { + self::update(); + } else { + self::update_db_version(); + } + } + + /** + * Get list of DB update callbacks. + * + * @since 3.0.0 + * @return array + */ + public static function get_db_update_callbacks() { + return self::$db_updates; + } + + /** + * Push all needed DB updates to the queue for processing. + */ + private static function update() { + $current_db_version = get_option( 'sponsors_db_version' ); + + foreach ( self::get_db_update_callbacks() as $version => $update_callbacks ) { + if ( version_compare( $current_db_version, $version, '<' ) ) { + foreach ( $update_callbacks as $update_callback ) { + self::run_update_callback( $update_callback ); + } + } + } + + self::update_db_version(); + } + + /** + * Update DB version to current. + * + * @param string|null $version New WooCommerce DB version or null. + */ + public static function update_db_version( $version = null ) { + update_option( 'sponsors_db_version', is_null( $version ) ? WP_SPONSORS_VERSION : $version ); + } + +} \ No newline at end of file diff --git a/includes/class-wp-sponsors-shortcode.php b/includes/class-wp-sponsors-shortcode.php deleted file mode 100644 index 6954734..0000000 --- a/includes/class-wp-sponsors-shortcode.php +++ /dev/null @@ -1,142 +0,0 @@ - 'post', - 'image' => 'yes', - 'images' => 'yes', - 'category' => '', - 'size' => 'default', - 'style' => 'list', - 'description' => 'no', - 'orderby' => 'menu_order', - 'title' => 'no', - 'max' => '-1', - 'debug' => NULL - ), $atts ) ); - - $args = array ( - 'post_type' => 'sponsor', - 'post_status' => 'publish', - 'pagination' => false, - 'order' => 'ASC', - 'orderby' => isset($atts['orderby']) ? $atts['orderby'] : 'menu_order', - 'posts_per_page' => isset($atts['max']) ? $atts['max'] : '-1', - 'tax_query' => array(), - ); - - if(!$atts) { $atts = array(); }; - - $nofollow = ( defined( 'SPONSORS_NO_FOLLOW' ) ) ? SPONSORS_NO_FOLLOW : true; - - if(!empty($category)) { - $args['tax_query'] = array( - array( - 'taxonomy' => 'sponsor_categories', - 'field' => 'slug', - 'terms' => $category, - ), - ); - } - - // $sizes = array('small' => '15%', 'medium' => '30%', 'large' => '50%', 'full' => '100%', 'default' => '30%'); - ob_start(); - - // Set default options with then shortcode is used without parameters - // style options defaults to list - if ( empty($atts['style']) ) { $atts['style'] = 'list'; } - // images options default to yes - - $images != 'no' && $image != 'no' ? $images = true : $images = false; - // debug option defaults to false - isset($debug) ? $debug = true : $debug = false; - $description === 'yes' ? $description = true : $description = false; - $title === 'yes' ? $title = true : $title = false; - - $query = new WP_Query($args); - - // Set up the shortcode styles - $style = array(); - $layout = $atts['style']; - - $shame = new Wp_Sponsors_Shame(); - - switch ($layout) { - case "list": - $style['containerPre'] = '
    '; - $style['containerPost'] = '
'; - $style['wrapperClass'] = 'sponsor-item'; - $style['wrapperPre'] = 'li'; - $style['wrapperPost'] = ''; - break; - case "linear": - case "grid": - $style['containerPre'] = '
'; - $style['containerPost'] = '
'; - $style['wrapperClass'] = 'sponsor-item'; - $style['wrapperPre'] = 'div'; - $style['wrapperPost'] = ''; - $style['imageSize'] = 'full'; - break; - } - - if ( $query->have_posts() ) { - while ( $query->have_posts() ) : $query->the_post(); - - if($query->current_post === 0) { echo $style['containerPre']; } - // Check if the sponsor was a link - get_post_meta( get_the_ID(), 'wp_sponsors_url', true ) != '' ? $link = get_post_meta( get_the_ID(), 'wp_sponsors_url', true ) : $link = false; - $link_target = get_post_meta( get_the_ID(), 'wp_sponsor_link_behaviour', true ); - $target = ($link_target == 1) ? 'target="_blank"' : ''; - $class = ''; - $class .= $size; - if($debug) { $class .= ' debug'; } - - echo '<' . $style['wrapperPre'] . ' class="' . $style['wrapperClass'] .' ' . $class . '">'; - $sponsor = ''; - // Check if we have a link - if($link && !$images) { - $sponsor .= ''; - } - // Check if we have a title - if($title) { - $sponsor .= '

'.get_the_title().'

'; - } - // Close the link tag if we have it - if($link && !$images) { - $sponsor .= '
'; - } - // Check if we have a link - if($link && $images) { - $sponsor .= ''; - } - // Check if we should do images, just show the title if there's no image set - if($images){ - $sponsor .= $shame->getImage(get_the_ID()); - } elseif ($title === false) { - $sponsor .= '

' . get_the_title() . '

'; - } - - // Check if we need a description and the description is not empty - if($description) { - $sponsor .= '

' . get_post_meta( get_the_ID(), 'wp_sponsors_desc', true ) . '

'; - } - // Close the link tag if we have it - if($link && $images) { - $sponsor .= '
'; - } - echo $sponsor; - echo $style['wrapperPost']; - if( ($query->current_post + 1) === $query->post_count) { echo $style['containerPost']; } - endwhile; - wp_reset_postdata(); - return ob_get_clean(); - } - } - add_shortcode( 'sponsors', 'sponsors_register_shortcode' ); diff --git a/includes/class-wp-sponsors-shortcodes.php b/includes/class-wp-sponsors-shortcodes.php new file mode 100644 index 0000000..97c3355 --- /dev/null +++ b/includes/class-wp-sponsors-shortcodes.php @@ -0,0 +1,152 @@ + 'post', + 'image' => 'yes', + 'images' => 'yes', + 'category' => '', + 'size' => 'default', + 'style' => 'list', + 'description' => 'no', + 'orderby' => 'menu_order', + 'title' => 'no', + 'max' => '-1', + 'debug' => NULL + ), $atts ) ); + + $args = array ( + 'post_type' => array( 'sponsors', 'sponsor' ), // Allowing 'sponsor' in case the update does not work. + 'post_status' => 'publish', + 'pagination' => false, + 'order' => 'ASC', + 'orderby' => isset($atts['orderby']) ? $atts['orderby'] : 'menu_order', + 'posts_per_page' => isset($atts['max']) ? $atts['max'] : '-1', + 'tax_query' => array(), + ); + + if(!$atts) { $atts = array(); }; + + $nofollow = ( defined( 'SPONSORS_NO_FOLLOW' ) ) ? SPONSORS_NO_FOLLOW : true; + + if(!empty($category)) { + $args['tax_query'] = array( + array( + 'taxonomy' => 'sponsor_categories', + 'field' => 'slug', + 'terms' => $category, + ), + ); + } + + // $sizes = array('small' => '15%', 'medium' => '30%', 'large' => '50%', 'full' => '100%', 'default' => '30%'); + ob_start(); + + // Set default options with then shortcode is used without parameters + // style options defaults to list + if ( empty($atts['style']) ) { $atts['style'] = 'list'; } + // images options default to yes + + $images != 'no' && $image != 'no' ? $images = true : $images = false; + // debug option defaults to false + isset($debug) ? $debug = true : $debug = false; + $description === 'yes' ? $description = true : $description = false; + $title === 'yes' ? $title = true : $title = false; + + $query = new WP_Query($args); + + // Set up the shortcode styles + $style = array(); + $layout = $atts['style']; + + $shame = new Wp_Sponsors_Shame(); + + switch ($layout) { + case "list": + $style['containerPre'] = '
    '; + $style['containerPost'] = '
'; + $style['wrapperClass'] = 'sponsor-item'; + $style['wrapperPre'] = 'li'; + $style['wrapperPost'] = ''; + break; + case "linear": + case "grid": + $style['containerPre'] = '
'; + $style['containerPost'] = '
'; + $style['wrapperClass'] = 'sponsor-item'; + $style['wrapperPre'] = 'div'; + $style['wrapperPost'] = ''; + $style['imageSize'] = 'full'; + break; + } + + if ( $query->have_posts() ) { + while ( $query->have_posts() ) : $query->the_post(); + + if($query->current_post === 0) { echo $style['containerPre']; } + // Check if the sponsor was a link + get_post_meta( get_the_ID(), 'wp_sponsors_url', true ) != '' ? $link = get_post_meta( get_the_ID(), 'wp_sponsors_url', true ) : $link = false; + $link_target = get_post_meta( get_the_ID(), 'wp_sponsor_link_behaviour', true ); + $target = ($link_target == 1) ? 'target="_blank"' : ''; + $class = ''; + $class .= $size; + if($debug) { $class .= ' debug'; } + + echo '<' . $style['wrapperPre'] . ' class="' . $style['wrapperClass'] .' ' . $class . '">'; + $sponsor = ''; + // Check if we have a link + if($link && !$images) { + $sponsor .= ''; + } + // Check if we have a title + if($title) { + $sponsor .= '

'.get_the_title().'

'; + } + // Close the link tag if we have it + if($link && !$images) { + $sponsor .= '
'; + } + // Check if we have a link + if($link && $images) { + $sponsor .= ''; + } + // Check if we should do images, just show the title if there's no image set + if($images){ + $sponsor .= $shame->getImage(get_the_ID()); + } elseif ($title === false) { + $sponsor .= '

' . get_the_title() . '

'; + } + + // Check if we need a description and the description is not empty + if($description) { + $sponsor .= '

' . get_post_meta( get_the_ID(), 'wp_sponsors_desc', true ) . '

'; + } + // Close the link tag if we have it + if($link && $images) { + $sponsor .= '
'; + } + echo $sponsor; + echo $style['wrapperPost']; + if( ($query->current_post + 1) === $query->post_count) { echo $style['containerPost']; } + endwhile; + wp_reset_postdata(); + return ob_get_clean(); + } + } +} diff --git a/includes/class-wp-sponsors-upgrade.php b/includes/class-wp-sponsors-upgrade.php deleted file mode 100644 index 61a4b48..0000000 --- a/includes/class-wp-sponsors-upgrade.php +++ /dev/null @@ -1,95 +0,0 @@ - - */ -class Wp_Sponsors_upgrade { - - - /** - * The ID of this plugin. - * - * @since 1.0.0 - * @access private - * @var string $wp_sponsors The ID of this plugin. - */ - private $wp_sponsors; - - /** - * The version of this plugin. - * - * @since 1.0.0 - * @access private - * @var string $version The current version of this plugin. - */ - private $version; - - /** - * Initialize the class and set its properties. - * - * @since 1.0.0 - * @var string $wp_sponsors The name of this plugin. - * @var string $version The version of this plugin. - */ - public function __construct( $version ) { - $this->version = $version; - - } - - public function run( $update ) { - if(is_callable(array($this, $update))) { - $this->$update(); - } - return; - } - - public function upgrade200() { - require_once ABSPATH . 'wp-includes/pluggable.php'; - global $wpdb; - $results = $wpdb->get_results( 'SELECT * FROM ' . $wpdb->prefix . 'postmeta WHERE meta_key like "'. $wpdb->prefix .'_sponsors_img"', OBJECT ); - - foreach ($results as $key => $sponsor) { - $data[$sponsor->post_id]['sponsor'] = $sponsor->post_id; - $image = preg_split('/uploads\//', $sponsor->meta_value, PREG_SPLIT_OFFSET_CAPTURE); - - $query = "SELECT post_id FROM " . $wpdb->prefix . "postmeta WHERE meta_key = '_wp_attached_file' AND meta_value = '". $image[1] . "'"; - $imageId = $wpdb->get_row($query, OBJECT); - $data[$sponsor->post_id]['current_image'] = $image[1]; - $data[$sponsor->post_id]['featured_image'] = $imageId->post_id; - } - if(isset($data)) { - foreach($data as $key => $entry) { - $wpdb->insert($wpdb->prefix . '_postmeta', - array( - 'post_id' => $key, - 'meta_key' => '_thumbnail_id', - 'meta_value' => $entry['featured_image'] - ), - array( '%d', '%s', '%s' ) - ); - } - } - $wpdb->insert($wpdb->prefix . 'options', array( 'option_name' => 'sponsors_db_version', 'option_value' => 2), array( '%s', '%d' )); - return; - } - -} diff --git a/includes/class-wp-sponsors-widget.php b/includes/class-wp-sponsors-widget.php index ba7a9b1..6f8b224 100644 --- a/includes/class-wp-sponsors-widget.php +++ b/includes/class-wp-sponsors-widget.php @@ -17,7 +17,7 @@ function widget( $args, $instance ) { } $args = array( - 'post_type' => 'sponsor', + 'post_type' => array( 'sponsors', 'sponsor' ), // Allowing 'sponsor' in case the update does not work. 'post_status' => 'publish', 'pagination' => false, 'order' => 'ASC', diff --git a/includes/class-wp-sponsors.php b/includes/class-wp-sponsors.php index 585835c..3f5e976 100755 --- a/includes/class-wp-sponsors.php +++ b/includes/class-wp-sponsors.php @@ -27,7 +27,7 @@ * @subpackage Wp_Sponsors/includes * @author Jan Henckens */ -class Wp_Sponsors { +class WP_Sponsors { /** * The loader that's responsible for maintaining and registering all hooks that power @@ -69,15 +69,23 @@ class Wp_Sponsors { public function __construct() { $this->wp_sponsors = 'wp-sponsors'; - $this->version = '2.0.0'; + $this->version = '3.0.0'; + $this->define_constants(); $this->load_dependencies(); $this->set_locale(); + $this->define_general_hooks(); $this->define_admin_hooks(); $this->define_public_hooks(); } + private function define_constants() { + if ( ! defined( 'WP_SPONSORS_VERSION' ) ) { + define( 'WP_SPONSORS_VERSION', $this->version ); + } + } + /** * Load the required dependencies for this plugin. * @@ -108,13 +116,13 @@ private function load_dependencies() { */ require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-wp-sponsors-i18n.php'; require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-wp-sponsors-widget.php'; - require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-wp-sponsors-shortcode.php'; + require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-wp-sponsors-shortcodes.php'; + require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-wp-sponsors-installer.php'; /** * The class responsible for defining all actions that occur in the Dashboard. */ require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-wp-sponsors-admin.php'; - require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-wp-sponsors-upgrade.php'; require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-wp-sponsors-extras.php'; require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-wp-sponsors-shame.php'; @@ -143,7 +151,7 @@ private function load_dependencies() { */ private function set_locale() { - $plugin_i18n = new Wp_Sponsors_i18n(); + $plugin_i18n = new WP_Sponsors_i18n(); $plugin_i18n->set_domain( $this->get_wp_sponsors() ); $this->loader->add_action( 'plugins_loaded', $plugin_i18n, 'load_plugin_textdomain' ); @@ -158,12 +166,18 @@ private function set_locale() { * @access private */ private function define_admin_hooks() { + if ( ! is_admin() ) { return; } + $plugin_admin = new WP_Sponsors_Admin( $this->get_wp_sponsors(), $this->get_version() ); - $plugin_admin = new Wp_Sponsors_Admin( $this->get_wp_sponsors(), $this->get_version() ); - + $this->loader->add_action( 'add_meta_boxes', $plugin_admin,'add_meta_boxes' ); + $this->loader->add_action( 'save_post', $plugin_admin,'save_meta_boxes' ); $this->loader->add_action( 'plugins_loaded', $plugin_admin, 'update' ); $this->loader->add_action( 'admin_enqueue_scripts', $plugin_admin, 'enqueue_styles' ); $this->loader->add_action( 'admin_enqueue_scripts', $plugin_admin, 'enqueue_scripts' ); + $this->loader->add_action( 'manage_sponsors_posts_custom_column', $plugin_admin, 'sponsors_custom_columns', 10, 2 ); + + $this->loader->add_filter( 'manage_edit-sponsors_sortable_columns', $plugin_admin,'sponsor_order_column' ); + $this->loader->add_filter( 'manage_sponsors_posts_columns', $plugin_admin, 'add_new_sponsors_column' ); } @@ -176,7 +190,7 @@ private function define_admin_hooks() { */ private function define_public_hooks() { - $plugin_public = new Wp_Sponsors_Public( $this->get_wp_sponsors(), $this->get_version() ); + $plugin_public = new WP_Sponsors_Public( $this->get_wp_sponsors(), $this->get_version() ); $this->loader->add_action( 'wp_enqueue_scripts', $plugin_public, 'enqueue_styles' ); $this->loader->add_action( 'wp_enqueue_scripts', $plugin_public, 'enqueue_scripts' ); @@ -184,6 +198,20 @@ private function define_public_hooks() { } + /** + * Define General Hooks used for anything else. + * Some both on public and admin. + */ + private function define_general_hooks() { + register_activation_hook( WP_SPONSORS_FILE, array( 'WP_Sponsors_Installer', 'install' ) ); + + $installer = new WP_Sponsors_Installer(); + $shortcodes = new WP_Sponsors_Shortcodes(); + $this->loader->add_action( 'init', $installer, 'register', 0 ); + $this->loader->add_action( 'init', $installer, 'check_version' ); + $this->loader->add_action( 'init', $shortcodes, 'register_shortcodes', 0 ); + } + /** * Run the loader to execute all of the hooks with WordPress. * @@ -191,228 +219,6 @@ private function define_public_hooks() { */ public function run() { $this->loader->run(); - - /** - * Trigger create_sponsor_taxonomies on init - */ - add_action( 'init', 'create_sponsor_taxonomies', 0 ); - - /** - * Creates a categories taxonomy for the sponsors post type - */ - function create_sponsor_taxonomies() { - // Labels for the sponsor categories - $labels = array( - 'name' => _x( 'Categories', 'taxonomy general name' ), - 'singular_name' => _x( 'Category', 'taxonomy singular name' ), - 'search_items' => __( 'Search categories' ), - 'all_items' => __( 'All categories' ), - 'parent_item' => __( 'Parent category' ), - 'parent_item_colon' => __( 'Parent category:' ), - 'edit_item' => __( 'Edit category' ), - 'update_item' => __( 'Update category' ), - 'add_new_item' => __( 'Add New category' ), - 'new_item_name' => __( 'New category' ), - 'menu_name' => __( 'Categories' ), - ); - // Arguments for the sponsor categories (public = false means it don't have a url) - $args = array( - 'hierarchical' => true, - 'public' => false, - 'rewrite' => false, - 'labels' => $labels, - 'show_ui' => true, - 'show_admin_column' => true, - 'query_var' => true - ); - // Register the sponsors taxonomy - register_taxonomy( 'sponsor_categories', array( 'sponsor' ), $args ); - } - - /** - * Registers the Sponsors custom post type - */ - function sponsors_register() { - $args = array( - 'public' => true, - 'label' => 'Sponsors', - 'public' => false, - 'exclude_from_search' => true, - 'publicly_queryable' => false, - 'show_ui' => true, - 'show_in_menu' => true, - 'show_in_admin_bar' => false, - 'menu_position' => 5, - 'menu_icon' => 'dashicons-format-image', - 'query_var' => true, - 'rewrite' => false, - 'capability_type' => 'post', - 'has_archive' => false, - 'hierarchical' => false, - 'can_export' => true, - 'query_var' => false, - 'capability_type' => 'post', - 'supports' => array( 'title', 'page-attributes' ), - 'taxonomies' => array( 'sponsor_categories' ), - 'register_meta_box_cb' => 'add_sponsor_metabox' - ); - register_post_type( 'sponsor', $args ); - } - - add_post_type_support( 'sponsor', 'thumbnail' ); - add_action( 'init', 'sponsors_register' ); - - /** - * Register meta box(es). - */ - function add_sponsor_metabox() { - add_meta_box( 'meta-box-id', __( 'Sponsor', 'wp_sponsors' ), 'sponsor_metabox_url', 'sponsor', 'normal', 'high' ); - } - - add_action( 'add_meta_boxes', 'add_sponsor_metabox' ); - - - /** - * Meta box display callback. - * - * @param WP_Post $post Current post object. - */ - function sponsor_metabox_url( $post ) { - // Display code/markup goes here. Don't forget to include nonces! - // Noncename needed to verify where the data originated - echo ''; - // Get the url data if its already been entered - $meta_value = get_post_meta( get_the_ID(), 'wp_sponsors_url', true ); - // Checks and displays the retrieved value - echo '

'; - echo ''; - // Display code/markup goes here. Don't forget to include nonces! - // Noncename needed to verify where the data originated - // Get the url data if its already been entered - $meta_value = get_post_meta( get_the_ID(), 'wp_sponsors_desc', true ); - $meta_value = apply_filters( 'the_content', $meta_value ); - $meta_value = str_replace( ']]>', ']]>', $meta_value ); - // Checks and displays the retrieved value - $editor_settings = array( 'wpautop' => true, - 'media_buttons' => false, - 'textarea_rows' => '8', - 'textarea_name' => 'wp_sponsors_desc' - ); - echo '

'; - echo wp_editor( $meta_value, 'wp_sponsors_desc', $editor_settings ); - $meta_value = get_post_meta( get_the_ID(), 'wp_sponsor_link_behaviour', true ); - echo '

'; - $meta_value = $meta_value == "" ? "1" : $meta_value; - echo ''; - } - - - /** - * Save meta box content. - * - * @param int $post_id Post ID - */ - function sponsors_save_metabox( $post_id ) { - // verify this came from the our screen and with proper authorization, - // because save_post can be triggered at other times - - // Checks save status - $is_autosave = wp_is_post_autosave( $post_id ); - $is_revision = wp_is_post_revision( $post_id ); - $is_valid_nonce = ( isset( $_POST['wp_sponsors_nonce'] ) && wp_verify_nonce( $_POST['wp_sponsors_nonce'], basename( __FILE__ ) ) ) ? 'true' : 'false'; - // Exits script depending on save status - if ( $is_autosave || $is_revision || ! $is_valid_nonce ) { - return; - } - // Checks for input and sanitizes/saves if needed - if ( isset( $_POST['wp_sponsors_url'] ) ) { - update_post_meta( $post_id, 'wp_sponsors_url', sanitize_text_field( $_POST['wp_sponsors_url'] ) ); - } - if ( isset( $_POST['wp_sponsors_desc'] ) ) { - update_post_meta( $post_id, 'wp_sponsors_desc', $_POST['wp_sponsors_desc'] ); - } - $link_behaviour = isset($_POST['wp_sponsor_link_behaviour']) ? '1' : '0'; - update_post_meta( $post_id, 'wp_sponsor_link_behaviour', $link_behaviour ); - } - - add_action( 'save_post', 'sponsors_save_metabox' ); - - /** - * Adds a new column to the Sponsors overview list in the dashboard - */ - function sponsors_add_new_column( $defaults ) { - $defaults['wp_sponsors_logo'] = __( 'Sponsor logo', 'wp-sponsors' ); - $defaults['menu_order'] = __( 'Order', 'wp-sponsors' ); - - return $defaults; - } - - add_filter( 'manage_sponsor_posts_columns', 'sponsors_add_new_column' ); - - /** - * Adds the sponsors image (if available) to the Sponsors overview list in the dashboard - */ - function sponsors_column_add_image( $column_name, $post_ID ) { - $shame = new Wp_Sponsors_Shame(); - if ( $column_name == 'wp_sponsors_logo' ) { - $image = $shame->getImage( $post_ID ); - echo $image; - } - } - - add_action( 'manage_sponsor_posts_custom_column', 'sponsors_column_add_image', 10, 2 ); - - /** - * show custom order column values - */ - function sponsors_column_add_order( $name ) { - global $post; - - switch ( $name ) { - case 'menu_order': - $order = $post->menu_order; - echo $order; - break; - default: - break; - } - } - - add_action( 'manage_sponsor_posts_custom_column', 'sponsors_column_add_order' ); - - - function sponsor_order_column( $columns ) { - $columns['menu_order'] = 'menu_order'; - - return $columns; - } - - add_filter( 'manage_edit-sponsor_sortable_columns', 'sponsor_order_column' ); - - function set_featured_image_filter() { - $screen = get_current_screen(); - if ( isset( $screen->post_type ) && $screen->post_type == 'sponsor' ) { - add_filter( 'admin_post_thumbnail_html', 'change_featured_image_strings', 10, 1 ); - } - } - - function change_featured_image_strings( $content ) { - $content = str_replace( __( 'Featured Image' ), __( 'Set sponsor logo', 'wp-sponsors' ), $content ); - $content = str_replace( __( 'Set featured image' ), __( 'Set sponsor logo', 'wp-sponsors' ), $content ); - $content = str_replace( __( 'Remove featured image' ), __( 'Remove sponsor logo', 'wp-sponsors' ), $content ); - - return $content; - } - - add_action( 'current_screen', 'set_featured_image_filter' ); - - function change_meta_box_title() { - remove_meta_box( 'postimagediv', 'sponsor', 'side' ); //replace post_type from your post type name - add_meta_box( 'postimagediv', __( 'Sponsor logo', 'wp-sponsors' ), 'post_thumbnail_meta_box', 'sponsor', 'side', 'high' ); - } - - add_action( 'admin_head', 'change_meta_box_title' ); - } /** diff --git a/includes/functions-updates.php b/includes/functions-updates.php new file mode 100644 index 0000000..500f1c0 --- /dev/null +++ b/includes/functions-updates.php @@ -0,0 +1,46 @@ +get_results( 'SELECT * FROM ' . $wpdb->prefix . 'postmeta WHERE meta_key like "'. $wpdb->prefix .'_sponsors_img"', OBJECT ); + + foreach ($results as $key => $sponsor) { + $data[$sponsor->post_id]['sponsor'] = $sponsor->post_id; + $image = preg_split('/uploads\//', $sponsor->meta_value, PREG_SPLIT_OFFSET_CAPTURE); + + $query = "SELECT post_id FROM " . $wpdb->prefix . "postmeta WHERE meta_key = '_wp_attached_file' AND meta_value = '". $image[1] . "'"; + $imageId = $wpdb->get_row($query, OBJECT); + $data[$sponsor->post_id]['current_image'] = $image[1]; + $data[$sponsor->post_id]['featured_image'] = $imageId->post_id; + } + if(isset($data)) { + foreach($data as $key => $entry) { + $wpdb->insert($wpdb->prefix . '_postmeta', + array( + 'post_id' => $key, + 'meta_key' => '_thumbnail_id', + 'meta_value' => $entry['featured_image'] + ), + array( '%d', '%s', '%s' ) + ); + } + } + $wpdb->insert($wpdb->prefix . 'options', array( 'option_name' => 'sponsors_db_version', 'option_value' => 2), array( '%s', '%d' )); + return; +} + +/** + * Update post type to 'sponsors' to be compatible with Simple Sponsorships. + */ +function wp_sponsors_update_post_type_300() { + global $wpdb; + + $wpdb->update( $wpdb->posts, array( 'post_type' => 'sponsors' ), array( 'post_type' => 'sponsor' ) ); +} \ No newline at end of file diff --git a/public/class-wp-sponsors-public.php b/public/class-wp-sponsors-public.php index 8beaa97..dd3faa3 100755 --- a/public/class-wp-sponsors-public.php +++ b/public/class-wp-sponsors-public.php @@ -20,7 +20,7 @@ * @subpackage Wp_Sponsors/public * @author Jan Henckens */ -class Wp_Sponsors_Public { +class WP_Sponsors_Public { /** * The ID of this plugin. diff --git a/wp-sponsors.php b/wp-sponsors.php index d31c8bc..4cc59f3 100755 --- a/wp-sponsors.php +++ b/wp-sponsors.php @@ -14,11 +14,11 @@ * * @wordpress-plugin * Plugin Name: Sponsors - * Plugin URI: https://www.ibenic.com + * Plugin URI: http://www.wpsimplesponsorships.com * Description: Add links and logo's for your sponsors/partners/etc to your sidebars and posts with our widget and shortcode. - * Version: 2.5.1 - * Author: Igor Benić - * Author URI: https://www.ibenic.com + * Version: 3.0.0 + * Author: Simple Sponsorships + * Author URI: http://www.wpsimplesponsorships.com * License: GPL-2.0+ * License URI: http://www.gnu.org/licenses/gpl-2.0.txt * Text Domain: wp-sponsors @@ -30,27 +30,10 @@ die; } -/** - * The code that runs during plugin activation. - * This action is documented in includes/class-wp-sponsors-activator.php - */ -function activate_wp_sponsors() { - require_once plugin_dir_path( __FILE__ ) . 'includes/class-wp-sponsors-activator.php'; - Wp_Sponsors_Activator::activate(); +if ( ! defined( 'WP_SPONSORS_FILE' ) ) { + define( 'WP_SPONSORS_FILE', __FILE__ ); } -/** - * The code that runs during plugin deactivation. - * This action is documented in includes/class-wp-sponsors-deactivator.php - */ -function deactivate_wp_sponsors() { - require_once plugin_dir_path( __FILE__ ) . 'includes/class-wp-sponsors-deactivator.php'; - Wp_Sponsors_Deactivator::deactivate(); -} - -register_activation_hook( __FILE__, 'activate_wp_sponsors' ); -register_deactivation_hook( __FILE__, 'deactivate_wp_sponsors' ); - /** * The core plugin class that is used to define internationalization, * dashboard-specific hooks, and public-facing site hooks. @@ -68,7 +51,7 @@ function deactivate_wp_sponsors() { */ function run_wp_sponsors() { - $plugin = new Wp_Sponsors(); + $plugin = new WP_Sponsors(); $plugin->run(); } From 1fc79e9dbd836e95f01ee79f18a019a99352062c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Igor=20Beni=C4=87?= Date: Fri, 5 Jul 2019 17:55:39 +0200 Subject: [PATCH 2/6] Fixing horizontal display. Adding image sizes to widget as well --- admin/class-wp-sponsors-admin.php | 6 +- includes/class-wp-sponsors-extras.php | 38 +++++- includes/class-wp-sponsors-shame.php | 24 ---- includes/class-wp-sponsors-shortcodes.php | 134 ++++++++++--------- includes/class-wp-sponsors-widget.php | 150 +++++++++++++++------- includes/class-wp-sponsors.php | 3 +- public/css/wp-sponsors-public.css | 17 ++- 7 files changed, 230 insertions(+), 142 deletions(-) delete mode 100644 includes/class-wp-sponsors-shame.php diff --git a/admin/class-wp-sponsors-admin.php b/admin/class-wp-sponsors-admin.php index 8a1c799..f44f20c 100755 --- a/admin/class-wp-sponsors-admin.php +++ b/admin/class-wp-sponsors-admin.php @@ -176,11 +176,7 @@ public function sponsors_custom_columns( $column_name, $post_id ) { switch ( $column_name ) { case 'wp_sponsors_logo': - $shame = new Wp_Sponsors_Shame(); - if ( $column_name == 'wp_sponsors_logo' ) { - $image = $shame->getImage( $post_id ); - echo $image; - } + echo get_the_post_thumbnail( $post_id, array( 0, 50 ) ); break; case 'menu_order': $order = $post->menu_order; diff --git a/includes/class-wp-sponsors-extras.php b/includes/class-wp-sponsors-extras.php index 033c678..3f25784 100644 --- a/includes/class-wp-sponsors-extras.php +++ b/includes/class-wp-sponsors-extras.php @@ -1,9 +1,38 @@ $_wp_additional_image_sizes[ $_size ]['width'], + 'height' => $_wp_additional_image_sizes[ $_size ]['height'], + 'crop' => $_wp_additional_image_sizes[ $_size ]['crop'], + ); + } + } + + return $sizes; + } public function setup() { - add_filter( 'plugin_row_meta', array($this, 'custom_plugin_row_meta'), 10, 2 ); + add_filter( 'plugin_row_meta', array( $this, 'add_row_meta' ), 10, 2 ); } /** @@ -13,7 +42,10 @@ public function setup() { * * @return array */ - function custom_plugin_row_meta( $links, $file ) { + function add_row_meta( $links, $file ) { + if ( 'wp-sponsors/wp-sponsors.php' === $file ) { + $links['simple-sponsorships'] = '' . __( 'Try Simple Sponsorships', 'wp-sponsors' ) . ''; + } return $links; } } \ No newline at end of file diff --git a/includes/class-wp-sponsors-shame.php b/includes/class-wp-sponsors-shame.php deleted file mode 100644 index dd13b15..0000000 --- a/includes/class-wp-sponsors-shame.php +++ /dev/null @@ -1,24 +0,0 @@ - - */ -class Wp_Sponsors_Shame { - - public function getImage($post_ID) { - $post_thumbnail = get_the_post_thumbnail( $post_ID, 'medium' ); - $post_custom_image = get_post_meta( $post_ID, 'wp_sponsors_img', true ); - - if ($post_thumbnail && !empty($post_thumbnail)) { - return $post_thumbnail; - } elseif (isset($post_custom_image)) { - return ''; - } - } - -} - \ No newline at end of file diff --git a/includes/class-wp-sponsors-shortcodes.php b/includes/class-wp-sponsors-shortcodes.php index 97c3355..09a3ef5 100644 --- a/includes/class-wp-sponsors-shortcodes.php +++ b/includes/class-wp-sponsors-shortcodes.php @@ -10,74 +10,67 @@ public function register_shortcodes() { } /** + * Shortcode for showing sponsors + * * @param $atts * * @return string */ - public static function sponsors_shortcode( $atts ) { + public static function sponsors_shortcode( $atts = array() ) { - // define attributes and their defaults - extract( shortcode_atts( array ( + $atts = shortcode_atts( array ( 'type' => 'post', 'image' => 'yes', 'images' => 'yes', 'category' => '', 'size' => 'default', + 'image_size' => 'medium', 'style' => 'list', 'description' => 'no', 'orderby' => 'menu_order', + 'order' => 'ASC', 'title' => 'no', 'max' => '-1', 'debug' => NULL - ), $atts ) ); + ), $atts, 'wp_sponsors' ); $args = array ( 'post_type' => array( 'sponsors', 'sponsor' ), // Allowing 'sponsor' in case the update does not work. - 'post_status' => 'publish', - 'pagination' => false, - 'order' => 'ASC', - 'orderby' => isset($atts['orderby']) ? $atts['orderby'] : 'menu_order', - 'posts_per_page' => isset($atts['max']) ? $atts['max'] : '-1', - 'tax_query' => array(), + 'post_status' => 'publish', + 'pagination' => false, + 'order' => $atts['order'], + 'orderby' => isset( $atts['orderby'] ) ? $atts['orderby'] : 'menu_order', + 'posts_per_page' => isset( $atts['max'] ) ? $atts['max'] : '-1', + 'tax_query' => array(), ); - if(!$atts) { $atts = array(); }; - $nofollow = ( defined( 'SPONSORS_NO_FOLLOW' ) ) ? SPONSORS_NO_FOLLOW : true; - if(!empty($category)) { + if( $atts['category'] ) { + $atts['category'] = explode( ',', $atts['category'] ); $args['tax_query'] = array( array( 'taxonomy' => 'sponsor_categories', 'field' => 'slug', - 'terms' => $category, + 'terms' => $atts['category'], ), ); } + $images = 'no' !== $atts['images'] && 'no' !== $atts['image'] ? true : false; + $debug = $atts['debug'] ? true : false; + $description = 'yes' === $atts['description'] ? true : false; + $title = 'yes' === $atts['title'] ? true : false; // $sizes = array('small' => '15%', 'medium' => '30%', 'large' => '50%', 'full' => '100%', 'default' => '30%'); ob_start(); - // Set default options with then shortcode is used without parameters - // style options defaults to list - if ( empty($atts['style']) ) { $atts['style'] = 'list'; } - // images options default to yes - - $images != 'no' && $image != 'no' ? $images = true : $images = false; - // debug option defaults to false - isset($debug) ? $debug = true : $debug = false; - $description === 'yes' ? $description = true : $description = false; - $title === 'yes' ? $title = true : $title = false; - - $query = new WP_Query($args); + $query = new WP_Query( $args ); // Set up the shortcode styles $style = array(); $layout = $atts['style']; - $shame = new Wp_Sponsors_Shame(); - - switch ($layout) { + switch ( $layout ) { case "list": $style['containerPre'] = '
    '; $style['containerPost'] = '
'; @@ -87,7 +80,7 @@ public static function sponsors_shortcode( $atts ) { break; case "linear": case "grid": - $style['containerPre'] = '
'; + $style['containerPre'] = '
'; $style['containerPost'] = '
'; $style['wrapperClass'] = 'sponsor-item'; $style['wrapperPre'] = 'div'; @@ -97,54 +90,69 @@ public static function sponsors_shortcode( $atts ) { } if ( $query->have_posts() ) { + echo $style['containerPre']; while ( $query->have_posts() ) : $query->the_post(); - if($query->current_post === 0) { echo $style['containerPre']; } - // Check if the sponsor was a link - get_post_meta( get_the_ID(), 'wp_sponsors_url', true ) != '' ? $link = get_post_meta( get_the_ID(), 'wp_sponsors_url', true ) : $link = false; - $link_target = get_post_meta( get_the_ID(), 'wp_sponsor_link_behaviour', true ); - $target = ($link_target == 1) ? 'target="_blank"' : ''; - $class = ''; - $class .= $size; - if($debug) { $class .= ' debug'; } + $sponsor_id = get_the_ID(); + $link = get_post_meta( $sponsor_id, 'wp_sponsors_url', true ); + $link_target = get_post_meta( $sponsor_id, 'wp_sponsor_link_behaviour', true ); + $target = 1 === absint( $link_target ) ? 'target="_blank"' : ''; + $class = ''; + $class .= $atts['size']; + $image = false; + + if( $debug ) { + $class .= ' debug'; + } echo '<' . $style['wrapperPre'] . ' class="' . $style['wrapperClass'] .' ' . $class . '">'; $sponsor = ''; + // Check if we have a link - if($link && !$images) { - $sponsor .= ''; - } - // Check if we have a title - if($title) { - $sponsor .= '

'.get_the_title().'

'; - } - // Close the link tag if we have it - if($link && !$images) { + if( $link && ! $images && $title ) { + $sponsor .= '
'; + $sponsor .= '

' . get_the_title() . '

'; $sponsor .= '
'; } - // Check if we have a link - if($link && $images) { - $sponsor .= ''; - } - // Check if we should do images, just show the title if there's no image set - if($images){ - $sponsor .= $shame->getImage(get_the_ID()); - } elseif ($title === false) { - $sponsor .= '

' . get_the_title() . '

'; + + if ( $images ) { + // Check if we should do images, just show the title if there's no image set + $image = get_the_post_thumbnail( $sponsor_id, $atts['image_size'] ); + + // We did not want title, but we don't have an image. Show the title then. + if ( ! $image && ! $title ) { + $image = '

' . get_the_title() . '

'; + } + + if ( $image ) { + // Check if we have a link + if( $link ) { + $sponsor .= '
'; + } + + $sponsor .= $image; + + // Close the link tag if we have it + if( $link ) { + $sponsor .= ''; + } + } } // Check if we need a description and the description is not empty - if($description) { - $sponsor .= '

' . get_post_meta( get_the_ID(), 'wp_sponsors_desc', true ) . '

'; - } - // Close the link tag if we have it - if($link && $images) { - $sponsor .= ''; + if( $description ) { + $desc = get_post_meta( get_the_ID(), 'wp_sponsors_desc', true ); + + if ( $desc ) { + $sponsor .= '

' . $desc . '

'; + } } + echo $sponsor; echo $style['wrapperPost']; - if( ($query->current_post + 1) === $query->post_count) { echo $style['containerPost']; } + endwhile; + echo $style['containerPost']; wp_reset_postdata(); return ob_get_clean(); } diff --git a/includes/class-wp-sponsors-widget.php b/includes/class-wp-sponsors-widget.php index 6f8b224..5393d46 100644 --- a/includes/class-wp-sponsors-widget.php +++ b/includes/class-wp-sponsors-widget.php @@ -1,22 +1,29 @@ __( 'List your sponsors, per category, with or without images', 'wp-sponsors' ) ) ); + parent::__construct( false, + $name = __( 'Sponsors', 'wp-sponsors' ), + array( + 'description' => __( 'List your sponsors, per category, with or without images', 'wp-sponsors' ) + ) + ); } + /** + * Front display. + * + * @param array $args + * @param array $instance + */ function widget( $args, $instance ) { - extract( $args ); - // WP_Query arguments - if ( $instance['category'] != 'all' && $instance['category'] != '' ) { - $term = $instance['category']; - } - $args = array( + $query_args = array( 'post_type' => array( 'sponsors', 'sponsor' ), // Allowing 'sponsor' in case the update does not work. 'post_status' => 'publish', 'pagination' => false, @@ -26,7 +33,8 @@ function widget( $args, $instance ) { ); if ( $instance['category'] != 'all' && $instance['category'] != '' ) { - $args['tax_query'] = array( + $term = explode( ',', $instance['category'] ); + $query_args['tax_query'] = array( array( 'taxonomy' => 'sponsor_categories', 'field' => 'slug', @@ -48,50 +56,71 @@ function widget( $args, $instance ) { $sponsorStyling = apply_filters( 'sponsors_widget_styling', 'sponsors-item' ); // The Query - $query = new WP_Query( $args ); - $shame = new Wp_Sponsors_Shame(); + $query = new WP_Query( $query_args ); // The Output ?> - + - + __( 'Our sponsors', 'wp-sponsors' ), - 'check_images' => 'on', - 'category' => 'all', - 'display_option' => 'vertical', - 'order_by' => 'menu_order', - 'target_blank' => 'on', - 'max' => '' + $defaults = array( + 'title' => __( 'Our sponsors', 'wp-sponsors' ), + 'check_images' => 'on', + 'category' => 'all', + 'display_option' => 'vertical', + 'order_by' => 'menu_order', + 'target_blank' => 'on', + 'max' => '', + 'show_title' => 'on', + 'show_description' => '', + 'image_size' => 'full' ); $instance = wp_parse_args( (array) $instance, $defaults ); @@ -125,6 +163,8 @@ function form( $instance ) { $key = array( 'check_images' ); $instance = array_fill_keys( $key, 'on' ); } + $images_sizes = array_keys( WP_Sponsors_Extras::get_image_sizes() ); + $images_sizes[] = 'full'; $cats = get_terms( 'sponsor_categories' ); ?>

@@ -183,6 +223,21 @@ function form( $instance ) { name="get_field_name( 'check_images' ); ?>" />

+

+ + +

+

/> @@ -197,5 +252,14 @@ function form( $instance ) { setup(); /** diff --git a/public/css/wp-sponsors-public.css b/public/css/wp-sponsors-public.css index 7697c50..eea4435 100755 --- a/public/css/wp-sponsors-public.css +++ b/public/css/wp-sponsors-public.css @@ -15,8 +15,12 @@ } .widget_sponsors_widget ul.horizontal { - margin: 10px; + display: flex; + justify-content: space-between; + flex-flow: row; + flex-wrap: wrap; } +.widget_sponsors_widget ul.horizontal li.sponsors-item { width: 32%; } .widget_sponsors_widget ul.horizontal li.sponsors-item a img { max-height: 100px; padding-bottom: 15px; @@ -32,7 +36,16 @@ p.sponsor-desc { /* * Styling the for the linear shortcode option */ -div#wp-sponsors { margin-bottom: 50px; display: flex; flex-wrap: wrap; } +div#wp-sponsors { + margin-bottom: 50px; + display: flex; + flex-wrap: wrap; + justify-content: space-between; + align-items: flex-start; +} +div#wp-sponsors.grid { + text-align: center; +} div#wp-sponsors ul li { list-style: none;} #wp-sponsors div.sponsor-item {margin: 0 20px 20px 0; } #wp-sponsors div.sponsor-item.debug {border: solid 1px red;} From 6968b55e23361957525cfa21c4e1fd82c1d66f78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Igor=20Beni=C4=87?= Date: Sat, 6 Jul 2019 01:23:10 +0200 Subject: [PATCH 3/6] Changing desc to post content --- includes/class-wp-sponsors-installer.php | 8 +++----- includes/class-wp-sponsors-loader.php | 2 +- includes/class-wp-sponsors.php | 2 +- includes/functions-updates.php | 9 +++++++++ 4 files changed, 14 insertions(+), 7 deletions(-) diff --git a/includes/class-wp-sponsors-installer.php b/includes/class-wp-sponsors-installer.php index c1176e3..7edf16c 100644 --- a/includes/class-wp-sponsors-installer.php +++ b/includes/class-wp-sponsors-installer.php @@ -95,9 +95,8 @@ private function sponsors_register() { ); $args = array( - //'public' => true, 'labels' => $labels, - 'public' => false, + 'public' => true, 'exclude_from_search' => true, 'publicly_queryable' => false, 'show_ui' => true, @@ -105,14 +104,13 @@ private function sponsors_register() { 'show_in_admin_bar' => false, 'menu_position' => 5, 'menu_icon' => 'dashicons-format-image', - //'query_var' => true, 'rewrite' => false, 'capability_type' => 'post', 'has_archive' => false, 'hierarchical' => false, 'can_export' => true, - 'query_var' => false, - 'supports' => array( 'title', 'page-attributes' ), + 'query_var' => true, + 'supports' => array( 'title', 'page-attributes', 'editor' ), 'taxonomies' => array( 'sponsor_categories' ), ); diff --git a/includes/class-wp-sponsors-loader.php b/includes/class-wp-sponsors-loader.php index fc7dab8..9848deb 100755 --- a/includes/class-wp-sponsors-loader.php +++ b/includes/class-wp-sponsors-loader.php @@ -21,7 +21,7 @@ * @subpackage Wp_Sponsors/includes * @author Jan Henckens */ -class Wp_Sponsors_Loader { +class WP_Sponsors_Loader { /** * The array of actions registered with WordPress. diff --git a/includes/class-wp-sponsors.php b/includes/class-wp-sponsors.php index d99910c..b5d4c4b 100755 --- a/includes/class-wp-sponsors.php +++ b/includes/class-wp-sponsors.php @@ -135,7 +135,7 @@ private function load_dependencies() { */ require_once plugin_dir_path( dirname( __FILE__ ) ) . 'public/class-wp-sponsors-public.php'; - $this->loader = new Wp_Sponsors_Loader(); + $this->loader = new WP_Sponsors_Loader(); } diff --git a/includes/functions-updates.php b/includes/functions-updates.php index 500f1c0..e4712db 100644 --- a/includes/functions-updates.php +++ b/includes/functions-updates.php @@ -43,4 +43,13 @@ function wp_sponsors_update_post_type_300() { global $wpdb; $wpdb->update( $wpdb->posts, array( 'post_type' => 'sponsors' ), array( 'post_type' => 'sponsor' ) ); + + $descriptions = $wpdb->get_results( $wpdb->prepare( 'SELECT post_id, meta_value FROM ' . $wpdb->postmeta . ' WHERE meta_key=%s', 'wp_sponsors_desc' ), ARRAY_A ); + if ( $descriptions ) { + foreach ( $descriptions as $description ) { + $text = $description['meta_value']; + $post_id = $description['post_id']; + $wpdb->update( $wpdb->posts, array( 'post_content' => $text ), array( 'ID' => $post_id ) ); + } + } } \ No newline at end of file From 3a1b63588436568de4404132e3b3773befcc9e86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Igor=20Beni=C4=87?= Date: Sat, 6 Jul 2019 01:53:46 +0200 Subject: [PATCH 4/6] Installing the versions. Checking in case we need an update --- admin/partials/meta-boxes/sponsor-info.php | 4 ++++ includes/class-wp-sponsors-activator.php | 18 ++++++++++++++++++ includes/class-wp-sponsors-installer.php | 12 +++++++++++- includes/class-wp-sponsors-shortcodes.php | 6 ++++-- includes/class-wp-sponsors-widget.php | 5 ++++- includes/functions-updates.php | 5 ++++- wp-sponsors.php | 11 +++++++++++ 7 files changed, 56 insertions(+), 5 deletions(-) create mode 100644 includes/class-wp-sponsors-activator.php diff --git a/admin/partials/meta-boxes/sponsor-info.php b/admin/partials/meta-boxes/sponsor-info.php index 58c096d..cd4b4f6 100644 --- a/admin/partials/meta-boxes/sponsor-info.php +++ b/admin/partials/meta-boxes/sponsor-info.php @@ -8,6 +8,8 @@ // Checks and displays the retrieved value echo '

'; echo ''; + + // Display code/markup goes here. Don't forget to include nonces! // Noncename needed to verify where the data originated // Get the url data if its already been entered @@ -21,7 +23,9 @@ 'textarea_name' => 'wp_sponsors_desc' ); echo '

'; +echo '

Description field will be deleted in 3.1.0. Please move all the content in the default content area above (if it did not move automatically).

'; echo wp_editor( $meta_value, 'wp_sponsors_desc', $editor_settings ); + $meta_value = get_post_meta( get_the_ID(), 'wp_sponsor_link_behaviour', true ); echo '

'; $meta_value = $meta_value == "" ? "1" : $meta_value; diff --git a/includes/class-wp-sponsors-activator.php b/includes/class-wp-sponsors-activator.php new file mode 100644 index 0000000..d4baa27 --- /dev/null +++ b/includes/class-wp-sponsors-activator.php @@ -0,0 +1,18 @@ +get_var( $wpdb->prepare( 'SELECT count(*) FROM ' . $wpdb->postmeta . ' WHERE meta_key=%s', 'wp_sponsors_url' ) ); + if ( absint( $count ) ) { + return true; + } + } + return ! is_null( $current_db_version ) && version_compare( $current_db_version, max( array_keys( $updates ) ), '<' ); } /** * See if we need to show or run database updates during install. * - * @since 3.2.0 + * @since 3.0.0 */ private static function maybe_update_db_version() { if ( self::needs_db_update() ) { diff --git a/includes/class-wp-sponsors-shortcodes.php b/includes/class-wp-sponsors-shortcodes.php index 09a3ef5..9d0a828 100644 --- a/includes/class-wp-sponsors-shortcodes.php +++ b/includes/class-wp-sponsors-shortcodes.php @@ -141,8 +141,10 @@ public static function sponsors_shortcode( $atts = array() ) { // Check if we need a description and the description is not empty if( $description ) { - $desc = get_post_meta( get_the_ID(), 'wp_sponsors_desc', true ); - + $desc = do_shortcode( wpautop( get_the_content( get_the_ID() ) ) ); + if ( ! $desc ) { + $desc = get_post_meta( get_the_ID(), 'wp_sponsors_desc', true ); + } if ( $desc ) { $sponsor .= '

' . $desc . '

'; } diff --git a/includes/class-wp-sponsors-widget.php b/includes/class-wp-sponsors-widget.php index 5393d46..2e0289e 100644 --- a/includes/class-wp-sponsors-widget.php +++ b/includes/class-wp-sponsors-widget.php @@ -97,7 +97,10 @@ function widget( $args, $instance ) { } if ( isset( $instance['show_description'] ) && $instance['show_description'] === "on" ) { - $desc = get_post_meta( get_the_ID(), 'wp_sponsors_desc', true ); + $desc = do_shortcode( wpautop( get_the_content( get_the_ID() ) ) ); + if ( ! $desc ) { + $desc = get_post_meta( get_the_ID(), 'wp_sponsors_desc', true ); + } if ( $desc ) { echo '
'; echo ''; diff --git a/includes/functions-updates.php b/includes/functions-updates.php index e4712db..d093772 100644 --- a/includes/functions-updates.php +++ b/includes/functions-updates.php @@ -49,7 +49,10 @@ function wp_sponsors_update_post_type_300() { foreach ( $descriptions as $description ) { $text = $description['meta_value']; $post_id = $description['post_id']; - $wpdb->update( $wpdb->posts, array( 'post_content' => $text ), array( 'ID' => $post_id ) ); + $ret = $wpdb->update( $wpdb->posts, array( 'post_content' => $text ), array( 'ID' => $post_id ) ); + if ( false !== $ret ) { + delete_post_meta( $post_id, 'wp_sponsors_desc' ); + } } } } \ No newline at end of file diff --git a/wp-sponsors.php b/wp-sponsors.php index 4cc59f3..7e4fd03 100755 --- a/wp-sponsors.php +++ b/wp-sponsors.php @@ -34,6 +34,17 @@ define( 'WP_SPONSORS_FILE', __FILE__ ); } +/** + * The code that runs during plugin activation. + * This action is documented in includes/class-wp-sponsors-activator.php + */ +function activate_wp_sponsors() { + require_once plugin_dir_path( __FILE__ ) . 'includes/class-wp-sponsors-activator.php'; + WP_Sponsors_Activator::activate(); +} + +register_activation_hook( __FILE__, 'activate_wp_sponsors' ); + /** * The core plugin class that is used to define internationalization, * dashboard-specific hooks, and public-facing site hooks. From fa6b5abcc34806286487aeb58fc4e6dac65a14a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Igor=20Beni=C4=87?= Date: Sat, 6 Jul 2019 13:13:37 +0200 Subject: [PATCH 5/6] Updating readme --- README.txt | 137 +++++------- admin/class-wp-sponsors-admin.php | 10 +- admin/partials/meta-boxes/sponsor-info.php | 14 +- includes/class-wp-sponsors-installer.php | 2 +- includes/class-wp-sponsors-shortcodes.php | 235 ++++++++++++++++----- includes/class-wp-sponsors-widget.php | 6 +- includes/class-wp-sponsors.php | 1 + includes/functions-updates.php | 5 +- public/class-wp-sponsors-public.php | 88 ++++++++ public/css/wp-sponsors-public.css | 29 ++- wp-sponsors.php | 12 -- 11 files changed, 375 insertions(+), 164 deletions(-) diff --git a/README.txt b/README.txt index b06baed..b0be82d 100755 --- a/README.txt +++ b/README.txt @@ -1,11 +1,11 @@ -=== Plugin Name === +=== Sponsors === Contributors: ibenic -Donate link: https://www.ibenic.com +Donate link: http://www.wpsimplesponsorships.com Tags: post type, images, partners, sponsors Requires at least: 3.0.1 -Tested up to: 4.9.7 +Tested up to: 5.2.2 Requires PHP: 7.0 -Stable tag: 2.5.1 +Stable tag: 3.0.0 License: GPLv2 or later License URI: http://www.gnu.org/licenses/gpl-2.0.html @@ -13,7 +13,8 @@ Sponsors makes it easy to add your sponsors and partners to your WordPress websi == Description == -Ever had to add a bunch of images with links on them for your event/company partners? With Sponsors, you won't have to use a text widget for that anymore. The companies and people that support you, your company or your event now get a separate place in the dashboar where you can add a link and an image for each of them. Then you add the Sponsors widget to the sidebar of your choosing and the linked images will show up there. +Ever had to add a bunch of images with links on them for your event/company partners? +With Sponsors, you won't have to use a text widget for that anymore. The companies and people that support you, your company or your event now get a separate place in the dashboar where you can add a link and an image for each of them. Then you add the Sponsors widget to the sidebar of your choosing and the linked images will show up there. == Installation == @@ -27,109 +28,73 @@ Ever had to add a bunch of images with links on them for your event/company part 1. After you activate the plugin, a new content type will become available on the your WordPress admin panel. Here you can add a link, a description and an image for each Sponsor. 2. Add the widget to one or more sidebars and the images of your sponsors will show up there. Choose from which category you want to display sponsors (or select all), to show or hide images and the description -== Shortcode == - -=== Shortcode === +== Shortcodes == +** Sponsors ** The shortcode [sponsors] takes the following options: * images (yes|no, default: yes) +* image_size (medium|full|thumbnail|large|[any registered image size], default: medium) * description (yes|no, default: no) * max (number, default: none, showing all entries) * title (yes|no, default: no) * category (category-slug, default: all) +* with_categories (yes|no, default: no ) +* category_title ( HTML tag for category title, default: h3) * size (small|medium|large|full, default: medium) * style (list|grid, default: list) +* order (ASC|DESC, default: ASC ) +* orderby (menu_order|post_title|..., default: menu_order) -== Changelog == - -= 2.5.1 = -* Fixed a PHP 7.2 deprecation error - -= 2.5.0 = -* Fixed a small error and the plugin now works with Gutenburg! - -= 2.4.1 = -* Fixed php errors when using the shortcode without any options - -= 2.4.0 = -* Better responsive styling for the shortcode grid -* Moved all fields to a single metabox in the admin dashboard - -= 2.3.3 = -* Fixed syntax errors in upgrade script for 2.0 -* Added link behaviour settings to shortcode - -= 2.3.2 = -* Sort by menu_order by default in the shortcode +When with_categories is used, it will show sponsors under their appropriate categories. +The attribute size is used to define the size of the columns. More style updates will come in future versions. -= 2.3.1 = -* Fixed a critical issue where we assumed your table prefix was 'wp_' -* Fixed the max settings in the shortcode +** Form ** +This shortcode will display a form so potential sponsors can submit their information. +Each Sponsor entered with that form will become a Draft so they won't be displayed immediately. +You can publish them, or send them an email to get their logo as well. -= 2.3.0 = -* Fixed equal height grid layout for the shortcode -* Added a per-sponsor setting to open the sponsor link in a new window +Shortcode [sponsors_acquisition_form] will allow it. Fields that are used here are: +* Name +* Email (so you can email them about their submission) +* Description +* Link -= 2.2.3 = -* Fixes an issue where the shortcode would take over the current page's post type and edit link. +== Planned Features == -= 2.2.2 = -* Changed how sponsors are links in the shortcode. No using the no-images option, the title will be linked and the description won't be. With images, the image will link and the title won't. +Here are some of the features planned for future versions: -= 2.2.1 = -* Bugfix +* Form Block - for the new WordPress block editor +* Sponsors Block - for the new WordPress block editor +* Category ordering +* Documentation Page +* Front optimizations -= 2.2 = -* Added translations for fr_BE and fr_FR -* Added a filter called "sponsors_widget_styling" to add a css class to each sponsor item from your theme +== Upgrade Notice == -= 2.1 = -* Added number of entries and title options to the shortcode -* Added number of entries option to the widget -* Added orderby title and random options to the widget += 3.0.0 = +3.0.0 is a big update. Make a full site backup. -= 2.0.3 = -* Fixed a problem where the sponsor link wouldn't be shown when using no images in the shortcode - -= 2.0.2 = -* The "no images" options in the shortcode now actually works -* Improved styling of the widget title across themes -* Updated translations for nl_NL and nl_BE - -= 2.0.1 = -* Fixes a PHP notice when installing the plugin for the first time - -= 2.0.0 = -* The plugin now uses the featured image field to save the sponsor's logo -* Improved shortcode code and added "grid" style option -* Added debug option to shortcode to better support layout issues - -= 1.9.1 = -* Bugfix in the nofollow feature - -= 1.9.0 = -* Improved sponsor description saving -* Added default rel="nofollow" for sponsor links -* Added support and donate links to plugin description - -= 1.8.5 = -* In the shortcode, items are now sorted by the menu_order by default. +== Changelog == -= 1.8.4 = -* PHP 7 Compatibilty -* Fixes a bug with the category selection in the shortcode, props to joachimjusth += 3.0.0 = +* Big Code refactor. +* Refactor: URL is stored under _website. Backwards compatibility is available. +* Refactor: Description is stored under the regular content so we don't waste the database space. +* Refactor: Post Type has changed from 'sponsor' to 'sponsors'. +* Refactor: CSS for horizontal widget display has been enabled. +* New: Image sizes can be defined in shortcode and in widget. +* New: Sponsors can be display under appropriate categories in shortcode. +* New: Sponsor Acquisition Form shortcode for front end information submission. +* New: Simple Sponsorships compatibility. -= 1.8.3 = -* One last fix to the category selection in the widget, this should have all cases possible now. += 2.5.1 = +* Fixed a PHP 7.2 deprecation error -= 1.8.2 = -* Fixes a bug caused in 1.8.1 where widgets that selected all sponsor categories wouldn't display any entries. += 2.5.0 = +* Fixed a small error and the plugin now works with Gutenburg! -= 1.8.1 = -* Fixes a problem with category filtering on the widget += 2.4.1 = +* Fixed php errors when using the shortcode without any options -= 1.8.0 = -* Added "show sponsor title" option to the widget, not checked by default -* Updated the translations files with the latest strings and included an updatedd nl_NL translation More information of older versions can be found in changelog.txt diff --git a/admin/class-wp-sponsors-admin.php b/admin/class-wp-sponsors-admin.php index f44f20c..a1a48eb 100755 --- a/admin/class-wp-sponsors-admin.php +++ b/admin/class-wp-sponsors-admin.php @@ -122,12 +122,18 @@ public function save_meta_boxes( $post_id ) { return; } // Checks for input and sanitizes/saves if needed - if ( isset( $_POST['wp_sponsors_url'] ) ) { - update_post_meta( $post_id, 'wp_sponsors_url', sanitize_text_field( $_POST['wp_sponsors_url'] ) ); + if ( isset( $_POST['_website'] ) ) { + update_post_meta( $post_id, '_website', sanitize_text_field( $_POST['_website'] ) ); } + + if ( isset( $_POST['_email'] ) ) { + update_post_meta( $post_id, '_email', sanitize_text_field( $_POST['_email'] ) ); + } + if ( isset( $_POST['wp_sponsors_desc'] ) ) { update_post_meta( $post_id, 'wp_sponsors_desc', $_POST['wp_sponsors_desc'] ); } + $link_behaviour = isset($_POST['wp_sponsor_link_behaviour']) ? '1' : '0'; update_post_meta( $post_id, 'wp_sponsor_link_behaviour', $link_behaviour ); } diff --git a/admin/partials/meta-boxes/sponsor-info.php b/admin/partials/meta-boxes/sponsor-info.php index cd4b4f6..d5094cd 100644 --- a/admin/partials/meta-boxes/sponsor-info.php +++ b/admin/partials/meta-boxes/sponsor-info.php @@ -4,10 +4,20 @@ // Noncename needed to verify where the data originated echo ''; // Get the url data if its already been entered -$meta_value = get_post_meta( get_the_ID(), 'wp_sponsors_url', true ); +$meta_value = get_post_meta( get_the_ID(), '_website', true ); +if ( ! $meta_value ) { + $meta_value = get_post_meta( get_the_ID(), 'wp_sponsors_url', true ); +} // Checks and displays the retrieved value echo '

'; -echo ''; +echo ''; + + +// Get the url data if its already been entered +$meta_value = get_post_meta( get_the_ID(), '_email', true ); +// Checks and displays the retrieved value +echo '

'; +echo ''; // Display code/markup goes here. Don't forget to include nonces! diff --git a/includes/class-wp-sponsors-installer.php b/includes/class-wp-sponsors-installer.php index 4d7ee97..16b43d5 100644 --- a/includes/class-wp-sponsors-installer.php +++ b/includes/class-wp-sponsors-installer.php @@ -66,7 +66,7 @@ private function create_sponsor_taxonomies() { 'query_var' => true ); // Register the sponsors taxonomy - register_taxonomy( 'sponsor_categories', array( 'sponsor' ), $args ); + register_taxonomy( 'sponsor_categories', array( 'sponsors' ), $args ); } /** diff --git a/includes/class-wp-sponsors-shortcodes.php b/includes/class-wp-sponsors-shortcodes.php index 9d0a828..ae9464f 100644 --- a/includes/class-wp-sponsors-shortcodes.php +++ b/includes/class-wp-sponsors-shortcodes.php @@ -7,8 +7,77 @@ class WP_Sponsors_Shortcodes { */ public function register_shortcodes() { add_shortcode( 'sponsors', array( __CLASS__, 'sponsors_shortcode' ) ); + add_shortcode( 'sponsors_acquisition_form', array( __CLASS__, 'sponsors_form' ) ); } + /** + * Sponsor Form + * @return string + */ + public static function sponsors_form( $atts = array() ) { + $atts = shortcode_atts( array ( + 'fields' => '', + 'fields_labels' => '', + 'button' => __( 'Submit', 'wp-sponsors' ), + ), $atts, 'wp_sponsors_form' ); + + + ob_start(); + ?> +
+ + +
+ + +
+ +
+ + +
+ +
+ + +
+ +
+ + +
+ + $field ) { + $label = isset( $labels[ $index ] ) ? $labels[ $index ] : ucfirst( $field ); + ?> +
+ + +
+ + + + +
+ 'post', 'image' => 'yes', 'images' => 'yes', 'category' => '', + 'with_categories' => 'no', + 'category_title' => 'h3', 'size' => 'default', 'image_size' => 'medium', 'style' => 'list', @@ -38,6 +108,7 @@ public static function sponsors_shortcode( $atts = array() ) { 'post_type' => array( 'sponsors', 'sponsor' ), // Allowing 'sponsor' in case the update does not work. 'post_status' => 'publish', 'pagination' => false, + 'no_found_rows' => true, 'order' => $atts['order'], 'orderby' => isset( $atts['orderby'] ) ? $atts['orderby'] : 'menu_order', 'posts_per_page' => isset( $atts['max'] ) ? $atts['max'] : '-1', @@ -57,14 +128,61 @@ public static function sponsors_shortcode( $atts = array() ) { ); } - $images = 'no' !== $atts['images'] && 'no' !== $atts['image'] ? true : false; - $debug = $atts['debug'] ? true : false; - $description = 'yes' === $atts['description'] ? true : false; - $title = 'yes' === $atts['title'] ? true : false; - // $sizes = array('small' => '15%', 'medium' => '30%', 'large' => '50%', 'full' => '100%', 'default' => '30%'); - ob_start(); + $images = 'no' !== $atts['images'] && 'no' !== $atts['image'] ? true : false; + $debug = $atts['debug'] ? true : false; + $description = 'yes' === $atts['description'] ? true : false; + $title = 'yes' === $atts['title'] ? true : false; + $sponsor_posts = get_posts( $args ); + $sponsors = array(); + $categories = array(); + + foreach ( $sponsor_posts as $sponsor_post ) { + $link = get_post_meta( $sponsor_post->ID, '_website', true ); + + if ( ! $link ) { + $link = get_post_meta( $sponsor_post->ID, 'wp_sponsors_url', true ); + } + + $sponsor = array(); + $sponsor['id'] = $sponsor_post->ID; + $sponsor['link'] = $link; + $sponsor['link_target'] = get_post_meta( $sponsor_post->ID, 'wp_sponsor_link_behaviour', true ); + $sponsor['logo'] = get_the_post_thumbnail( $sponsor_post->ID, $atts['image_size'] ); + $sponsor['title'] = get_the_title( $sponsor_post ); + $sponsor['categories'] = array(); + $desc = do_shortcode( wpautop( $sponsor_post->post_content ) ); + if ( ! $desc ) { + $desc = get_post_meta( $sponsor_post->ID, 'wp_sponsors_desc', true ); + } + + if( 'yes' === $atts['with_categories'] ) { + $sponsor['categories'] = get_the_terms( $sponsor_post, 'sponsor_categories'); + } + + $sponsor['desc'] = $desc; + $sponsors[] = $sponsor; + } - $query = new WP_Query( $args ); + if( 'yes' === $atts['with_categories'] ) { + foreach( $sponsors as $sponsor ) { + if ( $sponsor['categories'] ) { + foreach ( $sponsor['categories'] as $term ) { + if ( ! isset( $categories[ $term->term_id ] ) ) { + $categories[ $term->term_id ] = array( + 'title' => $term->name, + 'sponsors' => array() + ); + } + $categories[ $term->term_id ]['sponsors'][] = $sponsor; + } + } + } + } else { + // Get all under one category so we can iterate through them. + $categories[0] = array( 'title' => '', 'sponsors' => $sponsors ); + } + + ob_start(); // Set up the shortcode styles $style = array(); @@ -89,73 +207,74 @@ public static function sponsors_shortcode( $atts = array() ) { break; } - if ( $query->have_posts() ) { - echo $style['containerPre']; - while ( $query->have_posts() ) : $query->the_post(); - - $sponsor_id = get_the_ID(); - $link = get_post_meta( $sponsor_id, 'wp_sponsors_url', true ); - $link_target = get_post_meta( $sponsor_id, 'wp_sponsor_link_behaviour', true ); - $target = 1 === absint( $link_target ) ? 'target="_blank"' : ''; - $class = ''; - $class .= $atts['size']; - $image = false; + if ( $sponsors ) { + foreach ( $categories as $category ) { - if( $debug ) { - $class .= ' debug'; + if ( isset( $category['title'] ) && $category['title'] ) { + echo '<' . $atts['category_title'] . '>' . $category['title'] . ''; } + $_sponsors = $category['sponsors']; + echo $style['containerPre']; + foreach ( $_sponsors as $sponsor ) { - echo '<' . $style['wrapperPre'] . ' class="' . $style['wrapperClass'] .' ' . $class . '">'; - $sponsor = ''; + $link = $sponsor['link']; + $link_target = $sponsor['link_target']; + $target = 1 === absint( $link_target ) ? 'target="_blank"' : ''; + $class = ''; + $class .= $atts['size']; - // Check if we have a link - if( $link && ! $images && $title ) { - $sponsor .= '
'; - $sponsor .= '

' . get_the_title() . '

'; - $sponsor .= '
'; - } + if ( $debug ) { + $class .= ' debug'; + } - if ( $images ) { - // Check if we should do images, just show the title if there's no image set - $image = get_the_post_thumbnail( $sponsor_id, $atts['image_size'] ); + echo '<' . $style['wrapperPre'] . ' class="' . $style['wrapperClass'] . ' ' . $class . '">'; + $sponsor_html = ''; - // We did not want title, but we don't have an image. Show the title then. - if ( ! $image && ! $title ) { - $image = '

' . get_the_title() . '

'; + // Check if we have a link + if ( $link && ! $images && $title ) { + $sponsor_html .= ''; + $sponsor_html .= '

' . $sponsor['title'] . '

'; + $sponsor_html .= '
'; } - if ( $image ) { - // Check if we have a link - if( $link ) { - $sponsor .= ''; + if ( $images ) { + // Check if we should do images, just show the title if there's no image set + $image = $sponsor['logo']; + + // We did not want title, but we don't have an image. Show the title then. + if ( ! $image && ! $title ) { + $image = '

' . $sponsor['title'] . '

'; } - $sponsor .= $image; + if ( $image ) { + // Check if we have a link + if ( $link ) { + $sponsor_html .= '
'; + } + + $sponsor_html .= $image; - // Close the link tag if we have it - if( $link ) { - $sponsor .= ''; + // Close the link tag if we have it + if ( $link ) { + $sponsor_html .= ''; + } } } - } - // Check if we need a description and the description is not empty - if( $description ) { - $desc = do_shortcode( wpautop( get_the_content( get_the_ID() ) ) ); - if ( ! $desc ) { - $desc = get_post_meta( get_the_ID(), 'wp_sponsors_desc', true ); - } - if ( $desc ) { - $sponsor .= '

' . $desc . '

'; + // Check if we need a description and the description is not empty + if ( $description ) { + $desc = $sponsor['desc']; + if ( $desc ) { + $sponsor_html .= '

' . $desc . '

'; + } } - } - echo $sponsor; - echo $style['wrapperPost']; + echo $sponsor_html; + echo $style['wrapperPost']; - endwhile; - echo $style['containerPost']; - wp_reset_postdata(); + } + echo $style['containerPost']; + } return ob_get_clean(); } } diff --git a/includes/class-wp-sponsors-widget.php b/includes/class-wp-sponsors-widget.php index 2e0289e..209edf9 100644 --- a/includes/class-wp-sponsors-widget.php +++ b/includes/class-wp-sponsors-widget.php @@ -67,7 +67,11 @@ function widget( $args, $instance ) { have_posts() ) : $query->the_post(); ?> loader->add_action( 'wp_enqueue_scripts', $plugin_public, 'enqueue_styles' ); $this->loader->add_action( 'wp_enqueue_scripts', $plugin_public, 'enqueue_scripts' ); + $this->loader->add_action( 'init', $plugin_public, 'sponsors_acquisition_form_submit' ); } diff --git a/includes/functions-updates.php b/includes/functions-updates.php index d093772..b49ef90 100644 --- a/includes/functions-updates.php +++ b/includes/functions-updates.php @@ -32,7 +32,6 @@ function wp_sponsors_update_200() { ); } } - $wpdb->insert($wpdb->prefix . 'options', array( 'option_name' => 'sponsors_db_version', 'option_value' => 2), array( '%s', '%d' )); return; } @@ -53,6 +52,10 @@ function wp_sponsors_update_post_type_300() { if ( false !== $ret ) { delete_post_meta( $post_id, 'wp_sponsors_desc' ); } + + $url = get_post_meta( $post_id, 'wp_sponsors_url', true ); + update_post_meta( $post_id, '_website', $url ); + delete_post_meta( $post_id, 'wp_sponsors_url' ); } } } \ No newline at end of file diff --git a/public/class-wp-sponsors-public.php b/public/class-wp-sponsors-public.php index dd3faa3..3459e2b 100755 --- a/public/class-wp-sponsors-public.php +++ b/public/class-wp-sponsors-public.php @@ -40,6 +40,21 @@ class WP_Sponsors_Public { */ private $version; + /** + * Form Errors + * + * @var array + */ + public $form_errors = array(); + + + /** + * Form Notices + * + * @var array + */ + public $form_notices = array(); + /** * Initialize the class and set its properties. * @@ -100,4 +115,77 @@ public function enqueue_scripts() { } + /** + * Show Errors and notices on the Submission form. + */ + public function show_errors_and_notices() { + if ( $this->form_errors ) { + foreach ( $this->form_errors as $error ) { + echo '
' . $error . '
'; + } + } + + if ( $this->form_notices ) { + foreach ( $this->form_notices as $notice ) { + echo '
' . $notice . '
'; + } + } + } + + /** + * Form Submission for Sponsors. + */ + public function sponsors_acquisition_form_submit() { + if ( ! isset( $_POST['sponsors_acquisition_form_nonce'] ) ) { + return; + } + + if ( ! $_POST['sponsors_acquisition_form_nonce'] || ! wp_verify_nonce( $_POST['sponsors_acquisition_form_nonce'], 'sponsors_acquisition_form' ) ) { + return; + } + + $posted_data = isset( $_POST['wp_sponsors_form'] ) ? $_POST['wp_sponsors_form'] : array(); + + if ( ! $posted_data ) { + return; + } + + $name = isset( $posted_data['name'] ) ? $posted_data['name'] : ''; + + if ( ! $name ) { + $this->form_errors[] = __( 'Sponsor Name is required', 'wp-sponsors' ); + } + + $email = isset( $posted_data['email'] ) ? $posted_data['email'] : ''; + + if ( ! $email ) { + $this->form_errors[] = __( 'Sponsor Email is required', 'wp-sponsors' ); + } + + $desc = isset( $posted_data['desc'] ) ? $posted_data['desc'] : ''; + $url = isset( $posted_data['website'] ) ? $posted_data['website'] : ''; + + do_action( 'sponsors_acquisition_form_before_submit', $this, $posted_data ); + + if ( ! $this->form_errors ) { + $post = wp_insert_post( + array( + 'post_title' => $name, + 'post_content' => $desc, + 'post_type' => 'sponsors' + ), + true + ); + + if ( ! is_wp_error( $post ) ) { + $this->form_notices[] = __( 'Sponsor Information submitted.', 'wp-sponsors' ); + update_post_meta( $post, '_website', $url ); + update_post_meta( $post, '_email', $email ); + do_action( 'sponsors_acquisition_form_submitted', $post ); + } + } + + add_action( 'wp_sponsors_acquisition_form_fields_before', array( $this, 'show_errors_and_notices' ) ); + } + } diff --git a/public/css/wp-sponsors-public.css b/public/css/wp-sponsors-public.css index eea4435..7888460 100755 --- a/public/css/wp-sponsors-public.css +++ b/public/css/wp-sponsors-public.css @@ -82,4 +82,31 @@ div#wp-sponsors ul li { list-style: none;} #wp-sponsors div.sponsor-item.large {max-width: 100%; width: 100%; } -} \ No newline at end of file +} + +/** Form **/ +.wp-sponsors-form-field { + margin-bottom: 1em; +} + +.wp-sponsors-form-field label { + display: block; +} + +.wp-sponsors-form-field textarea, +.wp-sponsors-form-field input:not([type=checkbox]):not([type=radio]) { + display: block; + width: 100%; +} + +.wp-sponsors-form-notice { + padding: 1em; + text-align: center; + background: rgba(60, 206, 81, 0.05); +} + +.wp-sponsors-form-notice.wp-sponsors-form-error { + background: rgba(218, 52, 52, 0.05); + color: #b52020; +} + diff --git a/wp-sponsors.php b/wp-sponsors.php index 7e4fd03..76fb033 100755 --- a/wp-sponsors.php +++ b/wp-sponsors.php @@ -1,18 +1,6 @@ Date: Sat, 6 Jul 2019 13:15:37 +0200 Subject: [PATCH 6/6] updating readme --- README.txt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.txt b/README.txt index b0be82d..aedac81 100755 --- a/README.txt +++ b/README.txt @@ -54,6 +54,7 @@ Each Sponsor entered with that form will become a Draft so they won't be display You can publish them, or send them an email to get their logo as well. Shortcode [sponsors_acquisition_form] will allow it. Fields that are used here are: + * Name * Email (so you can email them about their submission) * Description @@ -68,6 +69,7 @@ Here are some of the features planned for future versions: * Category ordering * Documentation Page * Front optimizations +* Elementor Blocks == Upgrade Notice == @@ -84,7 +86,7 @@ Here are some of the features planned for future versions: * Refactor: CSS for horizontal widget display has been enabled. * New: Image sizes can be defined in shortcode and in widget. * New: Sponsors can be display under appropriate categories in shortcode. -* New: Sponsor Acquisition Form shortcode for front end information submission. +* New: Sponsor Acquisition Form shortcode [sponsors_acquisition_form] for front end information submission. * New: Simple Sponsorships compatibility. = 2.5.1 =