Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Extending BuddyPress with BP_Component Class
Search
Renato Alves
September 27, 2016
Programming
0
180
Extending BuddyPress with BP_Component Class
English version of my talk given at WordCamp Rio de Janeiro in 2016.
Renato Alves
September 27, 2016
Tweet
Share
More Decks by Renato Alves
See All by Renato Alves
Estendendo o BuddyPress com a classe BP_Component
espellcaste
0
120
Criando sua Rede Social com o BuddyPress
espellcaste
2
200
Other Decks in Programming
See All in Programming
testcontainers のススメ
sgash708
1
130
技術的負債と向き合うカイゼン活動を1年続けて分かった "持続可能" なプロダクト開発
yuichiro_serita
0
160
MCP with Cloudflare Workers
yusukebe
2
230
今年一番支援させていただいたのは認証系サービスでした
satoshi256kbyte
1
260
これでLambdaが不要に?!Step FunctionsのJSONata対応について
iwatatomoya
2
3.8k
テストコードのガイドライン 〜作成から運用まで〜
riku929hr
5
1k
선언형 UI에서의 상태관리
l2hyunwoo
0
200
CQRS+ES の力を使って効果を感じる / Feel the effects of using the power of CQRS+ES
seike460
PRO
0
170
Mermaid x AST x 生成AI = コードとドキュメントの完全同期への道
shibuyamizuho
1
300
htmxって知っていますか?次世代のHTML
hiro_ghap1
0
350
Androidアプリのモジュール分割における:x:commonを考える
okuzawats
1
220
歴史と現在から考えるスケーラブルなソフトウェア開発のプラクティス
i10416
0
150
Featured
See All Featured
Large-scale JavaScript Application Architecture
addyosmani
510
110k
A Tale of Four Properties
chriscoyier
157
23k
BBQ
matthewcrist
85
9.4k
Git: the NoSQL Database
bkeepers
PRO
427
64k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
28
2.1k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
666
120k
Java REST API Framework Comparison - PWX 2021
mraible
28
8.3k
Facilitating Awesome Meetings
lara
50
6.1k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
1
110
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
232
17k
Building an army of robots
kneath
302
44k
Build The Right Thing And Hit Your Dates
maggiecrowley
33
2.4k
Transcript
Extending BuddyPress with BP_Component Class WordCamp Rio | September 2016
Hi Rio! I'm Renato Alves Recifense/Pernambucano/Nordestino WordPress/BuddyPress/Freelancer Developer Full-stack developer
at Inglês na Rede Saucal WordPress Engineer bbPress and BuddyPress contributor
Our task: Integrate WooCommerce account page inside the BuddyPress user
profile. For this integration, we are gonna use BuddyPress BP_Component class.
BuddyPress?! What is it? :/ BuddyPress is a powerful community
plugin for WordPress that takes your site beyond the blog. With it, you'll be able to create any social network.
Use Cases Some real world examples of sites using BuddyPress
Inglês na Rede Users: +210 - https://inglesnarede.com.br
Tasty Kitchen Users: +230.000 - http://tastykitchen.com/
Tributário Users: +72.000 - https://tributario.com.br
Why integrate both plugins? Examples of WooCommerce and BuddyPress integrations
Sites with subscriptions and products; Sites where BP components are the focus; Avoid confusing the user.
I introduce BP_Component class
BP_Component Class What is it? Why does it exist? How
is it being used inside BuddyPress? How other plugins are using it? Menus; Component files; Cache groups; Post types/taxonomies registration; Database tables; Metadata tables; Permalinks; Rewrite rules.
BuddyPress Follow Downloads: +43.000
Tributário Users: +72.000
Integration Main Points Pages, endpoints and permalinks; Content; Redirection.
class IRB_WC_Account_Component extends BP_Component { public function __construct() { parent::start(
'bp_wc_account', __( 'Apoio', 'irb' ) ); $this->setup_hooks(); } public function setup_hooks() { add_filter( 'woocommerce_get_endpoint_url', array( $this, 'get_url' ), 10, 4 ); add_filter( 'woocommerce_get_myaccount_page_permalink', array( $this, 'account_url' ), 10, 1 ); add_filter( 'woocommerce_is_account_page', array( $this, 'is_account_page' ), 10, 1 ); add_filter( 'woocommerce_locate_template', array( $this, 'disable_account_nav' ), 10, 2 ); } public function disable_account_nav( $template = '', $template_name = '' ) { if ( 'myaccount/navigation.php' === $template_name && bp_is_current_component( 'minha-conta' ) ) { $template = plugin_dir_path( __FILE__ ) . 'dummy-nav.php'; } return $template; } // Continue.... # Plugin Beginning
public function setup_globals( $args = array() ) { $bp =
buddypress(); $bp->active_components[$this->id] = '1'; // Define slug, if needed if ( ! defined( 'BP_WC_ACCOUNT_SLUG' ) ) { define( 'BP_WC_ACCOUNT_SLUG', 'minha-conta' ); } $args = array( 'slug' => BP_WC_ACCOUNT_SLUG, 'has_directory' => false, ); parent::setup_globals( $args ); $this->irb_wc_link = ''; if ( is_user_logged_in() ) { $this->irb_wc_link = trailingslashit( bp_loggedin_user_domain() . $this->slug ); } } # Plugin globals
public function setup_nav( $main_nav = array(), $sub_nav = array() )
{ ... more code above foreach ( wc_get_account_menu_items() as $key_item => $item ) { // if ( 'dashboard' === $key_item || 'customer-logout' === $key_item ) { // continue; // } // Don't add edit-acount page // if ( bp_is_active( 'settings' ) && 'edit-account' === $key_item ) { // continue; // } ... more code below } # Menus
// WooCommerce uses it to redirect user after a form
entry is submited. public function account_url( $url = '' ) { global $wp; if ( ! bp_is_current_component( 'minha-conta' ) && ! isset( $wp->query_vars['customer-logout'] ) && empty( $this->was_redirected ) ) { return $url; } if ( isset( $wp->query_vars['customer-logout'] ) ) { return bp_get_root_domain(); } else { return trailingslashit( bp_loggedin_user_domain() . $this->slug ); } } # Filter URL
public function set_screen() { if ( bp_is_current_action( 'orders' ) &&
'view-order' === bp_action_variable( 0 ) && is_numeric( bp_action_variable( 1 ) ) ) { // WooCommerce usa a variável global $wp global $wp; unset( $wp->query_vars['orders'] ); $wp->query_vars['view-order'] = (int) bp_action_variable( 1 ); } remove_filter( 'the_title', 'wc_page_endpoint_title' ); add_action( 'bp_template_content', array( $this, 'wc_content' ) ); bp_core_load_template( 'members/single/plugins' ); } # Telling BP which screen to use
public function wc_content() { echo apply_filters( 'the_content', get_post_field( 'post_content', wc_get_page_id(
'myaccount' ) ) ); } # Content pre-WooCommerce 2.6
public function wc_content() { global $wp; $vars = $wp->query_vars['name']; if
( $vars == 'billing' ) { return woocommerce_account_edit_address( $vars ); } else if ( $vars == 'view-order' ) { return woocommerce_account_view_order( $wp->query_vars['page'] ); } else if ( $vars == 'minha-conta' ) { return woocommerce_account_content(); } else { do_action( 'woocommerce_account_' . $vars . '_endpoint'); } } # Content pos-WooCommerce 2.6
public function get_url( $url, $endpoint, $value, $permalink ) { if
( ! bp_is_current_component( 'minha-conta' ) ) { return $url; } $my_account_slugs = array_merge( wc_get_account_menu_items(), array( 'view-order' => true ) ); if ( isset( $my_account_slugs[ $endpoint ] ) ) { $slug = $endpoint; if ( 'view-order' === $endpoint ) { $slug = 'orders/' . $slug; } $url = trailingslashit( bp_loggedin_user_domain() . $this->slug ) . $slug; if ( ! empty( $value ) ) { $url = trailingslashit( $url ) . $value; } } return $url; } # Adjusting URLS
function irb_wc_account_component() { buddypress()->irb_wc_account = new IRB_WC_Acount_Component; } add_action( 'irb_wc_account_ready',
'irb_wc_account_component', 20 ); # Creating a class instance # Loading the component private function setup_actions() { if ( class_exists('WooCommerce') ) { require( $this->plugin_dir . 'woo/bp-wp-component.php' ); } # Adding the component do_action( 'irb_wc_account_ready' ); }
# Orders
# View Order
# My Addresses
# Edit Address
# Account Details
The end! Now go create your social network! =D
[email protected]
ralv.es // inglesnarede.com.br Questions?! Renato Alves