/** * Renders default/core config form. * * The page is rendered in wp-admin / Classifieds / Options / Core * * @since 0.1 * @return void */ function adext_core_page_options() { $page_title = __("Core Options", "adverts"); $button_text = __("Update Options", "adverts"); wp_enqueue_style('adverts-admin'); $flash = Adverts_Flash::instance(); $error = array(); $scheme = Adverts::instance()->get("form_core_config"); $form = new Adverts_Form($scheme); if (isset($_POST) && !empty($_POST)) { $form->bind(stripslashes_deep($_POST)); $valid = $form->validate(); if ($valid) { $data = $form->get_values(); $data["module"] = adverts_config('config.module'); update_option("adverts_config", $data); $flash->add_info(__("Settings updated.", "adverts")); } else { $flash->add_error(__("There are errors in your form.", "adverts")); } } else { $form->bind(adverts_config("config.ALL")); } include ADVERTS_PATH . 'addons/core/admin/options.php'; }
/** * wp-admin / Classifieds / Options panel logic. * * This functions is being executed when wp-admin / Classifieds / Options panel * is being loaded. It either generates list of available modules or loads module, * depending on $_REQUEST 'disabled', 'enabled' and 'module' params. * * @global $_REQUEST * * @uses adverts_config() * @uses adverts_config_set() * @uses adverts_config_save() * @uses adverts_request() * * @since 0.1 * @return void */ function adverts_admin_page_extensions() { // We are on one of Adverts admin pages, load default Adverts admin styles. wp_enqueue_style('adverts-admin'); // Load modules config $module = adverts_config('module'); $module_groups = array(array("title" => __("Modules", "adverts"), "modules" => array("core" => array("title" => __("Core", "adverts"), "text" => __("Here you can configure most basic Adverts options, applicable on any site.", "adverts"), "type" => "static", "plugin" => null), "payments" => array("title" => __("Payments", "adverts"), "text" => __("Charge users for posting classified ads on your site.", "adverts"), "type" => "", "plugin" => null), "wc-payments" => array("title" => __("WooCommerce Payments", "adverts"), "text" => __("Use WooCommerce to charge users for posting classifieds.", "adverts"), "type" => "", "plugin" => "wpadverts-wc/wpadverts-wc.php", "purchase_url" => "http://wpadverts.com/extensions/woocommerce-integration/"))), array("title" => __("Features", "adverts"), "modules" => array("featured" => array("title" => __("Featured Ads", "adverts"), "text" => __("Allow users to post Ads displayed always at the top of the list.", "adverts"), "type" => "", "plugin" => null), "wpadverts-mal" => array("title" => __("Maps And Locations", "adverts"), "text" => __("Locations Taxonomy, Google Maps integration and more.", "adverts"), "type" => "", "plugin" => "wpadverts-mal/wpadverts-mal.php", "purchase_url" => "http://wpadverts.com/extensions/maps-and-locations/"))), array("title" => __("Payment Gateways", "adverts"), "modules" => array("bank-transfer" => array("title" => __("Bank Transfer", "adverts"), "text" => __("Allow users to pay for ad posting using direct bank transfer or cash payments.", "adverts"), "type" => "", "plugin" => null), "paypal-standard" => array("title" => __("PayPal Standard", "adverts"), "text" => __("PayPal Payments Standard, allow users to pay for ads via PayPal.", "adverts"), "type" => "", "plugin" => "wpadverts-paypal-standard/wpadverts-paypal-standard.php", "purchase_url" => "http://wpadverts.com/extensions/paypal-payments-standard/")))); if (adverts_request('enable')) { // User is trying to enable module $enable = adverts_request('enable'); $module[$enable] = 0.5; // Save config adverts_config_set('config.module', $module); adverts_config_save('config'); wp_redirect(add_query_arg(array('enable' => false, 'noheader' => false, 'module' => $enable))); exit; } if (adverts_request('disable')) { // User is trying to disable module $disable = adverts_request('disable'); if (isset($module[$disable])) { unset($module[$disable]); } // Save config adverts_config_set('config.module', $module); adverts_config_save('config'); wp_redirect(remove_query_arg(array('disable', 'noheader'))); exit; } if (adverts_request('module')) { // Load module (based on $_GET[module]) panel $name = adverts_request('module'); $module_current = null; $module_key = null; foreach ($module_groups as $group) { foreach ($group["modules"] as $key => $tmp) { if ($key == $name) { $module_current = $tmp; $module_key = $key; break 2; } } } if ($module_current === null) { esc_html_e(sprintf(__("Module [%s] does not exist.", "adverts"), $name)); return; } if ($module_current["plugin"]) { include_once dirname(ADVERTS_PATH) . '/' . dirname($module_current["plugin"]) . '/includes/admin-module-options.php'; } else { include_once ADVERTS_PATH . 'addons/' . $module_key . '/includes/admin-module-options.php'; } } else { // Display modules list template include ADVERTS_PATH . 'admin/options.php'; } }
/** * Replaces taxonomy URL with custom URL. * * Replaces URLs for taxonomies registered in alt_term_links_config(), the new * URL points to default page with [adverts_list] shortcode * * @uses alt_term_links_config() * @since 1.0 * @access public * @param string $termlink Default link to taxonomy page * @param WP_Term $term Term for which the URL is generated * @param string $taxonomy Taxonomy name * @return string Custom link to taxonomy page */ function alt_term_links_urls($termlink, $term, $taxonomy = null) { $apply = alt_term_links_config(); if (!array_key_exists($taxonomy, $apply)) { return $termlink; } $tax = $apply[$taxonomy]; return add_query_arg($tax["param"], $term->slug, get_permalink(adverts_config("ads_list_id"))); }
/** * Renders Bank Transfer Payment Form * * If user will select Bank Transfer as a payment method, this function will render * payment instructions. * * It is executed in third step in [adverts_add] shortcode. * * @param array $data Payment data * @return array */ function adext_bank_transfer_render($data) { $price = $data["price"]; $payment_id = $data["payment_id"]; $atts = array("class" => "adverts-success"); $repl = array("{total}" => adverts_price($price), "{order_number}" => str_pad($payment_id, 6, "0", STR_PAD_LEFT)); $html = str_replace(array_keys($repl), array_values($repl), adverts_config("bank_transfer.custom_text")); $html = apply_filters("adext_bank_transfer_custom_text", $html, $data); return array("result" => 1, "html" => Adverts_Html::build("div", $atts, wpautop($html)), "execute" => null, "execute_id" => ""); }
/** * Removes empty $_GET params from current URL. * * The function checks if current URL has empty ('query or 'location') params, * if it has then empty params are removed from URL and user is redirected to * page with this params removed. * * @uses is_page() Checks if user is on main Ads list. * @uses remove_query_arg() Modifies current URL. * @uses wp_redirect() Redirects user to page with empty params removed. * @return void */ function cleanup_args() { if (!is_page(adverts_config('config.ads_list_id'))) { return; } $args = array('query', 'location'); $redirect = false; foreach ($args as $arg) { if (isset($_GET[$arg]) && empty($_GET[$arg])) { $redirect = remove_query_arg($arg, $redirect); } } if ($redirect) { wp_redirect($redirect); exit; } }
/** * Install Modules * * Run module installation functions (if any) on modules that were "just" activated. * This function is executed in "init" filter with low priority so all the modules * are initiated before it is run. * * @since 0.2 * @return void */ function adverts_install_modules() { $module = adverts_config('config.module'); $install = array(); foreach ((array) $module as $mod => $status) { if (!is_file(ADVERTS_PATH . "addons/{$mod}/{$mod}.php")) { continue; } if ($status > 0) { include_once ADVERTS_PATH . "addons/{$mod}/{$mod}.php"; } if ($status == 0.5) { add_action("init", "adverts_install_modules"); Adverts_Flash::instance()->add_info(__("Module activated successfully.", "adverts")); $module[$mod] = 1; adverts_config_set('config.module', $module); adverts_config_save('config'); do_action("adverts_install_module_{$mod}"); } } }
/** * Adds Expiry date to 'Publish' meta box * * @see post_submitbox_misc_actions action * * @param WP_Post $post * @global string $pagenow */ function adverts_expiry_meta_box() { global $post, $pagenow; // Do this for adverts only. if ($post->post_type != 'advert') { return; } $datef = __('M j, Y @ G:i'); $meta = get_post_meta($post->ID, '_expiration_date', true); if ($meta != '') { // Set expiration date based on _expiration_date meta $expiry = $meta; $date = date_i18n($datef, $expiry); $touch_time = date("Y-m-d H:i:s", $expiry); $never_expires = false; } else { if ($pagenow != "post-new.php") { // _expiration_date meta not in DB, set expiration to 'never' $expiry = strtotime(current_time('mysql') . " +30 DAYS"); $date = __("Never Expires", "adverts"); $touch_time = date("Y-m-d H:i:s", $expiry); $never_expires = true; } else { // User is creating a new Advert, the expiration date is set to today+30 days. $duration = intval(adverts_config("config.visibility")); $expiry = strtotime(current_time('mysql') . " +{$duration} DAYS"); $date = date_i18n($datef, $expiry); $touch_time = date("Y-m-d H:i:s", $expiry); $never_expires = false; } } // Check if date is in the past or in the future and set correct label based on this. if (strtotime(date_i18n("Y-m-d H:i:s")) > $expiry) { $stamp = __("Expired: <b>%s</b>", "adverts"); } else { $stamp = __("Expires: <b>%s</b>", "adverts"); } /* @todo: in a future select who can publish */ $can_publish = true; // render expiration date section if ($can_publish) { ?> <div class="misc-pub-section curtime misc-pub-curtime"> <span id="timestamp_expire"> <?php printf($stamp, $date); ?> </span> <a href="#edit_timestamp_expire" class="edit-timestamp hide-if-no-js"><span aria-hidden="true"><?php _e('Edit'); ?> </span> <span class="screen-reader-text"><?php _e('Edit date and time'); ?> </span></a> <div id="timestamp-expire-div" class="hide-if-js"><?php adverts_touch_time($touch_time, 2, $never_expires); ?> </div> </div><?php // /misc-pub-section ?> <?php } }
/** * Replaces user name with link to all user ads. * * If user who posted an Ad is registered WP user, then the "By John Doe" text * on Ad details page is replaced with link to all user Ads. * * This change is applied using adverts_tpl_single_posted_by filter. * * @see adverts_tpl_single_posted_by filter in wpadverts/templates/single.php * * @param string $pbase Default pagination base * @since 1.0 * @return string Updated pagination base */ function ads_by_author_tpl_single_posted_by($name, $post_id) { $post = get_post($post_id); $person = get_post_meta($post_id, 'adverts_person', true); if ($post->post_author) { include_once ADVERTS_PATH . "/includes/class-html.php"; $link = get_permalink(adverts_config('ads_list_id')); $glue = stripos($link, '?') ? '&' : '?'; $person = new Adverts_Html("a", array("href" => $link . $glue . "posted_by=" . $post->post_author), $person); } return sprintf(__("by <strong>%s</strong>", "adverts"), $person); }
<ul class="adverts-tabs adverts-payment-data" data-page-id="<?php esc_attr_e(get_the_ID()); ?> " data-listing-id="<?php esc_attr_e($listing->ID); ?> " data-object-id="<?php esc_attr_e($post->ID); ?> "> <?php foreach ($gateways as $g_name => $gateway) { ?> <li class="adverts-tab-link <?php if ($g_name == adverts_config("payments.default_gateway")) { ?> current<?php } ?> " data-tab="<?php esc_attr_e($g_name); ?> "><?php esc_html_e($gateway["title"]); ?> </li> <?php } ?> </ul>
/** * Disables Adverts archive page. * * We do not want to disaply adverts archive page because it is not possible * to control displayed conent there, instead we redirect users to default ads list page * * @access public * @since 1.0 * @return void */ function adverts_disable_default_archive() { if (is_post_type_archive("advert")) { wp_redirect(get_permalink(adverts_config("ads_list_id"))); exit; } }
/** * AJAX License validation * * This function is executed from /wp-admin/admin-ajax.php it handles * license key validation. * * @see wp_ajax_adverts_license_validate_$slug action * @see self::connect() * * @access public * @since 1.0 * @return void */ public function validate_license() { $license = trim(adverts_request("license")); $update = false; $response = new stdClass(); $response->status = 200; $response->error = ""; $response->message = ""; if (!empty($license)) { $request = $this->remote("license", array("license" => $license)); if ($request === false) { $response->status = 400; $response->message = __("Cannot connect to remote server. Please try again later.", "adverts"); } elseif ($request->result == 1) { $update = true; } else { $response->status = $request->result; $response->message = $request->message; } } else { $license = null; $update = true; } if ($update) { $config = adverts_config('config.license'); $config[$this->slug] = $license; adverts_config_set('config.license', $config); adverts_config_save('config'); } $transient = get_site_transient("update_plugins"); if ($update && $license === null && isset($transient->response[$this->plugin])) { unset($transient->checked[$this->plugin]); unset($transient->response[$this->plugin]); set_site_transient("update_plugins", $transient); } echo json_encode($response); exit; }
/** * Generates HTML for [adverts_add] shortcode * * @param array $atts Shortcode attributes * @since 0.1 * @return string Fully formatted HTML for "post ad" form. */ function shortcode_adverts_add($atts) { wp_enqueue_style('adverts-frontend'); wp_enqueue_style('adverts-icons'); wp_enqueue_style('adverts-icons-animate'); wp_enqueue_script('adverts-frontend'); wp_enqueue_script('adverts-auto-numeric'); extract(shortcode_atts(array('name' => 'default', 'moderate' => false), $atts)); include_once ADVERTS_PATH . 'includes/class-html.php'; include_once ADVERTS_PATH . 'includes/class-form.php'; $form = new Adverts_Form(Adverts::instance()->get("form")); $valid = null; $error = array(); $info = array(); $bind = array(); $content = ""; $adverts_flash = array("error" => array(), "info" => array()); $action = apply_filters('adverts_action', adverts_request("_adverts_action", ""), __FUNCTION__); $post_id = adverts_request("_post_id", null); $post_id = $post_id > 0 ? $post_id : null; // $post_id hijack attempt protection here! if ($post_id > 0 && get_post($post_id)->post_author == get_current_user_id()) { // if post was already saved in DB (for example for preview) then load it. $post = get_post($post_id); // bind data by field name foreach ($form->get_fields() as $f) { $bind[$f["name"]] = get_post_meta($post_id, $f["name"], true); } $bind["post_title"] = $post->post_title; $bind["post_content"] = $post->post_content; $bind["advert_category"] = array(); $terms = get_the_terms($post_id, 'advert_category'); if (is_array($terms)) { foreach ($terms as $term) { $bind["advert_category"][] = $term->term_id; } } } elseif (is_user_logged_in()) { $bind["adverts_person"] = wp_get_current_user()->display_name; $bind["adverts_email"] = wp_get_current_user()->user_email; } if ($action == "") { // show post ad form page wp_enqueue_style('adverts-frontend-add'); $bind["_post_id"] = $post_id; $bind["_adverts_action"] = "preview"; $form->bind($bind); // adverts/templates/add.php ob_start(); include apply_filters("adverts_template_load", ADVERTS_PATH . 'templates/add.php'); $content = ob_get_clean(); } elseif ($action == "preview") { // show preview page wp_enqueue_style('adverts-frontend-add'); $form->bind((array) stripslashes_deep($_POST)); $valid = $form->validate(); $adverts_flash = array("error" => $error, "info" => $info); // Allow to preview only if data in the form is valid. if ($valid) { $init = array("post" => array("ID" => $post_id, "post_name" => sanitize_title($form->get_value("post_title")), "post_type" => "advert", "post_author" => get_current_user_id(), "post_date" => current_time('mysql'), "post_date_gmt" => current_time('mysql', 1), "post_status" => adverts_tmp_post_status(), "guid" => ""), "meta" => array()); if (adverts_config("config.visibility") > 0) { $init["meta"]["_expiration_date"] = array("value" => strtotime(current_time('mysql') . " +" . adverts_config("config.visibility") . " DAYS"), "field" => array("type" => "adverts_field_hidden")); } // Save post as temporary in DB $post_id = Adverts_Post::save($form, $post_id, $init); if (is_wp_error($post_id)) { $error[] = $post_id->get_error_message(); $valid = false; } $adverts_flash = array("error" => $error, "info" => $info); // adverts/templates/add-preview.php ob_start(); include apply_filters("adverts_template_load", ADVERTS_PATH . 'templates/add-preview.php'); $content = ob_get_clean(); } else { $error[] = __("There are errors in your form. Please correct them before proceeding.", "adverts"); $adverts_flash = array("error" => $error, "info" => $info); // adverts/templates/add.php ob_start(); include apply_filters("adverts_template_load", ADVERTS_PATH . 'templates/add.php'); $content = ob_get_clean(); } // endif $valid } elseif ($action == "save") { // Save form in the database $post_id = wp_update_post(array("ID" => $post_id, "post_status" => $moderate == "1" ? 'pending' : 'publish')); $info[] = __("Thank you for submitting your ad!", "adverts"); $adverts_flash = array("error" => $error, "info" => $info); if (!is_user_logged_in() && get_post_meta($post_id, "_adverts_account", true) == 1) { adverts_create_user_from_post_id($post_id, true); } // adverts/templates/add-save.php ob_start(); include apply_filters("adverts_template_load", ADVERTS_PATH . 'templates/add-save.php'); $content = ob_get_clean(); } return apply_filters("adverts_action_{$action}", $content, $form); }
/** * Binds default payment_listing_type value * * @see adext_payments_form_load() Function which adds this function to filters list * @uses adverts_form_bind Filter which exexutes this function * * @since 1.0 * @access public * @param Adverts_Form $form * @return Adverts_Form */ function adext_payments_form_bind(Adverts_Form $form) { if (!$form->get_value("payments_listing_type")) { $form->set_value("payments_listing_type", adverts_config('payments.default_pricing')); } return $form; }
* @uses Adverts * @uses adverts_config * * @package Adverts * @copyright Copyright (c) 2015, Grzegorz Winiarski * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License * @since 0.1 */ // Exit if accessed directly if (!defined('ABSPATH')) { exit; } // Set default "Post Ad" form structure and save it in Adverts Singleton Adverts::instance()->set("form", array("name" => "advert", "action" => "", "field" => array(array("name" => "_post_id", "type" => "adverts_field_hidden", "order" => 10, "label" => ""), array("name" => "_adverts_action", "type" => "adverts_field_hidden", "order" => 10, "label" => ""), array("name" => "_contact_information", "type" => "adverts_field_header", "order" => 10, "label" => __('Contact Information', 'adverts')), array("name" => "_adverts_account", "type" => "adverts_field_account", "order" => 10, "label" => __("Account", "adverts")), array("name" => "adverts_person", "type" => "adverts_field_text", "order" => 10, "label" => __("Contact Person", "adverts"), "is_required" => true, "validator" => array(array("name" => "is_required"))), array("name" => "adverts_email", "type" => "adverts_field_text", "order" => 10, "label" => __("Email", "adverts"), "is_required" => true, "validator" => array(array("name" => "is_required"), array("name" => "is_email"))), array("name" => "adverts_phone", "type" => "adverts_field_text", "order" => 10, "label" => __("Phone Number", "adverts"), "validator" => array(array("name" => "string_length", "params" => array("min" => 5)))), array("name" => "_item_information", "type" => "adverts_field_header", "order" => 20, "label" => __('Item Information', 'adverts')), array("name" => "post_title", "type" => "adverts_field_text", "order" => 20, "label" => __("Title", "adverts"), "validator" => array(array("name" => "is_required"))), array("name" => "advert_category", "type" => "adverts_field_select", "order" => 20, "label" => __("Category", "adverts"), "max_choices" => 10, "options" => array(), "options_callback" => "adverts_taxonomies"), array("name" => "gallery", "type" => "adverts_field_gallery", "order" => 20, "label" => __("Gallery", "adverts")), array("name" => "post_content", "type" => "adverts_field_textarea", "order" => 20, "label" => __("Description", "adverts"), "validator" => array(array("name" => "is_required")), "mode" => "tinymce-mini"), array("name" => "adverts_price", "type" => "adverts_field_text", "order" => 20, "label" => __("Price", "adverts"), "description" => "", "attr" => array("key" => "value"), "filter" => array(array("name" => "money"))), array("name" => "adverts_location", "type" => "adverts_field_text", "order" => 20, "label" => __("Location", "adverts"))))); // Set default currency and save it in Adverts Singleton Adverts::instance()->set("currency", array('code' => adverts_config("config.currency_code"), 'sign' => adverts_currency_list(adverts_config("config.currency_code"), 'sign'), 'sign_type' => adverts_config("config.currency_sign_type"), 'decimals' => adverts_config("config.currency_decimals"), 'char_decimal' => adverts_config("config.currency_char_decimal"), 'char_thousand' => adverts_config("config.currency_char_thousand"))); /** REGISTER FORM FIELDS */ // Register <span> input /** @see adverts_field_label() */ adverts_form_add_field("adverts_field_label", array("renderer" => "adverts_field_label", "callback_save" => null, "callback_bind" => null)); // Register <input type="hidden" /> input /** @see adverts_field_hidden() */ adverts_form_add_field("adverts_field_hidden", array("renderer" => "adverts_field_hidden", "callback_save" => "adverts_save_single", "callback_bind" => "adverts_bind_single")); // Register <input type="text" /> input /** @see adverts_field_text() */ adverts_form_add_field("adverts_field_text", array("renderer" => "adverts_field_text", "callback_save" => "adverts_save_single", "callback_bind" => "adverts_bind_single")); // Register <textarea></textarea> input /** @see adverts_field_textarea() */ adverts_form_add_field("adverts_field_textarea", array("renderer" => "adverts_field_textarea", "callback_save" => "adverts_save_single", "callback_bind" => "adverts_bind_single")); // Register <select>...</select> input /** @see adverts_field_select() */