';
}
public function description() {
return __( 'Add each entry as a page in a Notion database.', 'fieldwren' );
}
public function help() {
return '- ' . esc_html__( 'notion.so/profile/integrations → New integration (internal) → copy the Internal Integration Secret.', 'fieldwren' ) . '
- ' . esc_html__( 'Open the database in Notion → ••• → Connections → add your integration, otherwise it is not listed.', 'fieldwren' ) . '
';
}
public function credential_fields() {
return array(
'token' => array( 'label' => __( 'Internal integration secret', 'fieldwren' ), 'type' => 'password', 'required' => true, 'placeholder' => 'ntn_… / secret_…' ),
);
}
private function api( $method, $path, $body = null, $token = null ) {
$args = array(
'headers' => array(
'Authorization' => 'Bearer ' . ( null === $token ? $this->cred( 'token' ) : $token ),
'Notion-Version' => self::VERSION,
),
);
if ( null !== $body ) {
$args['body'] = $body;
}
return self::body( $this->http( $method, 'https://api.notion.com/v1' . $path, $args ) );
}
public function verify( array $creds ) {
$b = $this->api( 'GET', '/users/me', null, (string) $creds['token'] );
return is_wp_error( $b ) ? $b : (string) ( $b['name'] ?? ( $b['bot']['workspace_name'] ?? '' ) );
}
/** @return array|\WP_Error Database properties: name => type. */
private function schema( $db ) {
$key = 'fwren_notion_' . md5( $db . $this->cred( 'token' ) );
$hit = get_transient( $key );
if ( is_array( $hit ) ) {
return $hit;
}
$b = $this->api( 'GET', '/databases/' . rawurlencode( $db ) );
if ( is_wp_error( $b ) ) {
return $b;
}
$out = array();
foreach ( (array) ( $b['properties'] ?? array() ) as $name => $p ) {
$out[ (string) $name ] = (string) ( $p['type'] ?? '' );
}
set_transient( $key, $out, 10 * MINUTE_IN_SECONDS );
return $out;
}
public function options( $source, array $settings ) {
if ( 'databases' === $source ) {
$out = array();
$cursor = null;
for ( $i = 0; $i < 5; $i++ ) {
$q = array( 'filter' => array( 'property' => 'object', 'value' => 'database' ), 'page_size' => 100 );
if ( $cursor ) {
$q['start_cursor'] = $cursor;
}
$b = $this->api( 'POST', '/search', $q );
if ( is_wp_error( $b ) ) {
return $b;
}
foreach ( (array) ( $b['results'] ?? array() ) as $d ) {
$t = '';
foreach ( (array) ( $d['title'] ?? array() ) as $part ) {
$t .= (string) ( $part['plain_text'] ?? '' );
}
$out[ (string) $d['id'] ] = '' !== $t ? $t : (string) $d['id'];
}
$cursor = ! empty( $b['has_more'] ) ? (string) ( $b['next_cursor'] ?? '' ) : '';
if ( ! $cursor ) {
break;
}
}
return $out;
}
if ( 'properties' === $source ) {
$db = (string) ( $settings['database'] ?? '' );
delete_transient( 'fwren_notion_' . md5( $db . $this->cred( 'token' ) ) );
$sch = $this->schema( $db );
if ( is_wp_error( $sch ) ) {
return $sch;
}
$ok = array( 'title', 'rich_text', 'email', 'phone_number', 'url', 'number', 'checkbox', 'select', 'multi_select', 'status', 'date' );
$out = array();
foreach ( $sch as $name => $type ) {
if ( ! in_array( $type, $ok, true ) ) {
continue;
}
$auto = 'email' === $type ? 'email' : ( 'phone_number' === $type ? 'phone' : ( 'title' === $type ? 'name' : ( 'url' === $type ? 'url' : '' ) ) );
$out[] = array( 'key' => $name, 'label' => $name . ' [' . $type . ']', 'required' => 'title' === $type, 'auto' => $auto );
}
return $out;
}
return parent::options( $source, $settings );
}
public function feed_fields( $form_id ) {
return array(
array( 'key' => 'database', 'type' => 'select', 'source' => 'databases', 'label' => __( 'Database', 'fieldwren' ), 'required' => true, 'desc' => __( 'Only databases shared with the integration are listed.', 'fieldwren' ) ),
array( 'key' => 'map', 'type' => 'map', 'source' => 'properties', 'depends' => array( 'database' ), 'label' => __( 'Properties', 'fieldwren' ), 'desc' => __( 'Multi-select takes a comma list; select and status need an existing option name; dates any readable date.', 'fieldwren' ) ),
array( 'key' => 'content', 'type' => 'toggle', 'label' => __( 'Page content', 'fieldwren' ), 'text' => __( 'Add all submitted data to the page body', 'fieldwren' ), 'default' => true ),
);
}
/** @return array Rich text array (2000 characters per item). */
private static function rt( $s ) {
$out = array();
foreach ( (array) preg_split( '/(.{1,1900})/us', (string) $s, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY ) as $chunk ) {
$out[] = array( 'type' => 'text', 'text' => array( 'content' => $chunk ) );
}
return array_slice( $out, 0, 100 );
}
/** @return array|null Notion property value of a type. */
private function prop( $type, $v ) {
switch ( $type ) {
case 'title':
return array( 'title' => self::rt( $v ) );
case 'rich_text':
return array( 'rich_text' => self::rt( $v ) );
case 'email':
return is_email( $v ) ? array( 'email' => $v ) : null;
case 'phone_number':
return array( 'phone_number' => $v );
case 'url':
return array( 'url' => esc_url_raw( $v ) );
case 'number':
return is_numeric( str_replace( ',', '.', $v ) ) ? array( 'number' => (float) str_replace( ',', '.', $v ) ) : null;
case 'checkbox':
return array( 'checkbox' => $this->truthy( $v ) );
case 'select':
return array( 'select' => array( 'name' => self::cut( str_replace( ',', ' ', $v ), 100 ) ) );
case 'status':
return array( 'status' => array( 'name' => $v ) );
case 'multi_select':
return array(
'multi_select' => array_map(
static function ( $n ) {
return array( 'name' => self::cut( $n, 100 ) );
},
array_values( array_filter( array_map( 'trim', explode( ',', $v ) ), 'strlen' ) )
),
);
case 'date':
$t = strtotime( $v );
return $t ? array( 'date' => array( 'start' => gmdate( preg_match( '/\d:\d/', $v ) ? 'c' : 'Y-m-d', $t ) ) ) : null;
}
return null;
}
public function process( array $feed, array $entry, array $values ) {
$s = $feed['settings'];
$db = (string) ( $s['database'] ?? '' );
if ( ! $db ) {
return new \WP_Error( 'fwren_notion', __( 'Choose a database.', 'fieldwren' ) );
}
$sch = $this->schema( $db );
if ( is_wp_error( $sch ) ) {
return $sch;
}
$props = array();
foreach ( $this->mapped( 'map' ) as $name => $v ) {
if ( isset( $sch[ $name ] ) ) {
$p = $this->prop( $sch[ $name ], $v );
if ( $p ) {
$props[ $name ] = $p;
}
}
}
$title = array_search( 'title', $sch, true );
if ( false !== $title && ! isset( $props[ $title ] ) ) {
$props[ $title ] = array( 'title' => self::rt( $this->default_title() ) );
}
$body = array( 'parent' => array( 'database_id' => $db ), 'properties' => (object) $props );
if ( ! empty( $s['content'] ) ) {
$blocks = array();
foreach ( array_slice( $this->data_rows(), 0, 90 ) as $r ) {
$blocks[] = array(
'object' => 'block',
'type' => 'paragraph',
'paragraph' => array( 'rich_text' => array_merge( array( array( 'type' => 'text', 'text' => array( 'content' => $r[0] . ': ' ), 'annotations' => array( 'bold' => true ) ) ), self::rt( $r[1] ) ) ),
);
}
$meta = $this->entry_meta();
if ( $meta['entry_url'] ) {
$blocks[] = array( 'object' => 'block', 'type' => 'paragraph', 'paragraph' => array( 'rich_text' => array( array( 'type' => 'text', 'text' => array( 'content' => __( 'View entry in WordPress', 'fieldwren' ), 'link' => array( 'url' => $meta['entry_url'] ) ) ) ) ) );
}
$body['children'] = $blocks;
}
$b = $this->api( 'POST', '/pages', $body );
/* translators: %s page URL */
return is_wp_error( $b ) ? $b : sprintf( __( 'Page created: %s', 'fieldwren' ), (string) ( $b['url'] ?? '' ) );
}
}