[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/src/js/bp-core/js/block-assets/data/ -> utils.js (source)

   1  /**
   2   * WordPress dependencies.
   3   */
   4  const {
   5      data: {
   6          useSelect,
   7      },
   8  } = wp;
   9  
  10  /**
  11   * External dependencies.
  12   */
  13  const {
  14      find,
  15      get,
  16  } = lodash;
  17  
  18  /**
  19   * Internal dependencies.
  20   */
  21  import BP_CORE_STORE_KEY from './register';
  22  
  23  /**
  24   * Checks whether a component or the feature of an active component is enabled.
  25   *
  26   * @since 9.0.0
  27   *
  28   * @param {string} component (required) The component to check.
  29   * @param {string} feature (optional) The feature to check.
  30   * @return {boolean} Whether a component or the feature of an active component is enabled.
  31   */
  32  export function isActive( component, feature = '' ) {
  33      const components = useSelect( ( select ) => {
  34          return select( BP_CORE_STORE_KEY ).getActiveComponents();
  35      }, [] );
  36  
  37      const activeComponent = find( components, ['name', component] );
  38  
  39      if ( ! feature ) {
  40          return !! activeComponent;
  41      }
  42  
  43      return get( activeComponent, [ 'features', feature ] );
  44  }
  45  
  46  export default isActive;
  47  
  48  /**
  49   * Checks whether a component or the feature of an active component is enabled.
  50   *
  51   * @since 9.0.0
  52   *
  53   * @return {array} An array of objects keyed by activity types.
  54   */
  55   export function activityTypes() {
  56      const components = useSelect( ( select ) => {
  57          return select( BP_CORE_STORE_KEY ).getActiveComponents();
  58      }, [] );
  59  
  60      const activityComponent = find( components, ['name', 'activity'] );
  61  
  62      if ( ! activityComponent ) {
  63          return [];
  64      }
  65  
  66      const activityTypes = get( activityComponent, [ 'features', 'types' ] );
  67      let activityTypesList = [];
  68  
  69      Object.entries( activityTypes ).forEach( ( [ type, label ] ) => {
  70          activityTypesList.push(
  71              {
  72                  label: label,
  73                  value: type,
  74              }
  75          )
  76      } );
  77  
  78      return activityTypesList;
  79  }
  80  
  81  /**
  82   * Returns the logged in user object.
  83   *
  84   * @since 9.0.0
  85   *
  86   * @return {Object} The logged in user object.
  87   */
  88  export function loggedInUser() {
  89      const loggedInUser = useSelect( ( select ) => {
  90          const store = select( 'core' );
  91  
  92          if ( store ) {
  93              return select( 'core' ).getCurrentUser();
  94          }
  95  
  96          return {};
  97      }, [] );
  98  
  99      return loggedInUser;
 100  }
 101  
 102  /**
 103   * Returns the post author user object.
 104   *
 105   * @since 9.0.0
 106   *
 107   * @return {Object} The post author user object.
 108   */
 109  export function postAuhor() {
 110      const postAuhor = useSelect( ( select ) => {
 111          const editorStore = select( 'core/editor' );
 112          const coreStore = select( 'core' );
 113  
 114          if ( editorStore && coreStore ) {
 115              const postAuthorId = editorStore.getCurrentPostAttribute( 'author' );
 116              const authorsList = coreStore.getAuthors();
 117  
 118              return find( authorsList, ['id', postAuthorId] );
 119          }
 120  
 121          return {};
 122      }, [] );
 123  
 124      return postAuhor;
 125  }
 126  
 127  /**
 128   * Returns the current post ID.
 129   *
 130   * @since 9.0.0
 131   *
 132   * @return {integer} The current post ID.
 133   */
 134  export function currentPostId() {
 135      const currentPostId = useSelect( ( select ) => {
 136          const store = select( 'core/editor' );
 137  
 138          if ( store ) {
 139              return store.getCurrentPostId();
 140          }
 141  
 142          return 0;
 143      }, [] );
 144  
 145      return currentPostId;
 146  }
 147  
 148  /**
 149   * Get the current sidebar of a Widget Block.
 150   *
 151   * @since 9.0.0
 152   *
 153   * @param {string} widgetClientId clientId of the sidebar widget.
 154   * @return {object} An object containing the sidebar Id.
 155   */
 156  export function getCurrentWidgetsSidebar( widgetClientId = '' ) {
 157      const currentWidgetsSidebar = useSelect( ( select ) => {
 158          const blockEditorStore = select( 'core/block-editor' );
 159          const widgetsStore = select( 'core/edit-widgets' );
 160  
 161          if ( widgetClientId && widgetsStore && blockEditorStore ) {
 162              const areas = blockEditorStore.getBlocks();
 163              const parents = blockEditorStore.getBlockParents( widgetClientId );
 164              let sidebars = [];
 165  
 166              areas.forEach( ( { clientId, attributes } ) => {
 167                  sidebars.push( {
 168                      id: attributes.id,
 169                      isCurrent: -1 !== parents.indexOf( clientId ),
 170                  } );
 171              } );
 172  
 173              return find( sidebars, ['isCurrent', true ] );
 174          }
 175  
 176          return {};
 177      }, [] );
 178  
 179      return currentWidgetsSidebar;
 180  }


Generated: Fri May 5 01:01:24 2023 Cross-referenced by PHPXref 0.7.1