Giter Club home page Giter Club logo

Comments (7)

dinhtungdu avatar dinhtungdu commented on May 15, 2024 3

Am I correct to use that Class? And is the WP_Site in the constructor the target site? Looking at the coding it does looks like it right?

@jmslbam Yes, you're right, you can use that class to distribute posts to sites in the network. Take a look at the following function for more inspiration on how to distribute posts programmatically.

function ajax_push() {
if ( ! check_ajax_referer( 'dt-push', 'nonce', false ) ) {
wp_send_json_error( new \WP_Error( 'invalid-referal', __( 'Invalid Ajax referer.', 'distributor' ) ) );
exit;
}
if ( empty( $_POST['postId'] ) ) {
wp_send_json_error( new \WP_Error( 'no-post-id', __( 'No post ID provided.', 'distributor' ) ) );
exit;
}
if ( empty( $_POST['connections'] ) ) {
wp_send_json_error( new \WP_Error( 'no-connection', __( 'No connection provided.', 'distributor' ) ) );
exit;
}
$connection_map = get_post_meta( intval( $_POST['postId'] ), 'dt_connection_map', true );
if ( empty( $connection_map ) ) {
$connection_map = array();
}
if ( empty( $connection_map['external'] ) ) {
$connection_map['external'] = array();
}
if ( empty( $connection_map['internal'] ) ) {
$connection_map['internal'] = array();
}
$external_push_results = array();
$internal_push_results = array();
foreach ( $_POST['connections'] as $connection ) {
if ( 'external' === $connection['type'] ) {
$external_connection_type = get_post_meta( $connection['id'], 'dt_external_connection_type', true );
$external_connection_url = get_post_meta( $connection['id'], 'dt_external_connection_url', true );
$external_connection_auth = get_post_meta( $connection['id'], 'dt_external_connection_auth', true );
if ( empty( $external_connection_auth ) ) {
$external_connection_auth = array();
}
if ( ! empty( $external_connection_type ) && ! empty( $external_connection_url ) ) {
$external_connection_class = \Distributor\Connections::factory()->get_registered()[ $external_connection_type ];
$auth_handler = new $external_connection_class::$auth_handler_class( $external_connection_auth );
$external_connection = new $external_connection_class( get_the_title( $connection['id'] ), $external_connection_url, $connection['id'], $auth_handler );
$push_args = array();
if ( ! empty( $connection_map['external'][ (int) $connection['id'] ] ) && ! empty( $connection_map['external'][ (int) $connection['id'] ]['post_id'] ) ) {
$push_args['remote_post_id'] = (int) $connection_map['external'][ (int) $connection['id'] ]['post_id'];
}
if ( ! empty( $_POST['postStatus'] ) ) {
$push_args['post_status'] = $_POST['postStatus'];
}
$remote_post = $external_connection->push( intval( $_POST['postId'] ), $push_args );
/**
* Record the external connection id's remote post id for this local post
*/
if ( ! is_wp_error( $remote_post ) ) {
$connection_map['external'][ (int) $connection['id'] ] = array(
'post_id' => (int) $remote_post['id'],
'time' => time(),
);
$external_push_results[ (int) $connection['id'] ] = array(
'post_id' => (int) $remote_post['id'],
'date' => gmdate( 'F j, Y g:i a' ),
'status' => 'success',
'url' => sprintf(
'%1$s/?p=%2$d',
get_site_url_from_rest_url( $external_connection_url ),
(int) $remote_post['id']
),
'errors' => empty( $remote_post['push-errors'] ) ? array() : $remote_post['push-errors'],
);
$external_connection->log_sync( array( (int) $remote_post['id'] => $_POST['postId'] ) );
} else {
$external_push_results[ (int) $connection['id'] ] = array(
'date' => gmdate( 'F j, Y g:i a' ),
'status' => 'fail',
'errors' => array( $remote_post->get_error_message() ),
);
}
}
} else {
$internal_connection = new \Distributor\InternalConnections\NetworkSiteConnection( get_site( $connection['id'] ) );
$push_args = array();
if ( ! empty( $connection_map['internal'][ (int) $connection['id'] ] ) && ! empty( $connection_map['internal'][ (int) $connection['id'] ]['post_id'] ) ) {
$push_args['remote_post_id'] = (int) $connection_map['internal'][ (int) $connection['id'] ]['post_id'];
}
if ( ! empty( $_POST['postStatus'] ) ) {
$push_args['post_status'] = esc_attr( $_POST['postStatus'] );
}
$remote_post = $internal_connection->push( intval( $_POST['postId'] ), $push_args );
/**
* Record the internal connection id's remote post id for this local post
*/
if ( ! is_wp_error( $remote_post ) ) {
$origin_site = get_current_blog_id();
switch_to_blog( intval( $connection['id'] ) );
$remote_url = get_permalink( $remote_post['id'] );
$internal_connection->log_sync( array( $_POST['postId'] => $remote_post['id'] ), $origin_site );
restore_current_blog();
$connection_map['internal'][ (int) $connection['id'] ] = array(
'post_id' => (int) $remote_post['id'],
'time' => time(),
);
$internal_push_results[ (int) $connection['id'] ] = array(
'post_id' => (int) $remote_post['id'],
'url' => esc_url_raw( $remote_url ),
'date' => gmdate( 'F j, Y g:i a' ),
'status' => 'success',
'errors' => empty( $remote_post['push-errors'] ) ? array() : $remote_post['push-errors'],
);
} else {
$internal_push_results[ (int) $connection['id'] ] = array(
'errors' => array( $remote_post->get_error_message() ),
'date' => gmdate( 'F j, Y g:i a' ),
'status' => 'fail',
);
}
}
}
update_post_meta( intval( $_POST['postId'] ), 'dt_connection_map', $connection_map );
wp_send_json_success(
array(
'results' => array(
'internal' => $internal_push_results,
'external' => $external_push_results,
),
)
);
exit;
}

from distributor.

jeffpaul avatar jeffpaul commented on May 15, 2024

@dleeward thanks for the input, interesting idea. What's the use case you're looking to solve for with this idea?

from distributor.

jeffpaul avatar jeffpaul commented on May 15, 2024

@dleeward whats the problem you're trying to solve with this cron-based solution?

from distributor.

jeffpaul avatar jeffpaul commented on May 15, 2024

@dleeward can you please provide some more insight into the problem you're trying to solve here?

from distributor.

jeffpaul avatar jeffpaul commented on May 15, 2024

I'm closing this issue due to inactivity, but please let me know if you're still having problems so I can try to help... thanks!

from distributor.

jmslbam avatar jmslbam commented on May 15, 2024

Hi @jeffpaul,

Before creating a new ticket, is it ok to jump in here because I have the same request which I need for my current project.
I'm importing events which need to be distributed to a subsite based on a taxonomy term. Noting more, nothing less.

As this is my first run with Distributer, I would like to double check my current view of the plugin / steps I will take.

  1. I map my subsite to a specific term.
  2. Loop over all posts with Term X.
  3. Push these posts to the subsite using class NetworkSiteConnection extends Connection

Am I correct to use that Class? And is the WP_Site in the constructor the target site? Looking at the coding it does looks like it right? https://github.com/10up/distributor/blob/develop/includes/classes/InternalConnections/NetworkSiteConnection.php#L53

And then I'm not taking in account the whole configuration / setting up the network and allowing site A to push to site B. Defining that in code, would be great and maybe it can already be done, but I think that is out-of-scope of this issue.

Kinds regards,

Jaime Martinez

from distributor.

jmslbam avatar jmslbam commented on May 15, 2024

Thank you @dinhtungdu for the feedback, will proceed with those example. If I find some optimalisations to decouple some code I will drop it here! Will keep you posted!

from distributor.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.