| 1 |
<?php |
| 2 |
/* |
| 3 |
* Plugin Name: WP Adverts |
| 4 |
* Plugin URI: http://wpadverts.com/ |
| 5 |
* Description: The lightweight WordPress classifieds plugin done right. |
| 6 |
* Author: Greg Winiarski |
| 7 |
* Text Domain: adverts |
| 8 |
* Version: 1.0.2 |
| 9 |
* |
| 10 |
* Adverts is free software: you can redistribute it and/or modify |
| 11 |
* it under the terms of the GNU General Public License as published by |
| 12 |
* the Free Software Foundation, either version 2 of the License, or |
| 13 |
* any later version. |
| 14 |
* |
| 15 |
* Adverts is distributed in the hope that it will be useful, |
| 16 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 |
* GNU General Public License for more details. |
| 19 |
* |
| 20 |
* You should have received a copy of the GNU General Public License |
| 21 |
* along with Adverts. If not, see <http://www.gnu.org/licenses/>. |
| 22 |
* |
| 23 |
* @package Adverts |
| 24 |
* @category Core |
| 25 |
* @author Grzegorz Winiarski |
| 26 |
* @version 1.0.0 |
| 27 |
*/ |
| 28 |
|
| 29 |
if( !defined("ADVERTS_FILE") ) { |
| 30 |
define( "ADVERTS_FILE", __FILE__ ); |
| 31 |
define( "ADVERTS_PATH", plugin_dir_path( ADVERTS_FILE ) ); |
| 32 |
define( "ADVERTS_URL", plugins_url() . "/" . basename(ADVERTS_PATH) ); |
| 33 |
} |
| 34 |
|
| 35 |
// define global $adverts_config variable |
| 36 |
$adverts_config = null; |
| 37 |
|
| 38 |
// define global $adverts_namespace variable |
| 39 |
$adverts_namespace = array( 'config' => array( |
| 40 |
'option_name' => 'adverts_config', |
| 41 |
'default' => array( |
| 42 |
'module' => array(), |
| 43 |
'license' => array(), |
| 44 |
'currency_code' => 'USD', |
| 45 |
'currency_sign' => '$', |
| 46 |
'currency_sign_type' => 'p', // either p=prefix or s=suffix |
| 47 |
'currency_decimals' => 2, |
| 48 |
'currency_char_decimal' => '.', |
| 49 |
'currency_char_thousand' => ',', |
| 50 |
'visibility' => 30, |
| 51 |
'ads_list_id' => null, |
| 52 |
) |
| 53 |
) ); |
| 54 |
|
| 55 |
/** |
| 56 |
* Main Adverts Init Function |
| 57 |
* |
| 58 |
* Registers: custom post types, additional post statuses, taxonomies, image sizes |
| 59 |
* and Adverts scripts. |
| 60 |
* |
| 61 |
* @global WP_Rewrite $wp_rewrite |
| 62 |
* @since 0.1 |
| 63 |
* @return void |
| 64 |
*/ |
| 65 |
function adverts_init() { |
| 66 |
|
| 67 |
wp_register_style( 'adverts-icons', ADVERTS_URL . '/assets/css/adverts-glyphs.css' ); |
| 68 |
wp_register_style( 'adverts-icons-animate', ADVERTS_URL . '/assets/css/animation.css' ); |
| 69 |
|
| 70 |
load_plugin_textdomain("adverts", false, dirname(plugin_basename(__FILE__))."/languages/"); |
| 71 |
|
| 72 |
register_post_status( 'expired', array( |
| 73 |
'label' => _x( 'Expired', 'post' ), |
| 74 |
'public' => is_admin(), |
| 75 |
'internal' => false, |
| 76 |
'label_count' => _n_noop( 'Expired <span class="count">(%s)</span>', 'Expired <span class="count">(%s)</span>' ) |
| 77 |
) ); |
| 78 |
|
| 79 |
register_post_status( 'advert_tmp', array( |
| 80 |
'label' => _x( 'Temporary', 'post' ), |
| 81 |
'public' => false, |
| 82 |
'internal' => true, |
| 83 |
'label_count' => _n_noop( 'Temporary <span class="count">(%s)</span>', 'Temporary <span class="count">(%s)</span>' ) |
| 84 |
) ); |
| 85 |
|
| 86 |
$labels = array( |
| 87 |
'name' => _x( 'Classifieds', 'post type general name', 'adverts' ), |
| 88 |
'singular_name' => _x( 'Classified', 'post type singular name', 'adverts' ), |
| 89 |
'add_new' => _x( 'Add New', 'classified', 'adverts' ), |
| 90 |
'add_new_item' => __( 'Add New Classified', 'adverts' ), |
| 91 |
'edit_item' => __( 'Edit Classified', 'adverts' ), |
| 92 |
'new_item' => __( 'New Classified', 'adverts' ), |
| 93 |
'all_items' => __( 'All Classifieds', 'adverts' ), |
| 94 |
'view_item' => __( 'View Classified', 'adverts' ), |
| 95 |
'search_items' => __( 'Search Classifieds', 'adverts' ), |
| 96 |
'not_found' => __( 'No Classifieds found', 'adverts' ), |
| 97 |
'not_found_in_trash' => __( 'No Classifieds found in the Trash', 'adverts' ), |
| 98 |
'parent_item_colon' => '', |
| 99 |
'menu_name' => __( 'Classifieds', 'adverts' ) |
| 100 |
); |
| 101 |
|
| 102 |
$args = array( |
| 103 |
'labels' => $labels, |
| 104 |
'description' => 'Holds our products and product specific data', |
| 105 |
'public' => true, |
| 106 |
'menu_icon' => 'dashicons-megaphone', |
| 107 |
'menu_position' => 5, |
| 108 |
'supports' => array( 'title', 'editor', 'author' ), |
| 109 |
'taxonomies' => array( 'advert_category' ), |
| 110 |
'has_archive' => true, |
| 111 |
); |
| 112 |
|
| 113 |
register_post_type( 'advert', apply_filters( 'adverts_post_type', $args, 'advert') ); |
| 114 |
|
| 115 |
$args = array( |
| 116 |
'hierarchical' => true, |
| 117 |
'query_var' => true, |
| 118 |
'rewrite' => array('slug' => 'advert-category') |
| 119 |
); |
| 120 |
|
| 121 |
register_taxonomy( 'advert_category', 'advert', apply_filters('adverts_register_taxonomy', $args, 'advert_category') ); |
| 122 |
|
| 123 |
add_image_size( "adverts-upload-thumbnail", 150, 105, true ); |
| 124 |
add_image_size( "adverts-list", 310, 190, true); |
| 125 |
|
| 126 |
include_once ADVERTS_PATH . 'includes/class-adverts.php'; |
| 127 |
include_once ADVERTS_PATH . 'includes/class-flash.php'; |
| 128 |
include_once ADVERTS_PATH . 'includes/class-post.php'; |
| 129 |
include_once ADVERTS_PATH . 'includes/events.php'; |
| 130 |
include_once ADVERTS_PATH . 'includes/functions.php'; |
| 131 |
|
| 132 |
include_once ADVERTS_PATH . 'includes/defaults.php'; |
| 133 |
|
| 134 |
$currency = Adverts::instance()->get("currency"); |
| 135 |
|
| 136 |
wp_register_script( 'adverts-auto-numeric', ADVERTS_URL .'/assets/js/auto-numeric.js', array(), false, true); |
| 137 |
wp_register_script( 'adverts-multiselect', ADVERTS_URL . '/assets/js/adverts-multiselect.js', array(), false, true); |
| 138 |
wp_register_script( 'adverts-gallery', ADVERTS_URL . '/assets/js/adverts-gallery.js', array('plupload-all'), false, true); |
| 139 |
|
| 140 |
wp_localize_script( 'adverts-auto-numeric', 'adverts_currency', array( |
| 141 |
"aSign" => $currency["sign"], |
| 142 |
"pSign" => $currency["sign_type"], |
| 143 |
"aSep" => $currency["char_thousand"], |
| 144 |
"aDec" => $currency["char_decimal"] |
| 145 |
)); |
| 146 |
|
| 147 |
wp_localize_script( 'adverts-multiselect', 'adverts_multiselect_lang', array( |
| 148 |
"hint" => __("Select options ...", "adverts") |
| 149 |
)); |
| 150 |
|
| 151 |
wp_localize_script( 'adverts-gallery', 'adverts_gallery_lang', array( |
| 152 |
"edit_image" => __( "Edit Image", "adverts" ), |
| 153 |
"delete_image" => __( "Delete Image", "adverts" ), |
| 154 |
"view_image" => __( "View Full Image", "adverts" ), |
| 155 |
"featured" => __( "Main", "adverts" ), |
| 156 |
)); |
| 157 |
|
| 158 |
$module = adverts_config( 'config.module' ); |
| 159 |
|
| 160 |
foreach((array)$module as $mod => $status) { |
| 161 |
if(!is_file(ADVERTS_PATH . "addons/$mod/$mod.php")) { |
| 162 |
continue; |
| 163 |
} |
| 164 |
if($status > 0) { |
| 165 |
include_once ADVERTS_PATH . "addons/$mod/$mod.php"; |
| 166 |
} |
| 167 |
if($status == 0.5) { |
| 168 |
add_action("init", "adverts_install_modules", 1000); |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
if( get_option( "adverts_delayed_install" ) == "yes" ) { |
| 173 |
global $wp_rewrite; |
| 174 |
$wp_rewrite->flush_rules(); |
| 175 |
|
| 176 |
delete_option( "adverts_delayed_install" ); |
| 177 |
} |
| 178 |
|
| 179 |
do_action("adverts_core_initiated"); |
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* Frontend Adverts Init Function |
| 184 |
* |
| 185 |
* Registers frontend: scripts, styles and shortcodes |
| 186 |
* |
| 187 |
* @since 0.1 |
| 188 |
* @return void |
| 189 |
*/ |
| 190 |
function adverts_init_frontend() { |
| 191 |
|
| 192 |
wp_register_style( 'adverts-frontend', ADVERTS_URL . '/assets/css/adverts-frontend.css'); |
| 193 |
|
| 194 |
wp_register_script('adverts-frontend', ADVERTS_URL . '/assets/js/adverts-frontend.js'); |
| 195 |
wp_register_script('adverts-frontend-add', ADVERTS_URL . '/assets/js/adverts-frontend-add.js'); |
| 196 |
wp_register_script('responsive-slides', ADVERTS_URL . '/assets/js/responsive-slides.js', array('jquery')); |
| 197 |
|
| 198 |
include_once ADVERTS_PATH . 'includes/functions.php'; |
| 199 |
include_once ADVERTS_PATH . 'includes/gallery.php'; |
| 200 |
include_once ADVERTS_PATH . 'includes/shortcodes.php'; |
| 201 |
|
| 202 |
add_filter('the_content', 'adverts_the_content'); |
| 203 |
add_filter('posts_results', 'adverts_posts_results', 10, 2 ); |
| 204 |
add_filter('template_include', 'adverts_template_include'); |
| 205 |
add_filter('post_thumbnail_html', 'adverts_post_thumbnail_html'); |
| 206 |
add_action('template_redirect', 'adverts_disable_default_archive'); |
| 207 |
|
| 208 |
wp_localize_script( 'adverts-frontend', 'adverts_frontend_lang', array( |
| 209 |
"ajaxurl" => admin_url('admin-ajax.php') |
| 210 |
) ); |
| 211 |
|
| 212 |
if(wp_get_theme()->get_template() == "twentytwelve") { |
| 213 |
add_action("wp_head", "adverts_css_rem_fix"); |
| 214 |
} |
| 215 |
|
| 216 |
} |
| 217 |
|
| 218 |
/** |
| 219 |
* Adverts Admin Init Function |
| 220 |
* |
| 221 |
* Registers admin: ajax actions, scripts, styles and meta boxes when adding new advert. |
| 222 |
* |
| 223 |
* @since 0.1 |
| 224 |
* @return void |
| 225 |
*/ |
| 226 |
function adverts_init_admin() { |
| 227 |
|
| 228 |
include_once ADVERTS_PATH . 'includes/ajax.php'; |
| 229 |
include_once ADVERTS_PATH . 'includes/admin-pages.php'; |
| 230 |
include_once ADVERTS_PATH . 'includes/admin-post-type.php'; |
| 231 |
include_once ADVERTS_PATH . 'includes/class-form.php'; |
| 232 |
include_once ADVERTS_PATH . 'includes/class-html.php'; |
| 233 |
include_once ADVERTS_PATH . 'includes/gallery.php'; |
| 234 |
|
| 235 |
add_action( 'add_meta_boxes', 'adverts_data_box' ); |
| 236 |
add_action( 'add_meta_boxes', 'adverts_box_gallery' ); |
| 237 |
|
| 238 |
wp_register_script('adverts-admin', ADVERTS_URL . '/assets/js/adverts-admin.js', array(), false, true); |
| 239 |
wp_register_style('adverts-admin', ADVERTS_URL . '/assets/css/adverts-admin.css'); |
| 240 |
|
| 241 |
wp_register_script( 'adverts-admin-updates', ADVERTS_URL . '/assets/js/adverts-admin-updates.js', array(), false, true ); |
| 242 |
wp_register_style( 'adverts-admin-updates', ADVERTS_URL . '/assets/css/adverts-admin-updates.css'); |
| 243 |
|
| 244 |
add_filter( 'display_post_states', 'adverts_display_expired_state' ); |
| 245 |
add_action( 'post_submitbox_misc_actions', 'adverts_expiry_meta_box' ); |
| 246 |
|
| 247 |
add_action( 'admin_print_scripts-post-new.php', 'adverts_admin_script', 11 ); |
| 248 |
add_action( 'admin_print_scripts-post.php', 'adverts_admin_script', 11 ); |
| 249 |
add_action( 'admin_print_scripts-edit.php', 'adverts_admin_script', 11 ); |
| 250 |
|
| 251 |
add_action( 'admin_head', 'adverts_admin_head' ); |
| 252 |
add_filter( 'wp_insert_post_data', 'adverts_insert_post_data'); |
| 253 |
add_action( 'save_post', 'adverts_save_post', 10, 2 ); |
| 254 |
add_action( 'save_post', 'adverts_save_post_validator', 10, 2 ); |
| 255 |
|
| 256 |
add_filter( 'post_updated_messages', 'adverts_post_updated_messages'); |
| 257 |
|
| 258 |
// Adverts category meta handlers |
| 259 |
add_action( 'edited_advert_category', 'adverts_save_category', 10, 2); |
| 260 |
add_action( 'advert_category_edit_form_fields', 'adverts_category_form_fields', 10, 2); |
| 261 |
|
| 262 |
// AJAX filters |
| 263 |
add_action('wp_ajax_adverts_author_suggest', 'adverts_author_suggest'); |
| 264 |
add_action('wp_ajax_adverts_gallery_upload', 'adverts_gallery_upload'); |
| 265 |
add_action('wp_ajax_adverts_gallery_update', 'adverts_gallery_update'); |
| 266 |
add_action('wp_ajax_adverts_gallery_delete', 'adverts_gallery_delete'); |
| 267 |
add_action('wp_ajax_adverts_show_contact', 'adverts_show_contact'); |
| 268 |
add_action('wp_ajax_adverts_delete_tmp', 'adverts_delete_tmp'); |
| 269 |
add_action('wp_ajax_adverts_delete', 'adverts_delete'); |
| 270 |
|
| 271 |
add_action('wp_ajax_nopriv_adverts_gallery_upload', 'adverts_gallery_upload'); |
| 272 |
add_action('wp_ajax_nopriv_adverts_gallery_update', 'adverts_gallery_update'); |
| 273 |
add_action('wp_ajax_nopriv_adverts_gallery_delete', 'adverts_gallery_delete'); |
| 274 |
|
| 275 |
add_action('wp_ajax_nopriv_adverts_show_contact', 'adverts_show_contact'); |
| 276 |
add_action('wp_ajax_nopriv_adverts_delete_tmp', 'adverts_delete_tmp'); |
| 277 |
|
| 278 |
add_filter( 'manage_edit-advert_columns', 'adverts_edit_columns' ); |
| 279 |
add_action( 'manage_advert_posts_custom_column', 'adverts_manage_post_columns', 10, 2 ); |
| 280 |
add_filter( 'manage_edit-advert_sortable_columns', 'adverts_admin_sortable_columns' ); |
| 281 |
|
| 282 |
/* Only run our customization on the 'edit.php' page in the admin. */ |
| 283 |
add_action( 'load-edit.php', 'adverts_admin_load' ); |
| 284 |
} |
| 285 |
|
| 286 |
/** |
| 287 |
* Registers Adverts Widgets |
| 288 |
* |
| 289 |
* This function is executed by 'widgets_init' action. |
| 290 |
* |
| 291 |
* @since 0.3 |
| 292 |
* @return void |
| 293 |
*/ |
| 294 |
function adverts_widgets_init() { |
| 295 |
|
| 296 |
include_once ADVERTS_PATH . '/includes/class-widget-categories.php'; |
| 297 |
|
| 298 |
register_widget( "Adverts_Widget_Categories" ); |
| 299 |
} |
| 300 |
|
| 301 |
/** |
| 302 |
* Install Modules |
| 303 |
* |
| 304 |
* Run module installation functions (if any) on modules that were "just" activated. |
| 305 |
* This function is executed in "init" filter with low priority so all the modules |
| 306 |
* are initiated before it is run. |
| 307 |
* |
| 308 |
* @since 0.2 |
| 309 |
* @return void |
| 310 |
*/ |
| 311 |
function adverts_install_modules() { |
| 312 |
$module = adverts_config( 'config.module' ); |
| 313 |
$install = array(); |
| 314 |
|
| 315 |
foreach((array)$module as $mod => $status) { |
| 316 |
if(!is_file(ADVERTS_PATH . "addons/$mod/$mod.php")) { |
| 317 |
continue; |
| 318 |
} |
| 319 |
if($status > 0) { |
| 320 |
include_once ADVERTS_PATH . "addons/$mod/$mod.php"; |
| 321 |
} |
| 322 |
if($status == 0.5) { |
| 323 |
add_action("init", "adverts_install_modules"); |
| 324 |
Adverts_Flash::instance()->add_info( __( "Module activated successfully.", "adverts" ) ); |
| 325 |
|
| 326 |
$module[$mod] = 1; |
| 327 |
adverts_config_set( 'config.module', $module ); |
| 328 |
adverts_config_save( 'config' ); |
| 329 |
|
| 330 |
do_action("adverts_install_module_$mod"); |
| 331 |
|
| 332 |
} |
| 333 |
} |
| 334 |
} |
| 335 |
|
| 336 |
/** |
| 337 |
* Activation Filter |
| 338 |
* |
| 339 |
* This function is run when Adverts is activated. |
| 340 |
* |
| 341 |
* @since 0.1 |
| 342 |
* @return void |
| 343 |
*/ |
| 344 |
function adverts_activate() { |
| 345 |
|
| 346 |
// on activation ALWAYS do this |
| 347 |
global $wp_rewrite; |
| 348 |
|
| 349 |
add_option( "adverts_delayed_install", "yes"); |
| 350 |
|
| 351 |
// on FIRST activation do this. |
| 352 |
if(get_option("adverts_first_run", "1") == "0") { |
| 353 |
return; |
| 354 |
} |
| 355 |
|
| 356 |
// make sure this will not be ran again. |
| 357 |
add_option("adverts_first_run", "0", '', false); |
| 358 |
|
| 359 |
// register post type and taxonomy in order to allow default data insertion. |
| 360 |
register_post_type( 'advert' ); |
| 361 |
register_taxonomy( 'advert_category', 'advert' ); |
| 362 |
|
| 363 |
$hid = wp_insert_post(array( |
| 364 |
'post_type' => 'page', |
| 365 |
'post_status' => 'publish', |
| 366 |
'post_title' => 'Adverts', |
| 367 |
'comment_status' => 'closed', |
| 368 |
'ping_status' => 'closed', |
| 369 |
'post_content' => "[adverts_list]" |
| 370 |
)); |
| 371 |
|
| 372 |
$aid = wp_insert_post(array( |
| 373 |
'post_type' => 'page', |
| 374 |
'post_status' => 'publish', |
| 375 |
'post_title' => 'Add', |
| 376 |
'post_parent' => $hid, |
| 377 |
'comment_status' => 'closed', |
| 378 |
'ping_status' => 'closed', |
| 379 |
'post_content' => "[adverts_add]" |
| 380 |
)); |
| 381 |
|
| 382 |
$mid = wp_insert_post(array( |
| 383 |
'post_type' => 'page', |
| 384 |
'post_status' => 'publish', |
| 385 |
'post_title' => 'Manage', |
| 386 |
'post_parent' => $hid, |
| 387 |
'comment_status' => 'closed', |
| 388 |
'ping_status' => 'closed', |
| 389 |
'post_content' => "[adverts_manage]" |
| 390 |
)); |
| 391 |
|
| 392 |
wp_insert_term( |
| 393 |
'Default', |
| 394 |
'advert_category' |
| 395 |
); |
| 396 |
|
| 397 |
} |
| 398 |
|
| 399 |
// Register activation function |
| 400 |
register_activation_hook( __FILE__, 'adverts_activate' ); |
| 401 |
|
| 402 |
// Run Adverts |
| 403 |
add_action( 'init', 'adverts_init', 5 ); |
| 404 |
add_action( 'widgets_init', 'adverts_widgets_init' ); |
| 405 |
|
| 406 |
if(is_admin() ) { |
| 407 |
// Run Adverts admin only actions |
| 408 |
add_action( 'init', 'adverts_init_admin' ); |
| 409 |
} else { |
| 410 |
// Run Adverts frontend only actions |
| 411 |
add_action( 'init', 'adverts_init_frontend' ); |
| 412 |
} |
| 413 |
|
| 414 |
|